メモです。
composerでCakePHPといくつかのプラグインをインストールしたらbake modelの、Test/Fixtureの出力の際にエラーが出てしまいました。
composer.jsonはこんな感じで、
{ "name": "hoge-app", "repositories": [ { "type": "pear", "url": "http://pear.cakephp.org" } ], "require": { "pear-cakephp/cakephp": ">=2.4.4", "cakephp/debug_kit": "2.2.*", "slywalker/boost_cake": "*", "cakedc/utils": "*", "cakedc/users": "*", "cakedc/search": "*" }, "config": { "vendor-dir": "Vendor/" } }
CakePHPのインストール、アプリケーションのbake、CAKE_CORE_INCLUDE_PATH
の修正、Config/bootstrap.php
に
// composerのautoloadを読み込み require APP . '/Vendor/autoload.php'; // CakePHPのオートローダーをいったん削除し、composerより先に評価されるように先頭に追加する // https://github.com/composer/composer/commit/c80cb76b9b5082ecc3e5b53b1050f76bb27b127b を参照 spl_autoload_unregister(array('App', 'load')); spl_autoload_register(array('App', 'load'), true, true);
上記を追加など、composerでCakePHPをインストールする際に必要な手順は満たしています。
bake modelでエラー
bake modelすると以下のようなエラーが出ます。
Wrote `/var/www/html/hoge/app/Test/Fixture/BrandFixture.php` Bake is detecting possible fixtures... PHP Fatal error: Cannot redeclare class AppModel in /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Test/Case/Model/models.php on line 57 PHP Stack trace: PHP 1. {main}() /var/www/html/hoge/hoge/Console/cake.php:0 PHP 2. ShellDispatcher::run() /var/www/html/hoge/app/Console/cake.php:35 PHP 3. ShellDispatcher->dispatch() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/ShellDispatcher.php:66 PHP 4. Shell->runCommand() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/ShellDispatcher.php:207 PHP 5. BakeShell->main() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Shell.php:434 PHP 6. ModelTask->execute() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Command/BakeShell.php:109 PHP 7. ModelTask->_interactive() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Command/Task/ModelTask.php:91 PHP 8. ModelTask->bakeTest() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Command/Task/ModelTask.php:282 PHP 9. TestTask->bake() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Command/Task/ModelTask.php:854 PHP 10. TestTask->generateFixtureList() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Command/Task/TestTask.php:146 PHP 11. TestTask->_processModel() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Command/Task/TestTask.php:376 PHP 12. Model->__get() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Command/Task/TestTask.php:394 PHP 13. Model->__isset() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Console/Command/Task/TestTask.php:892 PHP 14. Model->_constructLinkedModel() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Model/Model.php:859 PHP 15. ClassRegistry::init() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Model/Model.php:1048 PHP 16. class_exists() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Utility/ClassRegistry.php:145 PHP 17. Composer\Autoload\ClassLoader->loadClass() /var/www/html/hoge/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Utility/ClassRegistry.php:0
定義済みのAppModel
を定義しようとしてエラーが出ているので、おそらくcomposerのauto_loader絡みのエラーだとは当たりを付けられたのですが、どうするべきかわからなかったのでググりました。
解決策
cakephp – PHP Fatal error: Cannot redeclare class AppModel – Stack Overflow
こちらにフックスクリプトを利用した解決方法が掲載されていたため利用させていただきました。
composer.jsonにscriptsを追記
composer.json
{ "name": "hoge-app", "repositories": [ { "type": "pear", "url": "http://pear.cakephp.org" } ], // ↓このブロックを追加 "scripts": { "post-autoload-dump": [ "./composer_fix_autoload.sh" ] }, "require": { "pear-cakephp/cakephp": ">=2.4.4", "cakephp/debug_kit": "2.2.*", "slywalker/boost_cake": "*", "cakedc/utils": "*", "cakedc/users": "*", "cakedc/search": "*" }, "config": { "vendor-dir": "Vendor/" } }
composer_fix_autoload.shを追加
composer.jsonと同じディレクトリに以下のファイルを追加します。*1
composer_fix_autoload.sh
#!/bin/bash mv Vendor/composer/autoload_classmap.php Vendor/composer/autoload_classmap.php.bak sed '/CakePHP\/Cake\/Test\/Case\/Model\/models\.php/d' Vendor/composer/autoload_classmap.php.ori > Vendor/composer/autoload_classmap.php
autoload_classmap.phpをautoload_classmap.php.oriにコピー
cp /Vendor/composer/autoload_classmap.php /Vendor/composer/autoload_classmap.php.ori
composerのupdate
php composer.phar update
を実行して、composer_fix_autoload.sh
を通すようにする。
これでbake時にエラーが出なくなりました。
- composerのvendor-dirが異なる場合は置き換えてください [↩]