From 8e20aa3febd7d097d57a09d5527c3bd030c36195 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 9 Apr 2020 19:52:47 -0700 Subject: [PATCH 01/19] Add classmap output --- README.md | 10 +++++++- src/Console/Commands/Compose.php | 42 ++++++++++++++++++++++++++++++++ src/Replace/ClassmapReplacer.php | 14 +++++++---- src/Replace/Replacer.php | 2 +- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c10e936b..793f605b 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,11 @@ Mozart requires little configuration. All you need to do is tell it where the bu "packages": [ "pimple/pimple" ] - } + }, + "classmap_output": { + "filename": "src/autoload_classmap.php", + "relative_path": "/src" + }, }, ``` @@ -41,6 +45,10 @@ The following configuration values are required: **Important:** Since Mozart automatically processes the full dependency tree of the packages you specify, you **need to specify all these configuration options**, because you can't reliably determine what kind of autoloaders are being used in the full dependency tree. A package way down the tree might suddenly use a classmap autoloader for example. Make sure you also include the namespace directory and classmap directory in your own autoloader, so they are always loaded. + The following configuration is optional: + + - `classmap_output` defines the `filename` to write the classmap autoloader to (relative to the directory where `mozart compose` is invoked from) and the `relative_path` to remove from the beginning of each file path. + After Composer has loaded the packages as defined in your `composer.json` file, you can now run `mozart compose` and Mozart will bundle your packages according to the above configuration. It is recommended to dump the autoloader after Mozart has finished running, in case there are new classes or namespaces generated that aren't included in the autoloader yet. ## Scripts diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index a7769eb4..c8f6ff33 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -53,6 +53,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->replacer->replaceParentPackage($package, null); } + $this->generateClassmapAutoloader(); + return 0; } @@ -141,4 +143,44 @@ private function findPackages($slugs) return $packages; } + + + /** + * Write a classmap to file. + */ + private function generateClassmapAutoloader() { + + if (!isset($this->config->classmap_output)) { + return; + } + + $classmap = $this->replacer->classmap; + + if( !isset($this->config->classmap_output->filename)) { + return; + } + $output_filename = $this->config->classmap_output->filename; + + $relative_path = isset( $this->config->classmap_output->relative_path ) ? $this->config->classmap_output->relative_path : null; + + $output = " $filepath) { + + if ( !is_null($relative_path) && substr($filepath, 0, strlen($relative_path)) == $relative_path) { + $filepath = substr($filepath, strlen($relative_path)); + } + + $output .= " '{$class}' => '{$filepath}',\n"; + } + + $output .= ");"; + + file_put_contents( $output_filename, $output ); + + } } diff --git a/src/Replace/ClassmapReplacer.php b/src/Replace/ClassmapReplacer.php index fddec0a3..3c379498 100644 --- a/src/Replace/ClassmapReplacer.php +++ b/src/Replace/ClassmapReplacer.php @@ -5,26 +5,30 @@ class ClassmapReplacer extends BaseReplacer { /** @var array */ - public $replacedClasses = []; + public $replacedClasses = []; + + /** @var array */ + public $classmap = []; /** @var string */ public $classmap_prefix; - public function replace($contents) + public function replace($contents, $file) { return preg_replace_callback( '/(?:[abstract]*class |interface )([a-zA-Z\_]+)(?:[ \n]*{| extends| implements)/U', - function ($matches) { + function ($matches) use ($file) { $replace = $this->classmap_prefix . $matches[1]; - $this->saveReplacedClass($matches[1], $replace); + $this->saveReplacedClass($matches[1], $replace, $file); return str_replace($matches[1], $replace, $matches[0]); }, $contents ); } - public function saveReplacedClass($classname, $replacedName) + public function saveReplacedClass($classname, $replacedName, $file) { $this->replacedClasses[ $classname ] = $replacedName; + $this->classmap[ $replacedName ] = $file; } } diff --git a/src/Replace/Replacer.php b/src/Replace/Replacer.php index c803ff65..7453970e 100644 --- a/src/Replace/Replacer.php +++ b/src/Replace/Replacer.php @@ -5,5 +5,5 @@ interface Replacer { public function setAutoloader($autoloader); - public function replace($contents); + public function replace($contents, $file); } From 0ce198d379a965ec3ab05fa8bce2a806a5470062 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 9 Apr 2020 20:15:09 -0700 Subject: [PATCH 02/19] Missed some copying and pasting --- src/Replacer.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Replacer.php b/src/Replacer.php index 3f8d8470..465a2e45 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -25,7 +25,10 @@ class Replacer /** @var array */ protected $replacedClasses = []; - /** @var Filesystem */ + /** @var array */ + public $classmap = []; + + /** @var Filesystem */ protected $filesystem; public function __construct($workingDir, $config) @@ -66,6 +69,7 @@ public function replaceInFile($targetFile, $autoloader) if ($replacer instanceof ClassmapReplacer) { $this->replacedClasses = array_merge($this->replacedClasses, $replacer->replacedClasses); + $this->classmap = array_merge($this->classmap, $replacer->classmap); } $this->filesystem->put($targetFile, $contents); From 6e4b3d4f5f4ab201c26ae1edf881851f349d28b7 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 21 May 2020 22:17:59 -0700 Subject: [PATCH 03/19] Missed a merge conflict That never should've happened! --- src/Console/Commands/Compose.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index f5f9b15c..902ee1a4 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -55,13 +55,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->replacer->replaceParentPackage($package, null); } -<<<<<<< HEAD $this->replacer->replaceParentClassesInDirectory( $this->config->classmap_directory ); - -======= + $this->generateClassmapAutoloader(); ->>>>>>> generate-classmap return 0; } From 6883b58abfc23a28203fee4ab9247bbc3630b1a1 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 21 May 2020 22:23:38 -0700 Subject: [PATCH 04/19] Update NamespaceReplacer.php --- src/Replace/NamespaceReplacer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Replace/NamespaceReplacer.php b/src/Replace/NamespaceReplacer.php index 7bdc8b5c..4f92f969 100644 --- a/src/Replace/NamespaceReplacer.php +++ b/src/Replace/NamespaceReplacer.php @@ -7,7 +7,7 @@ class NamespaceReplacer extends BaseReplacer /** @var string */ public $dep_namespace = ''; - public function replace($contents) + public function replace($contents, $file) { $searchNamespace = $this->autoloader->getSearchNamespace(); return preg_replace_callback( From ad44c39a7f683fb936eb27daf700060892f4755b Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 21 May 2020 22:28:17 -0700 Subject: [PATCH 05/19] Update Replacer.php --- src/Replacer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Replacer.php b/src/Replacer.php index 465a2e45..52138239 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -65,7 +65,7 @@ public function replaceInFile($targetFile, $autoloader) } $replacer->setAutoloader($autoloader); - $contents = $replacer->replace($contents); + $contents = $replacer->replace($contents, $targetFile); if ($replacer instanceof ClassmapReplacer) { $this->replacedClasses = array_merge($this->replacedClasses, $replacer->replacedClasses); From 39520428407ed7429016b53bf2f957f7e683feae Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 21 May 2020 23:11:30 -0700 Subject: [PATCH 06/19] classmap_output example in readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 2e8df127..b57a0e79 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ Mozart requires little configuration. All you need to do is tell it where the bu "dep_directory": "/src/Dependencies/", "classmap_directory": "/classes/dependencies/", "classmap_prefix": "CJTP_", + "classmap_output": { + "filename": "src/autoload_classmap.php", + "relative_path": "/src" + }, "packages": [ "pimple/pimple" ], From 8485089b4ef178ba7cb2bcd7bd13576837a52dd8 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 21 May 2020 23:12:34 -0700 Subject: [PATCH 07/19] readdme indentation --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b57a0e79..c15cad89 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,9 @@ Mozart requires little configuration. All you need to do is tell it where the bu "classmap_directory": "/classes/dependencies/", "classmap_prefix": "CJTP_", "classmap_output": { - "filename": "src/autoload_classmap.php", - "relative_path": "/src" - }, + "filename": "src/autoload_classmap.php", + "relative_path": "/src" + }, "packages": [ "pimple/pimple" ], @@ -39,10 +39,10 @@ Mozart requires little configuration. All you need to do is tell it where the bu } } "delete_vendor_directories": true - "classmap_output": { - "filename": "src/autoload_classmap.php", + "classmap_output": { + "filename": "src/autoload_classmap.php", "relative_path": "/src" - } + } } ``` From cee820c9cadf25884a1894bc4f194226791e567e Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sun, 20 Sep 2020 10:27:49 -0700 Subject: [PATCH 08/19] GitHub Annotations for PHPCS --- .github/workflows/main.yml | 2 +- composer.json | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fae41817..3c02823b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -41,4 +41,4 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-suggest --no-progress - name: Run validate - run: composer run lint + run: vendor/bin/phpcs --report=GitHubActionsAnnotations diff --git a/composer.json b/composer.json index a925709f..22f88bd3 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,15 @@ }, "require-dev": { "phpunit/phpunit": "^8.5", - "squizlabs/php_codesniffer": "^3.5" + "squizlabs/php_codesniffer": "dev-master#9cce685", + "cweagans/composer-patches": "^1.0" + }, + "extra": { + "patches": { + "squizlabs/php_codesniffer": { + "Add GitHub Actions Annotations report type": "https://github.com/squizlabs/PHP_CodeSniffer/pull/2918.patch" + } + } }, "scripts": { "lint": [ From e9e9e6a59188b273ed9c08911026e37e31ea3d87 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sun, 20 Sep 2020 10:29:18 -0700 Subject: [PATCH 09/19] Run PHPCBF first so only important errors are noted --- .github/workflows/main.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3c02823b..7bec4c89 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -40,5 +40,10 @@ jobs: composer --version - name: Install dependencies run: composer install --prefer-dist --no-suggest --no-progress - - name: Run validate + + - name: PHPCBF + continue-on-error: true + run: vendor/bin/phpcbf + + - name: Run PHPCS run: vendor/bin/phpcs --report=GitHubActionsAnnotations From 91290514ca042f77f9b53880767bb8f75ce51e14 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sun, 20 Sep 2020 10:30:36 -0700 Subject: [PATCH 10/19] Fix and commit PHPCBF changes on master --- .github/workflows/phpcbf.yml | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/phpcbf.yml diff --git a/.github/workflows/phpcbf.yml b/.github/workflows/phpcbf.yml new file mode 100644 index 00000000..c4eaa26f --- /dev/null +++ b/.github/workflows/phpcbf.yml @@ -0,0 +1,36 @@ +name: Run PHP CodeSniffer + +# Run PHPCBF to fix changes then PHPCS with GitHubActionsAnnotations report +# https://github.com/squizlabs/PHP_CodeSniffer/pull/2918 +# +# NB: Pull requests from forks do not have access to repository secrets so cannot commit changes. +# +# @author BrianHenryIE + +on: + push: + branches: + - master + +jobs: + php-codesniffer: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Run Composer install + uses: php-actions/composer@v1 + + - name: Run PHPCBF to fix what it can + continue-on-error: true + run: vendor/bin/phpcbf + + - name: Commit PHPCBF changes + uses: stefanzweifel/git-auto-commit-action@v4.1.1 + with: + commit_message: "PHPCBF" + + - name: Run PHPCS to add annotations to the commit + continue-on-error: true + run: vendor/bin/phpcs --report=GitHubActionsAnnotations From d81cf26be1a337fa8af4fdd1dba50e4bc12fa10e Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 18:13:13 -0800 Subject: [PATCH 11/19] Use cs2pr for annotations https://github.com/staabm/annotate-pull-request-from-checkstyle --- .github/workflows/main.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7bec4c89..4aad540b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -33,6 +33,7 @@ jobs: uses: shivammathur/setup-php@1.3.7 with: php-version: '7.4' + tools: composer, cs2pr - name: Debugging run: | php --version @@ -46,4 +47,4 @@ jobs: run: vendor/bin/phpcbf - name: Run PHPCS - run: vendor/bin/phpcs --report=GitHubActionsAnnotations + run: vendor/bin/phpcs --report=checkstyle | cs2pr From 3819870d2a4982c9d50c88a5ef64ab9c95bcc803 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 18:17:15 -0800 Subject: [PATCH 12/19] Revert composer.json --- composer.json | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 22f88bd3..3beec772 100644 --- a/composer.json +++ b/composer.json @@ -24,15 +24,8 @@ }, "require-dev": { "phpunit/phpunit": "^8.5", - "squizlabs/php_codesniffer": "dev-master#9cce685", - "cweagans/composer-patches": "^1.0" - }, - "extra": { - "patches": { - "squizlabs/php_codesniffer": { - "Add GitHub Actions Annotations report type": "https://github.com/squizlabs/PHP_CodeSniffer/pull/2918.patch" - } - } + "squizlabs/php_codesniffer": "^3.5", + "mheap/phpunit-github-actions-printer": "^1.4" }, "scripts": { "lint": [ From 994bfa8a0f4e95489baf270d6e1f1d4a160e2899 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 18:23:53 -0800 Subject: [PATCH 13/19] Update phpcbf.yml --- .github/workflows/phpcbf.yml | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/.github/workflows/phpcbf.yml b/.github/workflows/phpcbf.yml index c4eaa26f..67dc08c0 100644 --- a/.github/workflows/phpcbf.yml +++ b/.github/workflows/phpcbf.yml @@ -1,7 +1,6 @@ name: Run PHP CodeSniffer -# Run PHPCBF to fix changes then PHPCS with GitHubActionsAnnotations report -# https://github.com/squizlabs/PHP_CodeSniffer/pull/2918 +# Run PHPCBF to fix changes then commit the change and annotates with remaining PHPCS errors. # # NB: Pull requests from forks do not have access to repository secrets so cannot commit changes. # @@ -16,13 +15,23 @@ jobs: php-codesniffer: runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v2 + - uses: actions/checkout@v1 + - name: Install PHP + uses: shivammathur/setup-php@1.3.7 + with: + php-version: '7.4' + tools: composer, cs2pr + + - name: Debugging + run: | + php --version + php -m + composer --version - - name: Run Composer install - uses: php-actions/composer@v1 + - name: Install dependencies + run: composer install --prefer-dist --no-suggest --no-progress - - name: Run PHPCBF to fix what it can + - name: PHPCBF continue-on-error: true run: vendor/bin/phpcbf @@ -33,4 +42,4 @@ jobs: - name: Run PHPCS to add annotations to the commit continue-on-error: true - run: vendor/bin/phpcs --report=GitHubActionsAnnotations + run: vendor/bin/phpcs --report=checkstyle | cs2pr From d9b95cb713849ae260dc8ff6f40b5f9574545d1f Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 20:10:03 -0800 Subject: [PATCH 14/19] Remove broken git-auto-commit-action --- .github/workflows/phpcbf.yml | 45 ------------------------------------ 1 file changed, 45 deletions(-) delete mode 100644 .github/workflows/phpcbf.yml diff --git a/.github/workflows/phpcbf.yml b/.github/workflows/phpcbf.yml deleted file mode 100644 index 67dc08c0..00000000 --- a/.github/workflows/phpcbf.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: Run PHP CodeSniffer - -# Run PHPCBF to fix changes then commit the change and annotates with remaining PHPCS errors. -# -# NB: Pull requests from forks do not have access to repository secrets so cannot commit changes. -# -# @author BrianHenryIE - -on: - push: - branches: - - master - -jobs: - php-codesniffer: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - name: Install PHP - uses: shivammathur/setup-php@1.3.7 - with: - php-version: '7.4' - tools: composer, cs2pr - - - name: Debugging - run: | - php --version - php -m - composer --version - - - name: Install dependencies - run: composer install --prefer-dist --no-suggest --no-progress - - - name: PHPCBF - continue-on-error: true - run: vendor/bin/phpcbf - - - name: Commit PHPCBF changes - uses: stefanzweifel/git-auto-commit-action@v4.1.1 - with: - commit_message: "PHPCBF" - - - name: Run PHPCS to add annotations to the commit - continue-on-error: true - run: vendor/bin/phpcs --report=checkstyle | cs2pr From 6fe6e049a6d1402f4c23fc3084465a640c7485e7 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 20:10:43 -0800 Subject: [PATCH 15/19] Tested --- .github/workflows/main.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4aad540b..446f8e77 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -42,9 +42,5 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-suggest --no-progress - - name: PHPCBF - continue-on-error: true - run: vendor/bin/phpcbf - - name: Run PHPCS - run: vendor/bin/phpcs --report=checkstyle | cs2pr + run: vendor/bin/phpcs -q -n --report=checkstyle | cs2pr From 56afd9394e29448fa60819ca6560735af7a3c7a6 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 20:14:37 -0800 Subject: [PATCH 16/19] Update setup-php to support tools --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 446f8e77..9bcfc490 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -30,7 +30,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Install PHP - uses: shivammathur/setup-php@1.3.7 + uses: shivammathur/setup-php@v2 with: php-version: '7.4' tools: composer, cs2pr From 09a7227bdb9fe0510362b763ff6290c5253c0a83 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 20:25:20 -0800 Subject: [PATCH 17/19] Update main.yml --- .github/workflows/main.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ee459c67..82882b4a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,17 +28,21 @@ jobs: runs-on: ubuntu-latest name: Lint project files steps: + - uses: actions/checkout@v1 + - name: Install PHP uses: shivammathur/setup-php@v2 with: php-version: '7.4' tools: composer, cs2pr + - name: Debugging run: | php --version php -m composer --version + - name: Install dependencies run: composer install --prefer-dist --no-suggest --no-progress From 2e59ef18fbb8f3f5ae0a070f98b25e091a93f30c Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 20:26:11 -0800 Subject: [PATCH 18/19] Revert "Update main.yml" This reverts commit 09a7227bdb9fe0510362b763ff6290c5253c0a83. --- .github/workflows/main.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 82882b4a..ee459c67 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,21 +28,17 @@ jobs: runs-on: ubuntu-latest name: Lint project files steps: - - uses: actions/checkout@v1 - - name: Install PHP uses: shivammathur/setup-php@v2 with: php-version: '7.4' tools: composer, cs2pr - - name: Debugging run: | php --version php -m composer --version - - name: Install dependencies run: composer install --prefer-dist --no-suggest --no-progress From cd631e04d8e7843c7fe45f1958273255b54bf364 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 24 Nov 2020 20:26:53 -0800 Subject: [PATCH 19/19] Revert bad merge --- .github/workflows/main.yml | 2 +- README.md | 8 - src/Console/Commands/Compose.php | 337 +++++++++++++++--------------- src/Mover.php | 22 +- src/Replace/ClassmapReplacer.php | 14 +- src/Replace/NamespaceReplacer.php | 2 +- src/Replace/Replacer.php | 2 +- src/Replacer.php | 8 +- 8 files changed, 178 insertions(+), 217 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ee459c67..9bcfc490 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,7 +23,7 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-suggest --no-progress - name: Run tests - run: vendor/bin/phpunit --printer mheap\\GithubActionsReporter\\Printer + run: composer run test lint: runs-on: ubuntu-latest name: Lint project files diff --git a/README.md b/README.md index f580b3e0..813edcff 100644 --- a/README.md +++ b/README.md @@ -24,16 +24,9 @@ Mozart requires little configuration. All you need to do is tell it where the bu "dep_directory": "/src/Dependencies/", "classmap_directory": "/classes/dependencies/", "classmap_prefix": "CJTP_", - "classmap_output": { - "filename": "src/autoload_classmap.php", - "relative_path": "/src" - }, "packages": [ "pimple/pimple" ], - "exclude_packages": [ - "psr/container" - ], "override_autoload": { "google/apiclient": { "classmap": [ @@ -59,7 +52,6 @@ The following configuration is optional: - `delete_vendor_directories` is a boolean flag to indicate if the packages' vendor directories should be deleted after being processed. _default: true_. - `packages` is an optional array that defines the packages to be processed by Mozart. The array requires the slugs of packages in the same format as provided in your `composer.json`. Mozart will automatically rewrite dependencies of these packages as well. You don't need to add dependencies of these packages to the list. If this field is absent, all packages listed under composer require will be included. -- `exclude_packages` is an optional array that defines the packages to be excluded from the processing performed by Mozart. This is useful if some of the packages in the `packages` array define dependent packages whose namespaces you want to keep unchanged. The array requires the slugs of the packages, as in the case of the `packages` array. - `override_autoload` a dictionary, keyed with the package names, of autoload settings to replace those in the original packages' `composer.json` `autoload` property. After Composer has loaded the packages as defined in your `composer.json` file, you can now run `mozart compose` and Mozart will bundle your packages according to the above configuration. It is recommended to dump the autoloader after Mozart has finished running, in case there are new classes or namespaces generated that aren't included in the autoloader yet. diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index 40a9cf6e..7bbee156 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -13,176 +13,169 @@ 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'); - $this->setDescription('Composes all dependencies as a package inside a WordPress plugin.'); - $this->setHelp(''); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $workingDir = getcwd(); - $this->workingDir = $workingDir; - - $composerFile = $workingDir . DIRECTORY_SEPARATOR. 'composer.json'; - if (!file_exists($composerFile)) { - $output->write('No composer.json found at current directory: ' . $workingDir); - return 1; - } - - $composer = json_decode(file_get_contents($composerFile)); - // If the json was malformed. - if (!is_object($composer)) { - $output->write('Unable to parse composer.json read at: ' . $workingDir); - return 1; - } - - // if `extra` is missing or not an object or if it does not have a `mozart` key which is an object. - if (!isset($composer->extra) || !is_object($composer->extra) - || !isset($composer->extra->mozart) || !is_object($composer->extra->mozart)) { - $output->write('Mozart config not readable in composer.json at extra->mozart'); - return 1; - } - $config = $composer->extra->mozart; - - $config->dep_namespace = preg_replace("/\\\{2,}$/", "\\", "$config->dep_namespace\\"); - - $this->config = $config; - - $require = array(); - if (isset($config->packages) && is_array($config->packages)) { - $require = $config->packages; - } elseif (isset($composer->require) && is_object($composer->require)) { - $require = array_keys(get_object_vars($composer->require)); - } - - $packagesByName = $this->findPackages($require); - $excludedPackagesNames = isset($config->excluded_packages) ? $config->excluded_packages : []; - $packagesToMoveByName = array_diff_key($packagesByName, array_flip($excludedPackagesNames)); - $packages = array_values($packagesToMoveByName); - - foreach ($packages as $package) { - $package->dependencies = array_diff_key($package->dependencies, array_flip($excludedPackagesNames)); - } - - $this->mover = new Mover($workingDir, $config); - $this->replacer = new Replacer($workingDir, $config); - - $this->mover->deleteTargetDirs($packages); - $this->movePackages($packages); - $this->replacePackages($packages); - - foreach ($packages as $package) { - $this->replacer->replaceParentPackage($package, null); - } - - $this->replacer->replaceParentClassesInDirectory($this->config->classmap_directory); - - return 0; - } - - /** - * @param $workingDir - * @param $config - * @param array $packages - */ - protected function movePackages($packages) - { - foreach ($packages as $package) { - $this->movePackage($package); - } - - $this->mover->deleteEmptyDirs(); - } - - /** - * @param $workingDir - * @param $config - * @param array $packages - */ - protected function replacePackages($packages) - { - foreach ($packages as $package) { - $this->replacePackage($package); - } - } - - /** - * Move all the packages over, one by one, starting on the deepest level of dependencies. - */ - public function movePackage($package) - { - if (! empty($package->dependencies)) { - foreach ($package->dependencies as $dependency) { - $this->movePackage($dependency); - } - } - - $this->mover->movePackage($package); - } - - /** - * Replace contents of all the packages, one by one, starting on the deepest level of dependencies. - */ - public function replacePackage($package) - { - if (! empty($package->dependencies)) { - foreach ($package->dependencies as $dependency) { - $this->replacePackage($dependency); - } - } - - $this->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. - */ - private function findPackages($slugs) - { - $packages = []; - - foreach ($slugs as $package_slug) { - $packageDir = $this->workingDir . DIRECTORY_SEPARATOR . 'vendor' - . DIRECTORY_SEPARATOR . $package_slug . DIRECTORY_SEPARATOR; - - if (! is_dir($packageDir)) { - continue; - } - - $autoloaders = null; - if (isset($this->config->override_autoload) && isset($this->config->override_autoload->$package_slug)) { - $autoloaders = $this->config->override_autoload->$package_slug; - } - - $package = new Package($packageDir, $autoloaders); - $package->findAutoloaders(); - - $config = json_decode(file_get_contents($packageDir . 'composer.json')); - - $dependencies = []; - if (isset($config->require)) { - $dependencies = array_keys((array)$config->require); - } - - $package->dependencies = $this->findPackages($dependencies); - $packages[$package_slug] = $package; - } - - return $packages; - } + /** @var Mover */ + private $mover; + + /** @var Replacer */ + private $replacer; + + /** @var string */ + private $workingDir; + + /** @var */ + private $config; + + protected function configure() + { + $this->setName('compose'); + $this->setDescription('Composes all dependencies as a package inside a WordPress plugin.'); + $this->setHelp(''); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $workingDir = getcwd(); + $this->workingDir = $workingDir; + + $composerFile = $workingDir . DIRECTORY_SEPARATOR. 'composer.json'; + if (!file_exists($composerFile)) { + $output->write('No composer.json found at current directory: ' . $workingDir); + return 1; + } + + $composer = json_decode(file_get_contents($composerFile)); + // If the json was malformed. + if (!is_object($composer)) { + $output->write('Unable to parse composer.json read at: ' . $workingDir); + return 1; + } + + // if `extra` is missing or not an object or if it does not have a `mozart` key which is an object. + if (!isset($composer->extra) || !is_object($composer->extra) + || !isset($composer->extra->mozart) || !is_object($composer->extra->mozart)) { + $output->write('Mozart config not readable in composer.json at extra->mozart'); + return 1; + } + $config = $composer->extra->mozart; + + $config->dep_namespace = preg_replace("/\\\{2,}$/", "\\", "$config->dep_namespace\\"); + + $this->config = $config; + + $require = array(); + if (isset($config->packages) && is_array($config->packages)) { + $require = $config->packages; + } elseif (isset($composer->require) && is_object($composer->require)) { + $require = array_keys(get_object_vars($composer->require)); + } + + $packages = $this->findPackages($require); + + $this->mover = new Mover($workingDir, $config); + $this->replacer = new Replacer($workingDir, $config); + + $this->mover->deleteTargetDirs($packages); + $this->movePackages($packages); + $this->replacePackages($packages); + + foreach ($packages as $package) { + $this->replacer->replaceParentPackage($package, null); + } + + $this->replacer->replaceParentClassesInDirectory($this->config->classmap_directory); + + return 0; + } + + /** + * @param $workingDir + * @param $config + * @param array $packages + */ + protected function movePackages($packages) + { + foreach ($packages as $package) { + $this->movePackage($package); + } + + $this->mover->deleteEmptyDirs(); + } + + /** + * @param $workingDir + * @param $config + * @param array $packages + */ + protected function replacePackages($packages) + { + foreach ($packages as $package) { + $this->replacePackage($package); + } + } + + /** + * Move all the packages over, one by one, starting on the deepest level of dependencies. + */ + public function movePackage($package) + { + if (! empty($package->dependencies)) { + foreach ($package->dependencies as $dependency) { + $this->movePackage($dependency); + } + } + + $this->mover->movePackage($package); + } + + /** + * Replace contents of all the packages, one by one, starting on the deepest level of dependencies. + */ + public function replacePackage($package) + { + if (! empty($package->dependencies)) { + foreach ($package->dependencies as $dependency) { + $this->replacePackage($dependency); + } + } + + $this->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. + */ + private function findPackages($slugs) + { + $packages = []; + + foreach ($slugs as $package_slug) { + $packageDir = $this->workingDir . DIRECTORY_SEPARATOR . 'vendor' + . DIRECTORY_SEPARATOR . $package_slug . DIRECTORY_SEPARATOR; + + if (! is_dir($packageDir)) { + continue; + } + + $autoloaders = null; + if (isset($this->config->override_autoload) && isset($this->config->override_autoload->$package_slug)) { + $autoloaders = $this->config->override_autoload->$package_slug; + } + + $package = new Package($packageDir, $autoloaders); + $package->findAutoloaders(); + + $config = json_decode(file_get_contents($packageDir . 'composer.json')); + + $dependencies = []; + if (isset($config->require)) { + $dependencies = array_keys((array)$config->require); + } + + $package->dependencies = $this->findPackages($dependencies); + $packages[] = $package; + } + + return $packages; + } } diff --git a/src/Mover.php b/src/Mover.php index 61cc2379..a0f8e2b1 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -147,9 +147,7 @@ public function movePackage(Package $package) } } - if (!in_array($package->config->name, $this->movedPackages)) { - $this->movedPackages[] = $package->config->name; - } + $this->movedPackages[] = $package->config->name; } if (!isset($this->config->delete_vendor_directories) || $this->config->delete_vendor_directories === true) { @@ -202,25 +200,11 @@ public function moveFile(Package $package, $autoloader, $file, $path = '') protected function deletePackageVendorDirectories() { foreach ($this->movedPackages as $movedPackage) { - $packageDir = 'vendor' . DIRECTORY_SEPARATOR . $movedPackage; - if (!is_dir($packageDir) || is_link($packageDir)) { + $packageDir = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $movedPackage; + if (is_link($packageDir)) { continue; } - $this->filesystem->deleteDir($packageDir); - - //Delete parent directory too if it became empty - //(because that package was the only one from that vendor) - $parentDir = dirname($packageDir); - if ($this->dirIsEmpty($parentDir)) { - $this->filesystem->deleteDir($parentDir); - } } } - - private function dirIsEmpty($dir) - { - $di = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS); - return iterator_count($di) === 0; - } } diff --git a/src/Replace/ClassmapReplacer.php b/src/Replace/ClassmapReplacer.php index 3c379498..fddec0a3 100644 --- a/src/Replace/ClassmapReplacer.php +++ b/src/Replace/ClassmapReplacer.php @@ -5,30 +5,26 @@ class ClassmapReplacer extends BaseReplacer { /** @var array */ - public $replacedClasses = []; - - /** @var array */ - public $classmap = []; + public $replacedClasses = []; /** @var string */ public $classmap_prefix; - public function replace($contents, $file) + public function replace($contents) { return preg_replace_callback( '/(?:[abstract]*class |interface )([a-zA-Z\_]+)(?:[ \n]*{| extends| implements)/U', - function ($matches) use ($file) { + function ($matches) { $replace = $this->classmap_prefix . $matches[1]; - $this->saveReplacedClass($matches[1], $replace, $file); + $this->saveReplacedClass($matches[1], $replace); return str_replace($matches[1], $replace, $matches[0]); }, $contents ); } - public function saveReplacedClass($classname, $replacedName, $file) + public function saveReplacedClass($classname, $replacedName) { $this->replacedClasses[ $classname ] = $replacedName; - $this->classmap[ $replacedName ] = $file; } } diff --git a/src/Replace/NamespaceReplacer.php b/src/Replace/NamespaceReplacer.php index 691d8cc1..be7715f9 100644 --- a/src/Replace/NamespaceReplacer.php +++ b/src/Replace/NamespaceReplacer.php @@ -7,7 +7,7 @@ class NamespaceReplacer extends BaseReplacer /** @var string */ public $dep_namespace = ''; - public function replace($contents, $file) + public function replace($contents) { $searchNamespace = preg_quote($this->autoloader->getSearchNamespace(), '/'); $dependencyNamespace = preg_quote($this->dep_namespace, '/'); diff --git a/src/Replace/Replacer.php b/src/Replace/Replacer.php index 7453970e..c803ff65 100644 --- a/src/Replace/Replacer.php +++ b/src/Replace/Replacer.php @@ -5,5 +5,5 @@ interface Replacer { public function setAutoloader($autoloader); - public function replace($contents, $file); + public function replace($contents); } diff --git a/src/Replacer.php b/src/Replacer.php index 2246b358..d83ba482 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -25,10 +25,7 @@ class Replacer /** @var array */ protected $replacedClasses = []; - /** @var array */ - public $classmap = []; - - /** @var Filesystem */ + /** @var Filesystem */ protected $filesystem; public function __construct($workingDir, $config) @@ -65,11 +62,10 @@ public function replaceInFile($targetFile, $autoloader) } $replacer->setAutoloader($autoloader); - $contents = $replacer->replace($contents, $targetFile); + $contents = $replacer->replace($contents); if ($replacer instanceof ClassmapReplacer) { $this->replacedClasses = array_merge($this->replacedClasses, $replacer->replacedClasses); - $this->classmap = array_merge($this->classmap, $replacer->classmap); } $this->filesystem->put($targetFile, $contents);