ソースを参照

Merge branch 'feature/document-need-to-require-autoloader-in-script-hooks' of git://github.com/jeskew/composer into jeskew-feature/document-need-to-require-autoloader-in-script-hooks

* 'feature/document-need-to-require-autoloader-in-script-hooks' of git://github.com/jeskew/composer:
  "No files" was inaccurate; amend to specify "no files except"
  House style requires concatenation in examples
  Reiterate the disclaimer in the context of callbacks
  Add a line and example to the documentation covering the need to require the autoloader file in order to use autoloaded files
Rob Bast 9 年 前
コミット
91336a8312
1 ファイル変更16 行追加1 行削除
  1. 16 1
      doc/articles/scripts.md

+ 16 - 1
doc/articles/scripts.md

@@ -82,6 +82,10 @@ For any given event:
 and command-line executable commands.
 - PHP classes containing defined callbacks must be autoloadable via Composer's
 autoload functionality.
+- Callbacks can only autoload classes from psr-0, psr-4 and classmap
+definitions. If a defined callback relies on functions defined outside of a
+class, the callback itself is responsible for loading the file containing these
+functions.
 
 Script definition example:
 
@@ -96,7 +100,10 @@ Script definition example:
             "MyVendor\\MyClass::warmCache",
             "phpunit -c app/"
         ],
-        "post-create-project-cmd" : [
+        "post-autoload-dump": [
+            "MyVendor\\MyClass::postAutoloadDump"
+        ],
+        "post-create-project-cmd": [
             "php -r \"copy('config/local-example.php', 'config/local.php');\""
         ]
     }
@@ -122,6 +129,14 @@ class MyClass
         // 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)
     {
         $installedPackage = $event->getOperation()->getPackage();