From 39e14a1e793332ee03f0c0dda382355458bef774 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Fri, 21 Sep 2018 13:58:05 +0200 Subject: [PATCH 1/6] Fix coding style in Mover --- src/Mover.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Mover.php b/src/Mover.php index dee0a100..d7c8a663 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -89,7 +89,8 @@ public function movePackage(Package $package) * @param $path * @return mixed */ - public function moveFile(Package $package, $autoloader, $file, $path = '') { + public function moveFile(Package $package, $autoloader, $file, $path = '') + { if ($autoloader instanceof NamespaceAutoloader) { $namespacePath = $autoloader->getNamespacePath(); $replaceWith = $this->config->dep_directory . $namespacePath; From b9b070f1b0bc8060f47b027c6000f10c31752dae Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Wed, 17 Oct 2018 17:58:48 +0200 Subject: [PATCH 2/6] Add ability to find installed dependencies into Package --- src/Composer/Package.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Composer/Package.php b/src/Composer/Package.php index 3d392c75..0ead0cfa 100644 --- a/src/Composer/Package.php +++ b/src/Composer/Package.php @@ -15,6 +15,9 @@ class Package /** @var array */ public $autoloaders = []; + /** @var array */ + public $dependencies = []; + public function __construct($path) { $this->path = $path; @@ -47,4 +50,20 @@ public function findAutoloaders() array_push($this->autoloaders, $autoloader); } } + + public function findDependencies($vendor_path) + { + $this->dependencies = array_keys((array)$this->config->require); + if (isset($this->config->suggest) && $this->config->suggest) { + $this->dependencies = array_unique( + array_merge($this->dependencies, array_keys((array)$this->config->suggest)) + ); + } + $this->dependencies = array_filter( + $this->dependencies, + function ($dependency) use ($vendor_path) { + return file_exists($vendor_path . '/' . $dependency); + } + ); + } } From ab0af7b21443a96ea74aafa85a8da34828279943 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Wed, 17 Oct 2018 17:59:53 +0200 Subject: [PATCH 3/6] Add option for processing packages with its sub-dependencies --- src/Console/Commands/Compose.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index fab03a02..f3757909 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -6,6 +6,7 @@ use CoenJacobs\Mozart\Mover; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Compose extends Command @@ -13,12 +14,20 @@ class Compose extends Command protected function configure() { $this->setName('compose'); + $this->addOption( + 'with_dependencies', + null, + InputOption::VALUE_OPTIONAL, + 'Process also sub-dependencies.', + false + ); $this->setDescription('Composes all dependencies as a package inside a WordPress plugin.'); $this->setHelp(''); } protected function execute(InputInterface $input, OutputInterface $output) { + $with_dependencies = $input->getOption('with_dependencies') !== false; $workingDir = getcwd(); $config = json_decode(file_get_contents($workingDir . '/composer.json')); @@ -27,10 +36,23 @@ protected function execute(InputInterface $input, OutputInterface $output) $mover = new Mover($workingDir, $config); $mover->deleteTargetDirs(); - foreach ($config->packages as $package_slug) { + $packages = new \ArrayIterator($config->packages); + $processed_packages = []; + + foreach ($packages as $package_slug) { $package = new Package($workingDir . '/vendor/' . $package_slug); $package->findAutoloaders(); $mover->movePackage($package); + if (!$with_dependencies) { + continue; + } + $package->findDependencies($workingDir . '/vendor'); + $processed_packages[] = $package; + foreach ($package->dependencies as $dependency) { + if (!in_array($dependency, $packages->getArrayCopy())) { + $packages->append($dependency); + }; + } } $mover->replaceClassmapNames(); From 9ce5b142ca28afddada5bf8ffd7da9da31a5a757 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Wed, 17 Oct 2018 18:00:59 +0200 Subject: [PATCH 4/6] Add synchronization of class names within moved classes --- src/Console/Commands/Compose.php | 22 ++++++++ src/Synchronizer.php | 95 ++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/Synchronizer.php diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index f3757909..bf861240 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -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; @@ -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)); + } + } } } diff --git a/src/Synchronizer.php b/src/Synchronizer.php new file mode 100644 index 00000000..0d6fc56e --- /dev/null +++ b/src/Synchronizer.php @@ -0,0 +1,95 @@ +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); + } +} From e7f04ffc136f8175dec82615dcee43e234f001ae Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Tue, 23 Oct 2018 20:15:03 +0200 Subject: [PATCH 5/6] Make sub-dependencies precessing a default behavior --- src/Console/Commands/Compose.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index bf861240..1966a60e 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -16,10 +16,10 @@ protected function configure() { $this->setName('compose'); $this->addOption( - 'with_dependencies', + 'skip_dependencies', null, InputOption::VALUE_OPTIONAL, - 'Process also sub-dependencies.', + 'Skip processing of package sub-dependencies.', false ); $this->setDescription('Composes all dependencies as a package inside a WordPress plugin.'); @@ -28,7 +28,7 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $with_dependencies = $input->getOption('with_dependencies') !== false; + $skip_dependencies = $input->getOption('skip_dependencies') !== false; $workingDir = getcwd(); $config = json_decode(file_get_contents($workingDir . '/composer.json')); @@ -44,7 +44,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $package = new Package($workingDir . '/vendor/' . $package_slug); $package->findAutoloaders(); $mover->movePackage($package); - if (!$with_dependencies) { + if ($skip_dependencies) { continue; } $package->findDependencies($workingDir . '/vendor'); @@ -58,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $mover->replaceClassmapNames(); - if (!$with_dependencies) { + if ($skip_dependencies) { return; } From 3ba81efb0a4e3b43531e1f1188e3fc290b03d2c2 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Sun, 9 Dec 2018 20:40:26 +0100 Subject: [PATCH 6/6] Process only explicitly mentioned sub-dependencies --- src/Composer/Package.php | 15 +++++---------- src/Console/Commands/Compose.php | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Composer/Package.php b/src/Composer/Package.php index 0ead0cfa..b92285d5 100644 --- a/src/Composer/Package.php +++ b/src/Composer/Package.php @@ -18,6 +18,9 @@ class Package /** @var array */ public $dependencies = []; + /** @var array */ + public $suggested_dependencies = []; + public function __construct($path) { $this->path = $path; @@ -51,19 +54,11 @@ public function findAutoloaders() } } - public function findDependencies($vendor_path) + public function findDependencies() { $this->dependencies = array_keys((array)$this->config->require); if (isset($this->config->suggest) && $this->config->suggest) { - $this->dependencies = array_unique( - array_merge($this->dependencies, array_keys((array)$this->config->suggest)) - ); + $this->suggested_dependencies = array_keys((array)$this->config->suggest); } - $this->dependencies = array_filter( - $this->dependencies, - function ($dependency) use ($vendor_path) { - return file_exists($vendor_path . '/' . $dependency); - } - ); } } diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index 1966a60e..2298ab48 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -47,10 +47,12 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($skip_dependencies) { continue; } - $package->findDependencies($workingDir . '/vendor'); + $package->findDependencies(); $processed_packages[] = $package; foreach ($package->dependencies as $dependency) { - if (!in_array($dependency, $packages->getArrayCopy())) { + if (file_exists($workingDir . '/vendor/' . $dependency) && + !in_array($dependency, $packages->getArrayCopy()) + ) { $packages->append($dependency); }; } @@ -68,14 +70,20 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!$package->dependencies) { continue; } - foreach ($package->dependencies as $dependency_name) { + $dependencies = array_unique(array_merge($package->dependencies, $package->suggested_dependencies)); + foreach ($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)); + $dependent_package = reset($matching_packages); + // Suggested dependency might not be installed + if (!$dependent_package) { + continue; + } + $synchronizer->syncMovedPackageWithDependency($package, $dependent_package); } } }