From 26a2e94e78340a00fc4d9ca9b945cb50b65880b7 Mon Sep 17 00:00:00 2001 From: Jonathan Eskew Date: Sat, 27 Jun 2015 14:38:09 -0700 Subject: [PATCH 1/4] Add a line and example to the documentation covering the need to require the autoloader file in order to use autoloaded files --- doc/articles/scripts.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/articles/scripts.md b/doc/articles/scripts.md index dec1fa06d..d7cdd7602 100644 --- a/doc/articles/scripts.md +++ b/doc/articles/scripts.md @@ -82,6 +82,8 @@ For any given event: and command-line executable commands. - PHP classes containing defined callbacks must be autoloadable via Composer's autoload functionality. +- If a defined callback relies on functions defined outside of a class, the callback +must explicitly require the composer autoloader. Script definition example: @@ -121,6 +123,14 @@ class MyClass $composer = $event->getComposer(); // do stuff } + + public static function postAutoloadDump(Event $event) + { + $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir'); + require "$vendorDir/autoload.php"; + + some_function_from_an_autoloaded_file(); + } public static function postPackageInstall(PackageEvent $event) { From 5fe37357a230a0b82c2f8b224bb71a1985e50240 Mon Sep 17 00:00:00 2001 From: Jonathan Eskew Date: Mon, 29 Jun 2015 14:44:28 -0700 Subject: [PATCH 2/4] Reiterate the disclaimer in the context of callbacks --- doc/articles/scripts.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/articles/scripts.md b/doc/articles/scripts.md index d7cdd7602..5ea5204f7 100644 --- a/doc/articles/scripts.md +++ b/doc/articles/scripts.md @@ -82,8 +82,11 @@ For any given event: and command-line executable commands. - PHP classes containing defined callbacks must be autoloadable via Composer's autoload functionality. -- If a defined callback relies on functions defined outside of a class, the callback -must explicitly require the composer autoloader. +- If a defined callback relies on functions defined outside of a class, the +callback must explicitly require the composer autoloader. If used in a +context where`vendor/autoload.php` might not yet exist (such as during a +`pre-install` or `pre-update` command), the callback should explicitly require +whatever files within your root package it needs to execute successfully. Script definition example: From 56ffa407f15a719577f72aeee991152c6a6658a3 Mon Sep 17 00:00:00 2001 From: Jonathan Eskew Date: Wed, 1 Jul 2015 09:15:22 -0700 Subject: [PATCH 3/4] House style requires concatenation in examples --- doc/articles/scripts.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/doc/articles/scripts.md b/doc/articles/scripts.md index 5ea5204f7..db983a233 100644 --- a/doc/articles/scripts.md +++ b/doc/articles/scripts.md @@ -83,10 +83,8 @@ and command-line executable commands. - PHP classes containing defined callbacks must be autoloadable via Composer's autoload functionality. - If a defined callback relies on functions defined outside of a class, the -callback must explicitly require the composer autoloader. If used in a -context where`vendor/autoload.php` might not yet exist (such as during a -`pre-install` or `pre-update` command), the callback should explicitly require -whatever files within your root package it needs to execute successfully. +callback itself is responsible for loading the appropriate files, as no files +are autoloaded during Composer commands. Script definition example: @@ -130,7 +128,7 @@ class MyClass public static function postAutoloadDump(Event $event) { $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir'); - require "$vendorDir/autoload.php"; + require $vendorDir . '/autoload.php'; some_function_from_an_autoloaded_file(); } From 4e3fbeabfcdfc03d1bb2fd4996ad54c3b08d4d09 Mon Sep 17 00:00:00 2001 From: Jonathan Eskew Date: Wed, 1 Jul 2015 09:38:14 -0700 Subject: [PATCH 4/4] "No files" was inaccurate; amend to specify "no files except" --- doc/articles/scripts.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/articles/scripts.md b/doc/articles/scripts.md index db983a233..8d9eaa17e 100644 --- a/doc/articles/scripts.md +++ b/doc/articles/scripts.md @@ -84,7 +84,8 @@ and command-line executable commands. autoload functionality. - If a defined callback relies on functions defined outside of a class, the callback itself is responsible for loading the appropriate files, as no files -are autoloaded during Composer commands. +beyond those required for `psr-0`, `psr-4`, and `classmap` autoloading are +loaded during Composer commands. Script definition example: @@ -99,6 +100,9 @@ Script definition example: "MyVendor\\MyClass::warmCache", "phpunit -c app/" ], + "post-autoload-dump": [ + "MyVendor\\MyClass::postAutoloadDump" + ] "post-create-project-cmd" : [ "php -r \"copy('config/local-example.php', 'config/local.php');\"" ]