Skip to content

Commit

Permalink
Add synchronization of class names within moved classes
Browse files Browse the repository at this point in the history
  • Loading branch information
costasovo committed Oct 17, 2018
1 parent ab0af7b commit 9ce5b14
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Console/Commands/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CoenJacobs\Mozart\Composer\Package;
use CoenJacobs\Mozart\Mover;
use CoenJacobs\Mozart\Synchronizer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -56,5 +57,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$mover->replaceClassmapNames();

if (!$with_dependencies) {
return;
}

$synchronizer = new Synchronizer($workingDir, $config);

foreach ($processed_packages as $package) {
if (!$package->dependencies) {
continue;
}
foreach ($package->dependencies as $dependency_name) {
$matching_packages = array_filter(
$processed_packages,
function (Package $pack) use ($dependency_name) {
return $pack->config->name === $dependency_name;
}
);
$synchronizer->syncMovedPackageWithDependency($package, reset($matching_packages));
}
}
}
}
95 changes: 95 additions & 0 deletions src/Synchronizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace CoenJacobs\Mozart;

use CoenJacobs\Mozart\Composer\Autoload\Classmap;
use CoenJacobs\Mozart\Composer\Autoload\NamespaceAutoloader;
use CoenJacobs\Mozart\Composer\Package;
use CoenJacobs\Mozart\Replace\ClassmapReplacer;
use CoenJacobs\Mozart\Replace\NamespaceReplacer;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\Finder\Finder;

class Synchronizer
{
/** @var string */
protected $workingDir;

/** @var string */
protected $targetDir;

/** @var \stdClass */
protected $config;

/** @var Filesystem */
protected $filesystem;

public function __construct($workingDir, $config)
{
$this->workingDir = $workingDir;
$this->targetDir = $config->dep_directory;
$this->config = $config;

$this->filesystem = new Filesystem(new Local($this->workingDir));
}


public function syncMovedPackageWithDependency(Package $package, Package $dependency)
{
$finder = new Finder();

foreach ($package->autoloaders as $autoloader) {
if ($autoloader instanceof NamespaceAutoloader) {
$source_path = $this->workingDir . $this->targetDir . $autoloader->getNamespacePath();
$finder->files()->in($source_path);

foreach ($finder as $file) {
if ('.php' == substr($file, '-4', 4)) {
foreach ($dependency->autoloaders as $dep_autoloader) {
$targetFile = str_replace($this->workingDir, '', $file->getRealPath());
$this->replaceInFile($targetFile, $dep_autoloader);
}
}
}
}

if ($autoloader instanceof Classmap) {
$finder = new Finder();
$source_path = $this->workingDir . $this->config->classmap_directory . $package->config->name;
$finder->files()->in($source_path);

foreach ($finder as $foundFile) {
if ('.php' == substr($foundFile, '-4', 4)) {
foreach ($dependency->autoloaders as $dep_autoloader) {
$targetFile = str_replace($this->workingDir, '', $foundFile->getRealPath());
$this->replaceInFile($targetFile, $dep_autoloader);
}
}
}
}
}
}

/**
* @param $targetFile
* @param $autoloader
*/
public function replaceInFile($targetFile, $autoloader)
{
$contents = $this->filesystem->read($targetFile);

if ($autoloader instanceof NamespaceAutoloader) {
$replacer = new NamespaceReplacer();
$replacer->dep_namespace = $this->config->dep_namespace;
} else {
$replacer = new ClassmapReplacer();
$replacer->classmap_prefix = $this->config->classmap_prefix;
}

$replacer->setAutoloader($autoloader);
$contents = $replacer->replace($contents);

$this->filesystem->put($targetFile, $contents);
}
}

0 comments on commit 9ce5b14

Please sign in to comment.