Pyrusã§ããã¸ã§ã¯ããã¼ã«ã«ãªPEARã©ã¤ãã©ãªã¤ã³ã¹ãã¼ã« : PHP Advent Calendar jp 2011 Day 10
PHP Avent Calendar jp 2011ã®10æ¥ç®ã[twitter:@calpo22]ã§ãã
âåæ¥ãincludeとextractの組み合わせでテンプレート処理を作る。PHPのAdvent Calender #9 - それマグで!ã
â»è¿½è¨ 2012/09/16
PHPãå¤ãã¨ãã®äºæ
ããªããã°ãä»ã¯ã©ã¤ãã©ãªã®ä¾å管çã«ã¯composer使ãã®ãããã¨æãã¾ãã
åãæ¥ãæå
ã§PHPUnit使ãããã¨ãã§ããã° http://qiita.com/items/81085381c4281e498cde ã§ä¸çºã
ã¿ãªããPEAR使ã£ã¦ã¾ããï¼
PEARã®ã©ã¤ãã©ãªãæ®éã«ã¤ã³ã¹ãã¼ã«ããã¨/usr/binã¨ãã«å
¥ããã§ããããããããªãã¦èªåç¨ã«~/binã«å
¥ãããã¨ãããã¬ã¼ã ã¯ã¼ã¯ã®vendorãã£ã¬ã¯ããªã«å
¥ãããã¨ãããã¾ãããã
ããã§çµæ§è¦å´ãã¦ããã¨ãããä»å¹´ã®PHPã«ã³ãã¡ã¬ã³ã¹ã§ãPyrus使ãã¨æãããã¿ãããªè©±ãèãããã¨ãã§ãã¾ããã
pearã®å¾ç¶ã¨ãªãããã±ã¼ã¸ããã¼ã¸ã£ããããããªã«ãããããï¼ããã§ãã¤ãï¼ãã¨æå¾
ã«è¸ããµããã¾ãã¤ã¤å®¶ã«å¸°ã£ã¦è©¦ãã¦ã¿ãã¨ããã»ã»ã»
ã¨ã«ããæ¥æ¬èªã®æ å ±ãå ¨ç¶ç¡ã
ã¨ããç¶æ ã§ãªããå¹ã¿ã¾ããã
ã§ããPyrusã ã¨pearãããããã¸ã§ã¯ããã¼ã«ã«ãªã©ã¤ãã©ãªã®ã¤ã³ã¹ãã¼ã«ããããããã§ãï¼
ã¨ãããã¨ã§Pyrusã«ããPEARã©ã¤ãã©ãªã¤ã³ã¹ãã¼ã«ã®ãç´¹ä»ã
ã¾ãã¯ä¿ºãã¼ã«ã«ãªPEARã©ã¤ãã©ãªã¤ã³ã¹ãã¼ã«
~/mypyrusã«èªåç¨ã®PEARã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãã
(å¿
ããã俺ãã¼ã«ã«ãå¿
è¦ãªããã§ã¯ãªãã§ãããæ´çããããã£ãã®ã§ãã®ããã«ãã¦ãã¾ã)
pyrus.pharãã¦ã³ãã¼ã
pyrus.pharèªä½ã¯ã©ãã«ãã£ã¦ããããã§ãããã¨ãããã~/mypyrusã«å ¥ãã¦ãããã¨ã«ã
$ mkdir mypyrus $ cd mypyrus/ $ wget http://pear2.php.net/pyrus.phar
pyrusã®åæè¨å®
æåã«pyrusãå®è¡ããã¨åæè¨å®ãè¡ãããããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«å ã®ãã£ã¬ã¯ããªçãè¨å®ããã¾ãã
$ php pyrus.phar install Pyrus version 2.0.0a3 SHA-1: BE7EA9D171AE3873F1BBAF692EEE9165BB14BD5D Pyrus: No user configuration file detected It appears you have not used Pyrus before, welcome! Initialize install? Please choose: yes no [yes] : # âyesãªã®ã§ãã®ã¾ã¾Enter Great. We will store your configuration in: /home/calpo/.pear/pearconfig.xml Where would you like to install packages by default? [/home/calpo/mypyrus] : # âã«ã¤ã³ã¹ãã¼ã«ãªã®ã§ãã®ã¾ã¾Enter $ ls ~/.pear/pearconfig.xml /home/calpo/.pear/pearconfig.xml
~/.pear/pearconfig.xmlãä½æããã¾ãã
config-showã³ãã³ãã§è¨å®ã確èªã§ãã¾ãã
$ php pyrus.phar config-show Pyrus version 2.0.0a3 SHA-1: BE7EA9D171AE3873F1BBAF692EEE9165BB14BD5D Using PEAR installation found at /home/calpo/mypyrus System paths: php_dir => /home/calpo/mypyrus/php ext_dir => /usr/lib64/php/modules cfg_dir => /home/calpo/mypyrus/cfg doc_dir => /home/calpo/mypyrus/docs bin_dir => /usr/bin data_dir => /home/calpo/mypyrus/data ããã» ããã»
bin_dirã/usr/binã«ãªã£ã¦ããã®ã§èªåç¨ã«å¤æ´ãã¾ãããã
(~/mypyrus/.config ãçæããã¾ã)
$ php pyrus.phar set bin_dir /home/calpo/mypyrus/bin $ php pyrus.phar config-show ããã» ããã» bin_dir => /home/calpo/mypyrus/bin ããã» ããã»
PHPUnitã¤ã³ã¹ãã¼ã«
æºåãã§ããã®ã§ä¿ºå°ç¨PHPUnitãã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãããã
$ php pyrus.phar set auto_discover on $ php pyrus.phar channel-discover pear.phpunit.de $ php pyrus.phar install phpunit/PHPUnit Pyrus version 2.0.0a3 SHA-1: BE7EA9D171AE3873F1BBAF692EEE9165BB14BD5D Using PEAR installation found at /home/calpo/mypyrus Sorry, phpunit/PHPUnit references an unknown channel pear.symfony-project.com for pear.symfony-project.com/YAML Do you want to add this channel and continue? Please choose: yes no [yes] : # âéä¸ãã£ã³ãã«ã®è¿½å ã«ã¤ãã¦åããã¾ã ããã» ããã» pear.phpunit.de/PHP_Invoker depended on by pear.phpunit.de/PHPUnit
ã©ãï¼
å
¥ãã¾ããã
$cd ~/mypyrus $ ls bin cache docs downloads php pyrus.phar $ ls bin/ phpunit $ ls php/ File PHP PHPUnit SymfonyComponents Text
include_pathã®è¨å®
æ©ébin/ã«å ¥ã£ãphpunitãå®è¡ãããã¨ãã¦ããå¿ è¦ãªãã£ã¬ã¯ããªãinclude_pathã«è¨å®ããã¦ããªãã®ã§æããã¾ãã
$ cd ~/mypyrus/bin $ ./phpunit Warning: require(PHPUnit/Autoload.php): failed to open stream: No such file or directory in /home/calpo/mypyrus/bin/phpunit on line 42 $ php -i | grep include_path include_path => .:/usr/share/pear:/usr/share/php => .:/usr/share/pear:/usr/share/php
ä»åã¯ãæ軽ã«~/mypyrus/bin/phpunit ã«è¨å®ã追å ãã¦ã~/mypyrus/phpãinclude_pathã«å ¥ãã¦ãã¾ãã¾ãã
<?php // âãrequireã®åã«è¿½å set_include_path( realpath(dirname(__FILE__).'/../php') . PATH_SEPARATOR . dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
ãã ã©ã¤ãã©ãªã«ãã£ã¦bin/ã«å
¥ããã¡ã¤ã«ãphpã¹ã¯ãªããã ã£ããã·ã§ã«ã¹ã¯ãªããã ã£ãããPHP_CLASSPATHã¿ãããªç¬èªã®ç°å¢å¤æ°ãè¨å®ããå¿
è¦ããã£ããã¨ãªããããã»ã»ã»
php.iniãå¤æ´ã§ãããªããããä¸çªç¢ºå®ã§ãã
$ cd ~/mypyrus/bin $ ./phpunit --version PHPUnit 3.6.4 by Sebastian Bergmann.
ããã¼ã
ããã¸ã§ã¯ããã¼ã«ã«ãªPEARã©ã¤ãã©ãªã¤ã³ã¹ãã¼ã«
ç¶ãã¦é©å½ãªããã¸ã§ã¯ãã®vendorãã£ã¬ã¯ããªã«ããã¸ã§ã¯ãå°ç¨PEARã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãã
ãã£ã¬ã¯ããªã®è¨å®
setã¨ãã®ã³ãã³ãã®åã«ãã£ã¬ã¯ããªãæå®ãã¦ãããã¨ããããã¼ã«ã«ãªä½æ¥ã¨è¨å®ãã§ãã¾ãã
(ãã®ãã£ã¬ã¯ããªã«.configãåºæ¥ä¸ããã¾ã)
ä¾ãã°~/project_hoge/vendorã«ã¤ã³ã¹ãã¼ã«ããå ´åã»ã»ã»
$ php pyrus.phar /home/calpo/project_hoge/vendor set bin_dir /home/calpo/project_hoge/vendor/bin $ php pyrus.phar /home/calpo/project_hoge/vendor config-show Pyrus version 2.0.0a3 SHA-1: BE7EA9D171AE3873F1BBAF692EEE9165BB14BD5D Using PEAR installation found at /home/calpo/project_hoge/vendor System paths: php_dir => /home/calpo/project_hoge/vendor/php ext_dir => /usr/lib64/php/modules cfg_dir => /home/calpo/project_hoge/vendor/cfg doc_dir => /home/calpo/project_hoge/vendor/docs bin_dir => /home/calpo/project_hoge/vendor/bin ããã» ããã»
ã¤ã³ã¹ãã¼ã«
ãã£ã³ãã«ã®è¨å®ãªã©ãããã¸ã§ã¯ããã¼ã«ã«ãªã®ã§ã¾ãè¨å®ãã¾ãã
$ php pyrus.phar /home/calpo/project_hoge/vendor set auto_discover on $ php pyrus.phar /home/calpo/project_hoge/vendor channel-discover pear.phpunit.de $ php pyrus.phar /home/calpo/project_hoge/vendor install phpunit/PHPUnit ããã» ããã» pear.phpunit.de/PHP_Invoker depended on by pear.phpunit.de/PHPUnit $ ls ~/project_hoge/vendor/bin phpunit
ãããªæãã§ããã¸ã§ã¯ããã¼ã«ã«ãªPEARã©ã¤ãã©ãªãã»ãã»ãã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã