From 19608228bc32bc8efaa49c47bfe3903bc5993843 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Tue, 29 Jan 2019 23:48:07 +0100 Subject: [PATCH 01/12] Recursively load all packages in full tree before processing --- src/Console/Commands/Compose.php | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index fab03a02..bf2ae0d3 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -27,12 +27,37 @@ protected function execute(InputInterface $input, OutputInterface $output) $mover = new Mover($workingDir, $config); $mover->deleteTargetDirs(); - foreach ($config->packages as $package_slug) { - $package = new Package($workingDir . '/vendor/' . $package_slug); - $package->findAutoloaders(); + $packages = $this->findPackages($workingDir, $config->packages, []); + + foreach( $packages as $package ) { $mover->movePackage($package); } $mover->replaceClassmapNames(); } + + /** + * Loops through all dependencies and their dependencies and so on... + * will eventually return a list of all packages required by the full tree. + */ + private function findPackages($workingDir, $slugs, $packages) + { + foreach ($slugs as $package_slug) { + $packageDir = $workingDir . '/vendor/' . $package_slug .'/'; + + if (! is_dir($packageDir) ) { + continue; + } + + $package = new Package($packageDir); + $package->findAutoloaders(); + $packages[] = $package; + + $config = json_decode(file_get_contents($packageDir . 'composer.json')); + $dependencies = array_keys( (array) $config->require); + $packages = $this->findPackages($workingDir, $dependencies, $packages); + } + + return $packages; + } } From ace879730bf8e8a142eed1c51a83deefef0b2ec6 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 30 Jan 2019 15:52:44 +0100 Subject: [PATCH 02/12] Send packages to mover one by one, starting with dependencies at deepest level --- src/Composer/Package.php | 3 +++ src/Console/Commands/Compose.php | 27 +++++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Composer/Package.php b/src/Composer/Package.php index 3d392c75..5088775e 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; diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index bf2ae0d3..894743d7 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -27,21 +27,35 @@ protected function execute(InputInterface $input, OutputInterface $output) $mover = new Mover($workingDir, $config); $mover->deleteTargetDirs(); - $packages = $this->findPackages($workingDir, $config->packages, []); + $packages = $this->findPackages($workingDir, $config->packages); foreach( $packages as $package ) { - $mover->movePackage($package); + $this->movePackage($package, $mover); } + } - $mover->replaceClassmapNames(); + /** + * Move all the packages over, one by one, starting on the deepest level of dependencies. + */ + public function movePackage($package, $mover) + { + if ( ! empty( $package->dependencies ) ) { + foreach( $package->dependencies as $dependency ) { + $this->movePackage($dependency, $mover); + } + } + + $mover->movePackage($package); } /** * Loops through all dependencies and their dependencies and so on... * will eventually return a list of all packages required by the full tree. */ - private function findPackages($workingDir, $slugs, $packages) + private function findPackages($workingDir, $slugs) { + $packages = []; + foreach ($slugs as $package_slug) { $packageDir = $workingDir . '/vendor/' . $package_slug .'/'; @@ -51,11 +65,12 @@ private function findPackages($workingDir, $slugs, $packages) $package = new Package($packageDir); $package->findAutoloaders(); - $packages[] = $package; $config = json_decode(file_get_contents($packageDir . 'composer.json')); $dependencies = array_keys( (array) $config->require); - $packages = $this->findPackages($workingDir, $dependencies, $packages); + + $package->dependencies = $this->findPackages($workingDir, $dependencies); + $packages[] = $package; } return $packages; From e272f46e3ee96da50c9a29b0e2c6b6f16cd46b5e Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 30 Jan 2019 19:57:56 +0100 Subject: [PATCH 03/12] Replace packages one by one, starting with dependencies at deepest level --- src/Console/Commands/Compose.php | 21 ++++++ src/Mover.php | 67 +--------------- src/Replacer.php | 126 +++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 65 deletions(-) create mode 100644 src/Replacer.php diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index 894743d7..e45ee73d 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\Replacer; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -32,6 +33,12 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach( $packages as $package ) { $this->movePackage($package, $mover); } + + $replacer = new Replacer($workingDir, $config); + + foreach( $packages as $package ) { + $this->replacePackage($package, $replacer); + } } /** @@ -48,6 +55,20 @@ public function movePackage($package, $mover) $mover->movePackage($package); } + /** + * Replace contents of all the packages, one by one, starting on the deepest level of dependencies. + */ + public function replacePackage($package, $replacer) + { + if ( ! empty( $package->dependencies ) ) { + foreach( $package->dependencies as $dependency ) { + $this->replacePackage($dependency, $replacer); + } + } + + $replacer->replacePackage($package); + } + /** * Loops through all dependencies and their dependencies and so on... * will eventually return a list of all packages required by the full tree. diff --git a/src/Mover.php b/src/Mover.php index dee0a100..c290f485 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -22,9 +22,6 @@ class Mover /** @var \stdClass */ protected $config; - /** @var array */ - protected $replacedClasses = []; - /** @var Filesystem */ protected $filesystem; @@ -56,11 +53,7 @@ public function movePackage(Package $package) $finder->files()->in($source_path); foreach ($finder as $file) { - $targetFile = $this->moveFile($package, $autoloader, $file, $path); - - if ('.php' == substr($targetFile, '-4', 4)) { - $this->replaceInFile($targetFile, $autoloader); - } + $this->moveFile($package, $autoloader, $file, $path); } } @@ -71,11 +64,7 @@ public function movePackage(Package $package) $finder->files()->name($file)->in($source_path); foreach ($finder as $foundFile) { - $targetFile = $this->moveFile($package, $autoloader, $foundFile); - - if ('.php' == substr($targetFile, '-4', 4)) { - $this->replaceInFile($targetFile, $autoloader); - } + $this->moveFile($package, $autoloader, $foundFile); } } } @@ -109,56 +98,4 @@ public function moveFile(Package $package, $autoloader, $file, $path = '') { return $targetFile; } - - public function replaceClassmapNames() - { - $classmap_path = $this->workingDir . $this->config->classmap_directory; - $finder = new Finder(); - $finder->files()->in($classmap_path); - - $filesystem = new Filesystem(new Local($this->workingDir)); - - foreach ($finder as $file) { - $file_path = str_replace($this->workingDir, '', $file->getRealPath()); - $contents = $filesystem->read($file_path); - - foreach ($this->replacedClasses as $original => $replacement) { - $contents = preg_replace_callback( - '/\W(?put($file_path, $contents); - } - } - - /** - * @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); - - if ($replacer instanceof ClassmapReplacer) { - $this->replacedClasses = array_merge($this->replacedClasses, $replacer->replacedClasses); - } - - $this->filesystem->put($targetFile, $contents); - } } diff --git a/src/Replacer.php b/src/Replacer.php new file mode 100644 index 00000000..f6a0d786 --- /dev/null +++ b/src/Replacer.php @@ -0,0 +1,126 @@ +workingDir = $workingDir; + $this->targetDir = $config->dep_directory; + $this->config = $config; + + $this->filesystem = new Filesystem(new Local($this->workingDir)); + } + + public function replacePackage(Package $package) + { + $finder = new Finder(); + + foreach ($package->autoloaders as $autoloader) { + $source_path = $this->workingDir . $this->targetDir . str_replace('\\', '/', $autoloader->namespace) .'/'; + $finder->files()->in($source_path); + + foreach ($finder as $file) { + $targetFile = $file->getPathName(); + + if ('.php' == substr($targetFile, '-4', 4)) { + $this->replaceInFile($targetFile, $autoloader); + } + } + + if ($autoloader instanceof Classmap && ! empty($autoloader->files)) { + foreach ($autoloader->files as $file) { + $finder = new Finder(); + $source_path = $this->workingDir . $this->targetDir . $package->config->name; + $finder->files()->name($file)->in($source_path); + + foreach ($finder as $foundFile) { + $targetFile = $foundFile->getRealPath(); + + if ('.php' == substr($targetFile, '-4', 4)) { + $this->replaceInFile($targetFile, $autoloader); + } + } + } + } + } + } + + public function replaceClassmapNames() + { + $classmap_path = $this->workingDir . $this->config->classmap_directory; + $finder = new Finder(); + $finder->files()->in($classmap_path); + + $filesystem = new Filesystem(new Local($this->workingDir)); + + foreach ($finder as $file) { + $file_path = str_replace($this->workingDir, '', $file->getPath()); + $contents = $filesystem->read($file_path); + + foreach ($this->replacedClasses as $original => $replacement) { + $contents = preg_replace_callback( + '/\W(?put($file_path, $contents); + } + } + + /** + * @param $targetFile + * @param $autoloader + */ + public function replaceInFile($targetFile, $autoloader) + { + $targetFile = str_replace($this->workingDir, '', $targetFile); + $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); + + if ($replacer instanceof ClassmapReplacer) { + $this->replacedClasses = array_merge($this->replacedClasses, $replacer->replacedClasses); + } + + $this->filesystem->put($targetFile, $contents); + } +} \ No newline at end of file From 28a052154ee67e22675819e9d6fa667c7318f4e6 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Wed, 30 Jan 2019 20:08:42 +0100 Subject: [PATCH 04/12] Split core functionality bits in two main methods --- src/Console/Commands/Compose.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index e45ee73d..91a04e39 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -25,18 +25,37 @@ protected function execute(InputInterface $input, OutputInterface $output) $config = json_decode(file_get_contents($workingDir . '/composer.json')); $config = $config->extra->mozart; + $packages = $this->findPackages($workingDir, $config->packages); + + $this->movePackages($workingDir, $config, $packages); + $this->replacePackages($workingDir, $config, $packages); + } + + /** + * @param $workingDir + * @param $config + * @param array $packages + */ + protected function movePackages($workingDir, $config, $packages) + { $mover = new Mover($workingDir, $config); $mover->deleteTargetDirs(); - $packages = $this->findPackages($workingDir, $config->packages); - - foreach( $packages as $package ) { + foreach ($packages as $package) { $this->movePackage($package, $mover); } + } + /** + * @param $workingDir + * @param $config + * @param array $packages + */ + protected function replacePackages($workingDir, $config, $packages) + { $replacer = new Replacer($workingDir, $config); - foreach( $packages as $package ) { + foreach ($packages as $package) { $this->replacePackage($package, $replacer); } } From 1b3d076b040cf85ba510990243a544b63ebae96c Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 31 Jan 2019 12:16:59 +0100 Subject: [PATCH 05/12] Replace namespaces of dependencies in parent package --- src/Console/Commands/Compose.php | 83 ++++++++++++++++++++++++-------- src/Mover.php | 4 +- src/Replacer.php | 73 +++++++++++++++++----------- 3 files changed, 108 insertions(+), 52 deletions(-) diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index 91a04e39..4ebc59cf 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -11,6 +11,18 @@ class Compose extends Command { + /** @var Mover */ + private $mover; + + /** @var Replacer */ + private $replacer; + + /** @var string */ + private $workingDir; + + /** @var */ + private $config; + protected function configure() { $this->setName('compose'); @@ -21,14 +33,23 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $workingDir = getcwd(); + $this->workingDir = $workingDir; $config = json_decode(file_get_contents($workingDir . '/composer.json')); $config = $config->extra->mozart; + $this->config = $config; + + $this->mover = new Mover($workingDir, $config); + $this->replacer = new Replacer($workingDir, $config); - $packages = $this->findPackages($workingDir, $config->packages); + $packages = $this->findPackages($config->packages); - $this->movePackages($workingDir, $config, $packages); - $this->replacePackages($workingDir, $config, $packages); + $this->movePackages($packages); + $this->replacePackages($packages); + + foreach( $packages as $package ) { + $this->replaceParentPackage($package, null); + } } /** @@ -36,13 +57,12 @@ protected function execute(InputInterface $input, OutputInterface $output) * @param $config * @param array $packages */ - protected function movePackages($workingDir, $config, $packages) + protected function movePackages($packages) { - $mover = new Mover($workingDir, $config); - $mover->deleteTargetDirs(); + $this->mover->deleteTargetDirs(); foreach ($packages as $package) { - $this->movePackage($package, $mover); + $this->movePackage($package); } } @@ -51,53 +71,70 @@ protected function movePackages($workingDir, $config, $packages) * @param $config * @param array $packages */ - protected function replacePackages($workingDir, $config, $packages) + protected function replacePackages($packages) { - $replacer = new Replacer($workingDir, $config); - foreach ($packages as $package) { - $this->replacePackage($package, $replacer); + $this->replacePackage($package); } } /** * Move all the packages over, one by one, starting on the deepest level of dependencies. */ - public function movePackage($package, $mover) + public function movePackage($package) { if ( ! empty( $package->dependencies ) ) { foreach( $package->dependencies as $dependency ) { - $this->movePackage($dependency, $mover); + $this->movePackage($dependency); } } - $mover->movePackage($package); + $this->mover->movePackage($package); } /** * Replace contents of all the packages, one by one, starting on the deepest level of dependencies. */ - public function replacePackage($package, $replacer) + public function replacePackage($package) { if ( ! empty( $package->dependencies ) ) { foreach( $package->dependencies as $dependency ) { - $this->replacePackage($dependency, $replacer); + $this->replacePackage($dependency); + } + } + + $this->replacer->replacePackage($package); + } + + protected function replaceParentPackage(Package $package, $parent) + { + if ( $parent !== null ) { + // Replace everything in parent, based on the dependencies + foreach( $parent->autoloaders as $parentAutoloader ) { + foreach( $package->autoloaders as $autoloader ) { + $directory = $this->workingDir . $this->config->dep_directory . str_replace('\\', '/', $parentAutoloader->namespace) . '/'; + $this->replacer->replaceInDirectory($autoloader, $directory); + } } } - $replacer->replacePackage($package); + if ( ! empty($package->dependencies)) { + foreach ($package->dependencies as $dependency) { + $this->replaceParentPackage($dependency, $package); + } + } } /** * Loops through all dependencies and their dependencies and so on... * will eventually return a list of all packages required by the full tree. */ - private function findPackages($workingDir, $slugs) + private function findPackages($slugs) { $packages = []; foreach ($slugs as $package_slug) { - $packageDir = $workingDir . '/vendor/' . $package_slug .'/'; + $packageDir = $this->workingDir . '/vendor/' . $package_slug .'/'; if (! is_dir($packageDir) ) { continue; @@ -107,9 +144,13 @@ private function findPackages($workingDir, $slugs) $package->findAutoloaders(); $config = json_decode(file_get_contents($packageDir . 'composer.json')); - $dependencies = array_keys( (array) $config->require); - $package->dependencies = $this->findPackages($workingDir, $dependencies); + $dependencies = []; + if ( isset( $config->require) ) { + $dependencies = array_keys((array)$config->require); + } + + $package->dependencies = $this->findPackages($dependencies); $packages[] = $package; } diff --git a/src/Mover.php b/src/Mover.php index c290f485..b19e8bf3 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -5,8 +5,6 @@ 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; @@ -86,7 +84,7 @@ public function moveFile(Package $package, $autoloader, $file, $path = '') { $targetFile = str_replace('/vendor/' . $package->config->name . '/' . $path, '', $targetFile); } else { $namespacePath = $package->config->name; - $replaceWith = $this->config->classmap_directory . $namespacePath; + $replaceWith = $this->config->classmap_directory . '/' . $namespacePath; $targetFile = str_replace($this->workingDir, $replaceWith, $file->getRealPath()); $targetFile = str_replace('/vendor/' . $package->config->name . '/', '/', $targetFile); } diff --git a/src/Replacer.php b/src/Replacer.php index f6a0d786..7543f1dc 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -39,35 +39,8 @@ public function __construct($workingDir, $config) public function replacePackage(Package $package) { - $finder = new Finder(); - foreach ($package->autoloaders as $autoloader) { - $source_path = $this->workingDir . $this->targetDir . str_replace('\\', '/', $autoloader->namespace) .'/'; - $finder->files()->in($source_path); - - foreach ($finder as $file) { - $targetFile = $file->getPathName(); - - if ('.php' == substr($targetFile, '-4', 4)) { - $this->replaceInFile($targetFile, $autoloader); - } - } - - if ($autoloader instanceof Classmap && ! empty($autoloader->files)) { - foreach ($autoloader->files as $file) { - $finder = new Finder(); - $source_path = $this->workingDir . $this->targetDir . $package->config->name; - $finder->files()->name($file)->in($source_path); - - foreach ($finder as $foundFile) { - $targetFile = $foundFile->getRealPath(); - - if ('.php' == substr($targetFile, '-4', 4)) { - $this->replaceInFile($targetFile, $autoloader); - } - } - } - } + $this->replacePackageByAutoloader($package, $autoloader); } } @@ -123,4 +96,48 @@ public function replaceInFile($targetFile, $autoloader) $this->filesystem->put($targetFile, $contents); } + + /** + * @param Package $package + * @param $autoloader + */ + public function replacePackageByAutoloader(Package $package, $autoloader) + { + if ($autoloader instanceof NamespaceAutoloader) { + $source_path = $this->workingDir . $this->targetDir . str_replace('\\', '/', $autoloader->namespace) . '/'; + $this->replaceInDirectory($autoloader, $source_path); + } elseif ($autoloader instanceof Classmap && !empty($autoloader->files)) { + foreach ($autoloader->files as $file) { + $finder = new Finder(); + $source_path = $this->workingDir . $this->targetDir . $package->config->name; + $finder->files()->name($file)->in($source_path); + + foreach ($finder as $foundFile) { + $targetFile = $foundFile->getRealPath(); + + if ('.php' == substr($targetFile, '-4', 4)) { + $this->replaceInFile($targetFile, $autoloader); + } + } + } + } + } + + /** + * @param $autoloader + * @param $directory + */ + public function replaceInDirectory($autoloader, $directory) + { + $finder = new Finder(); + $finder->files()->in($directory); + + foreach ($finder as $file) { + $targetFile = $file->getPathName(); + + if ('.php' == substr($targetFile, '-4', 4)) { + $this->replaceInFile($targetFile, $autoloader); + } + } + } } \ No newline at end of file From 72488b5403da6a6e19cf747a376d1585468feaa5 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 31 Jan 2019 13:06:33 +0100 Subject: [PATCH 06/12] Reinstate the original classmap replacer without extra method --- src/Replacer.php | 48 ++++++++++-------------------------------------- 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/src/Replacer.php b/src/Replacer.php index 7543f1dc..e09707ce 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -44,32 +44,6 @@ public function replacePackage(Package $package) } } - public function replaceClassmapNames() - { - $classmap_path = $this->workingDir . $this->config->classmap_directory; - $finder = new Finder(); - $finder->files()->in($classmap_path); - - $filesystem = new Filesystem(new Local($this->workingDir)); - - foreach ($finder as $file) { - $file_path = str_replace($this->workingDir, '', $file->getPath()); - $contents = $filesystem->read($file_path); - - foreach ($this->replacedClasses as $original => $replacement) { - $contents = preg_replace_callback( - '/\W(?put($file_path, $contents); - } - } - /** * @param $targetFile * @param $autoloader @@ -106,18 +80,16 @@ public function replacePackageByAutoloader(Package $package, $autoloader) if ($autoloader instanceof NamespaceAutoloader) { $source_path = $this->workingDir . $this->targetDir . str_replace('\\', '/', $autoloader->namespace) . '/'; $this->replaceInDirectory($autoloader, $source_path); - } elseif ($autoloader instanceof Classmap && !empty($autoloader->files)) { - foreach ($autoloader->files as $file) { - $finder = new Finder(); - $source_path = $this->workingDir . $this->targetDir . $package->config->name; - $finder->files()->name($file)->in($source_path); - - foreach ($finder as $foundFile) { - $targetFile = $foundFile->getRealPath(); - - if ('.php' == substr($targetFile, '-4', 4)) { - $this->replaceInFile($targetFile, $autoloader); - } + } elseif ($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) { + $targetFile = $foundFile->getRealPath(); + + if ('.php' == substr($targetFile, '-4', 4)) { + $this->replaceInFile($targetFile, $autoloader); } } } From 5d473b883f2b963b1bf02ec95a973f094703164a Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 31 Jan 2019 13:14:07 +0100 Subject: [PATCH 07/12] Correctly move path based classmap autoloader files --- src/Mover.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Mover.php b/src/Mover.php index b19e8bf3..9a23c6b3 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -45,17 +45,17 @@ public function movePackage(Package $package) $finder = new Finder(); foreach ($package->autoloaders as $autoloader) { - foreach ($autoloader->paths as $path) { - $source_path = $this->workingDir . '/vendor/' . $package->config->name . '/' . $path; + if ($autoloader instanceof NamespaceAutoloader ) { + foreach ($autoloader->paths as $path) { + $source_path = $this->workingDir . '/vendor/' . $package->config->name . '/' . $path; - $finder->files()->in($source_path); + $finder->files()->in($source_path); - foreach ($finder as $file) { - $this->moveFile($package, $autoloader, $file, $path); + foreach ($finder as $file) { + $this->moveFile($package, $autoloader, $file, $path); + } } - } - - if ($autoloader instanceof Classmap && ! empty($autoloader->files)) { + } elseif ($autoloader instanceof Classmap) { foreach ($autoloader->files as $file) { $finder = new Finder(); $source_path = $this->workingDir . '/vendor/' . $package->config->name; @@ -65,6 +65,16 @@ public function movePackage(Package $package) $this->moveFile($package, $autoloader, $foundFile); } } + + foreach ($autoloader->paths as $path) { + $source_path = $this->workingDir . '/vendor/' . $package->config->name; + + $finder->files()->in($source_path); + + foreach ($finder as $file) { + $this->moveFile($package, $autoloader, $file); + } + } } } } From efdfab0b6c9081c0b3d639fa9a59ec9288bd4931 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 31 Jan 2019 15:51:13 +0100 Subject: [PATCH 08/12] Implement classmap replacer for parent packages --- src/Console/Commands/Compose.php | 23 +++++++++++++++++++++-- src/Replacer.php | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index 4ebc59cf..b050b8ce 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -2,6 +2,8 @@ namespace CoenJacobs\Mozart\Console\Commands; +use CoenJacobs\Mozart\Composer\Autoload\Classmap; +use CoenJacobs\Mozart\Composer\Autoload\NamespaceAutoloader; use CoenJacobs\Mozart\Composer\Package; use CoenJacobs\Mozart\Mover; use CoenJacobs\Mozart\Replacer; @@ -112,8 +114,25 @@ protected function replaceParentPackage(Package $package, $parent) // Replace everything in parent, based on the dependencies foreach( $parent->autoloaders as $parentAutoloader ) { foreach( $package->autoloaders as $autoloader ) { - $directory = $this->workingDir . $this->config->dep_directory . str_replace('\\', '/', $parentAutoloader->namespace) . '/'; - $this->replacer->replaceInDirectory($autoloader, $directory); + if ($parentAutoloader instanceof NamespaceAutoloader) { + $directory = $this->workingDir . $this->config->dep_directory . str_replace('\\', '/', $parentAutoloader->namespace) . '/'; + + if ( $autoloader instanceof NamespaceAutoloader ) { + $this->replacer->replaceInDirectory($autoloader, $directory); + } else { + $directory = str_replace($this->workingDir, '', $directory); + $this->replacer->replaceParentClassesInDirectory($directory); + } + } else { + $directory = $this->workingDir . $this->config->classmap_directory . $parent->config->name; + + if ( $autoloader instanceof NamespaceAutoloader ) { + $this->replacer->replaceInDirectory($autoloader, $directory); + } else { + $directory = str_replace($this->workingDir, '', $directory); + $this->replacer->replaceParentClassesInDirectory($directory); + } + } } } } diff --git a/src/Replacer.php b/src/Replacer.php index e09707ce..1ee2ad52 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -95,6 +95,33 @@ public function replacePackageByAutoloader(Package $package, $autoloader) } } + /** + * @param $autoloader + * @param $directory + */ + public function replaceParentClassesInDirectory($directory) + { + $directory = trim($directory, '//'); + $finder = new Finder(); + $finder->files()->in($directory); + + $replacedClasses = $this->replacedClasses; + + foreach ($finder as $file) { + $targetFile = $file->getPathName(); + + if ('.php' == substr($targetFile, '-4', 4)) { + $contents = $this->filesystem->read($targetFile); + + foreach( $replacedClasses as $key => $value ) { + $contents = str_replace($key, $value, $contents); + } + + $this->filesystem->put($targetFile, $contents); + } + } + } + /** * @param $autoloader * @param $directory From e70e17ec26f0d2bde4af298b9a4fad5db6733e46 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 31 Jan 2019 15:56:39 +0100 Subject: [PATCH 09/12] Coding style fixes --- phpcs.xml | 3 ++- src/Console/Commands/Compose.php | 29 ++++++++++++----------- src/Mover.php | 5 ++-- src/Replacer.php | 4 ++-- tests/replacers/ClassMapReplacerTest.php | 3 ++- tests/replacers/NamespaceReplacerTest.php | 3 ++- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index 030316de..495a8b48 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -3,5 +3,6 @@ Coding standard ruleset based on the PSR-2 coding standard. - ./src/ + vendor/* + tests/* \ No newline at end of file diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index b050b8ce..80ffeb01 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -49,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->movePackages($packages); $this->replacePackages($packages); - foreach( $packages as $package ) { + foreach ($packages as $package) { $this->replaceParentPackage($package, null); } } @@ -85,8 +85,8 @@ protected function replacePackages($packages) */ public function movePackage($package) { - if ( ! empty( $package->dependencies ) ) { - foreach( $package->dependencies as $dependency ) { + if (! empty($package->dependencies)) { + foreach ($package->dependencies as $dependency) { $this->movePackage($dependency); } } @@ -99,8 +99,8 @@ public function movePackage($package) */ public function replacePackage($package) { - if ( ! empty( $package->dependencies ) ) { - foreach( $package->dependencies as $dependency ) { + if (! empty($package->dependencies)) { + foreach ($package->dependencies as $dependency) { $this->replacePackage($dependency); } } @@ -110,14 +110,15 @@ public function replacePackage($package) protected function replaceParentPackage(Package $package, $parent) { - if ( $parent !== null ) { + if ($parent !== null) { // Replace everything in parent, based on the dependencies - foreach( $parent->autoloaders as $parentAutoloader ) { - foreach( $package->autoloaders as $autoloader ) { + foreach ($parent->autoloaders as $parentAutoloader) { + foreach ($package->autoloaders as $autoloader) { if ($parentAutoloader instanceof NamespaceAutoloader) { - $directory = $this->workingDir . $this->config->dep_directory . str_replace('\\', '/', $parentAutoloader->namespace) . '/'; + $namespace = str_replace('\\', '/', $parentAutoloader->namespace); + $directory = $this->workingDir . $this->config->dep_directory . $namespace . '/'; - if ( $autoloader instanceof NamespaceAutoloader ) { + if ($autoloader instanceof NamespaceAutoloader) { $this->replacer->replaceInDirectory($autoloader, $directory); } else { $directory = str_replace($this->workingDir, '', $directory); @@ -126,7 +127,7 @@ protected function replaceParentPackage(Package $package, $parent) } else { $directory = $this->workingDir . $this->config->classmap_directory . $parent->config->name; - if ( $autoloader instanceof NamespaceAutoloader ) { + if ($autoloader instanceof NamespaceAutoloader) { $this->replacer->replaceInDirectory($autoloader, $directory); } else { $directory = str_replace($this->workingDir, '', $directory); @@ -137,7 +138,7 @@ protected function replaceParentPackage(Package $package, $parent) } } - if ( ! empty($package->dependencies)) { + if (! empty($package->dependencies)) { foreach ($package->dependencies as $dependency) { $this->replaceParentPackage($dependency, $package); } @@ -155,7 +156,7 @@ private function findPackages($slugs) foreach ($slugs as $package_slug) { $packageDir = $this->workingDir . '/vendor/' . $package_slug .'/'; - if (! is_dir($packageDir) ) { + if (! is_dir($packageDir)) { continue; } @@ -165,7 +166,7 @@ private function findPackages($slugs) $config = json_decode(file_get_contents($packageDir . 'composer.json')); $dependencies = []; - if ( isset( $config->require) ) { + if (isset($config->require)) { $dependencies = array_keys((array)$config->require); } diff --git a/src/Mover.php b/src/Mover.php index 9a23c6b3..1f5e7011 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -45,7 +45,7 @@ public function movePackage(Package $package) $finder = new Finder(); foreach ($package->autoloaders as $autoloader) { - if ($autoloader instanceof NamespaceAutoloader ) { + if ($autoloader instanceof NamespaceAutoloader) { foreach ($autoloader->paths as $path) { $source_path = $this->workingDir . '/vendor/' . $package->config->name . '/' . $path; @@ -86,7 +86,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; diff --git a/src/Replacer.php b/src/Replacer.php index 1ee2ad52..634df0ba 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -113,7 +113,7 @@ public function replaceParentClassesInDirectory($directory) if ('.php' == substr($targetFile, '-4', 4)) { $contents = $this->filesystem->read($targetFile); - foreach( $replacedClasses as $key => $value ) { + foreach ($replacedClasses as $key => $value) { $contents = str_replace($key, $value, $contents); } @@ -139,4 +139,4 @@ public function replaceInDirectory($autoloader, $directory) } } } -} \ No newline at end of file +} diff --git a/tests/replacers/ClassMapReplacerTest.php b/tests/replacers/ClassMapReplacerTest.php index 3a112333..f0c4f586 100644 --- a/tests/replacers/ClassMapReplacerTest.php +++ b/tests/replacers/ClassMapReplacerTest.php @@ -66,7 +66,8 @@ public function it_stores_replaced_class_names() } /** @test */ - public function it_replaces_class_declarations_psr2() { + public function it_replaces_class_declarations_psr2() + { $contents = "class Hello_World\n{"; $replacer = new ClassmapReplacer(); $replacer->classmap_prefix = 'Mozart_'; diff --git a/tests/replacers/NamespaceReplacerTest.php b/tests/replacers/NamespaceReplacerTest.php index 111b541e..76c0f58d 100644 --- a/tests/replacers/NamespaceReplacerTest.php +++ b/tests/replacers/NamespaceReplacerTest.php @@ -30,7 +30,8 @@ public function it_replaces_namespace_declarations() /** @test */ - public function it_doesnt_replaces_namespace_inside_namespace() { + public function it_doesnt_replaces_namespace_inside_namespace() + { $autoloader = new Psr0(); $autoloader->namespace = 'Test'; From e67633f3bba672cf1f34d65f0016d8bb6c6ddae5 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sat, 2 Feb 2019 10:50:45 +0100 Subject: [PATCH 10/12] Only copy files in classmap path instead of entire directory --- src/Mover.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mover.php b/src/Mover.php index 1f5e7011..add529da 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -67,7 +67,7 @@ public function movePackage(Package $package) } foreach ($autoloader->paths as $path) { - $source_path = $this->workingDir . '/vendor/' . $package->config->name; + $source_path = $this->workingDir . '/vendor/' . $package->config->name . '/' . $path; $finder->files()->in($source_path); From 5320e2deb1100097f5b6eef5f944490c30c550ea Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sat, 2 Feb 2019 11:01:51 +0100 Subject: [PATCH 11/12] Move replaceParentPackage method to Replacer class --- src/Console/Commands/Compose.php | 39 +------------------------------- src/Replacer.php | 37 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index 80ffeb01..743a4fb8 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -50,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->replacePackages($packages); foreach ($packages as $package) { - $this->replaceParentPackage($package, null); + $this->replacer->replaceParentPackage($package, null); } } @@ -108,43 +108,6 @@ public function replacePackage($package) $this->replacer->replacePackage($package); } - protected function replaceParentPackage(Package $package, $parent) - { - if ($parent !== null) { - // Replace everything in parent, based on the dependencies - foreach ($parent->autoloaders as $parentAutoloader) { - foreach ($package->autoloaders as $autoloader) { - if ($parentAutoloader instanceof NamespaceAutoloader) { - $namespace = str_replace('\\', '/', $parentAutoloader->namespace); - $directory = $this->workingDir . $this->config->dep_directory . $namespace . '/'; - - if ($autoloader instanceof NamespaceAutoloader) { - $this->replacer->replaceInDirectory($autoloader, $directory); - } else { - $directory = str_replace($this->workingDir, '', $directory); - $this->replacer->replaceParentClassesInDirectory($directory); - } - } else { - $directory = $this->workingDir . $this->config->classmap_directory . $parent->config->name; - - if ($autoloader instanceof NamespaceAutoloader) { - $this->replacer->replaceInDirectory($autoloader, $directory); - } else { - $directory = str_replace($this->workingDir, '', $directory); - $this->replacer->replaceParentClassesInDirectory($directory); - } - } - } - } - } - - if (! empty($package->dependencies)) { - foreach ($package->dependencies as $dependency) { - $this->replaceParentPackage($dependency, $package); - } - } - } - /** * Loops through all dependencies and their dependencies and so on... * will eventually return a list of all packages required by the full tree. diff --git a/src/Replacer.php b/src/Replacer.php index 634df0ba..3f8d8470 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -139,4 +139,41 @@ public function replaceInDirectory($autoloader, $directory) } } } + + public function replaceParentPackage(Package $package, $parent) + { + if ($parent !== null) { + // Replace everything in parent, based on the dependencies + foreach ($parent->autoloaders as $parentAutoloader) { + foreach ($package->autoloaders as $autoloader) { + if ($parentAutoloader instanceof NamespaceAutoloader) { + $namespace = str_replace('\\', '/', $parentAutoloader->namespace); + $directory = $this->workingDir . $this->config->dep_directory . $namespace . '/'; + + if ($autoloader instanceof NamespaceAutoloader) { + $this->replaceInDirectory($autoloader, $directory); + } else { + $directory = str_replace($this->workingDir, '', $directory); + $this->replaceParentClassesInDirectory($directory); + } + } else { + $directory = $this->workingDir . $this->config->classmap_directory . $parent->config->name; + + if ($autoloader instanceof NamespaceAutoloader) { + $this->replaceInDirectory($autoloader, $directory); + } else { + $directory = str_replace($this->workingDir, '', $directory); + $this->replaceParentClassesInDirectory($directory); + } + } + } + } + } + + if (! empty($package->dependencies)) { + foreach ($package->dependencies as $dependency) { + $this->replaceParentPackage($dependency, $package); + } + } + } } From 546cef66bf24e3a5f35beee5f3beda4dd7514846 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sat, 2 Feb 2019 11:15:47 +0100 Subject: [PATCH 12/12] Reset Finder before finding/moving files to prevent duplicates --- src/Mover.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Mover.php b/src/Mover.php index add529da..28e35f45 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -42,10 +42,10 @@ public function deleteTargetDirs() public function movePackage(Package $package) { - $finder = new Finder(); - foreach ($package->autoloaders as $autoloader) { if ($autoloader instanceof NamespaceAutoloader) { + $finder = new Finder(); + foreach ($autoloader->paths as $path) { $source_path = $this->workingDir . '/vendor/' . $package->config->name . '/' . $path; @@ -56,8 +56,9 @@ public function movePackage(Package $package) } } } elseif ($autoloader instanceof Classmap) { + $finder = new Finder(); + foreach ($autoloader->files as $file) { - $finder = new Finder(); $source_path = $this->workingDir . '/vendor/' . $package->config->name; $finder->files()->name($file)->in($source_path); @@ -66,6 +67,8 @@ public function movePackage(Package $package) } } + $finder = new Finder(); + foreach ($autoloader->paths as $path) { $source_path = $this->workingDir . '/vendor/' . $package->config->name . '/' . $path;