FuelPHPåä½å®é¨ - oil console ï¼ PHP Interactiveæ¹ã使ã£ã¦ ããã¥ã¢ã«ã®ä¾æã³ããã§ããããªã¡ã½ããã試ãã¦ã¿ããâ彡
çæ§ããã«ã¡ã¯ï¼ FuelPHP Advent Calendar 2011 17æ¥ç®ã§ãã
æ¨æ¥ã¯ @madmamor ããã® ãFuelPHPã®coreã¯ã©ã¹ãæ¡å¼µãã¦ã¿ãã ãã§ããã
æ¬æ¥ã®ãé¡ã¯ãoil console ï¼ PHP Interactiveæ¹ã使ã£ã¦ ããã¥ã¢ã«ã®ä¾æã³ããã§ããããªã¡ã½ããã試ãã¦ã¿ããâ彡ãã§ãã
- oil console ã使ã£ã¦ã³ãã³ãã©ã¤ã³ã§FuelPHPã®ã¡ã½ãããå¼ã³åºãã¦ã¿ãã
- PHP InteractiveããFuelPHPã®ã¡ã½ãããå¼ã¹ãããã«å¾®æ¹é ãã¦ãããã¥ã¢ã«ä¾æã³ããã§è²ããªã¡ã½ããã試ãã¦ã¿ããã
ã®éçºã»å¦ç¿è£å©ãã¿2æ¬ç«ã¦ã§ããã¾ãã
ï¼ããã¥ã¡ã³ãã® http://docs.fuelphp.com/packages/oil/console.html ãä»å該å½ããç®æã«ãªãã¾ããï¼
1. oil consoleã£ã¦ä½ãï¼
FuelPHPã®ããã¥ã¢ã«ãèªãã§ããæä¸ãã¡ãã£ã¨ã ãã³ã¼ãã®æçã試ãããæãä½ãã®ã³ã³ããã¼ã©ã¼ãã¡ã¤ã«ã«<?php ï¼ä¸ç¥ï¼ public function action_hogehoge() { $arr1= Arr::average(array('1', 2, 4, '8')); var_dump($arr1); $arr2 = array('foo', 'bar', 'baz', 'yay'); Debug::dump(Arr::to_assoc($arr2)); } ï¼ä¸ç¥ï¼ ?>
ï¼Arr Class ã®ãåå¼·ä¸ - http://docs.fuelphp.com/classes/arr.html ï¼
ã¿ããã«ã½ã¼ã¹ãããããæ¸ãã¦ãã©ã¦ã¶ãIDEã§ç¢ºèªããã®ã£ã¦ã«ãã¿ã«ã¤ã§ãããã
FuelPHPã«ã¯ããããæç¨ã®consoleæ©è½ããããã³ãã³ãã©ã¤ã³ããè²ã
試ããã¨ãã§ãã¾ãã ( http://docs.fuelphp.com/packages/oil/console.html)
oilã³ãã³ãã¯ã½ã¼ã¹ã®README.md,fuel,docs,publicçã¨åãé層ã«ããã¾ããï¼
-
- -
phpæ¨æºã®date()ãdate_sunrise()ãªãã¦ã®ã試ãã¦ã¿ãããFuelPHPã®Arr::average()ãArr::to_assoc()ã試ãã¦ãå³ï¼
FuelPHPãæåããå¼ã³åºããphp -a ã¿ãããªãã®ã§ããã
ãã ã
<?php $arr1= Arr::average(array('1', 2, 4, '8')); var_dump($arr1); $arr2 = array('foo', 'bar', 'baz', 'yay'); Debug::dump(Arr::to_assoc($arr2)); ?>
ã®æ§ãªä¸è¡ã³ã¼ããããã¥ã¢ã«ããã³ãããã¦è©¦ãããæã«ã¯ã¾ãè¯ããã§ããã
<?php $people = array( array( "name" => "Jack", "age" => 21 ), array( "name" => "Jill", "age" => 23 ) ); print_r( Arr::assoc_to_keyval($people, 'name', 'age') ); ?>
ã¿ããã«ã³ã¼ãã«æ¹è¡ãå
¥ã£ã¦ãã¾ã£ãå ´åãã¾ãåãã¾ããorz
ãããããã½(`д)ï¾ï½·ï¾ï½²ï¾ï½° ï¼
2. PHP Interactiveã§FuelPHPã®ã¯ã©ã¹ãå¼ã³åºããããã«ããã°ããããããï¼
ã§ããã®oilã³ãã³ãã®consoleæ©è½ã§ããä¸è¡æ¯ã«readline()ã§å ¥åãèªã¿è¾¼ãã§evalãã¦ãããã§è¤æ°è¡å¯¾å¿ãããã®ã¯é¢åããã㧠PHP Interactive - http://www.hping.org/phpinteractive/ ã®åºçªã§ãã奥ããã
ããã¤ã¯æ¹è¡OKã§è¤æ°è¡å
¥åããªãããã³ã¼ãã®æçãã¤ã³ã¿ã©ã¯ãã£ãã«åããã¦è©¦ããã¹ã°ã¬ã¢ãã§ãã
ãããªæãï¼
ã§ãPHP Interactiveã«FuelPHPã®bootstrap.phpãèªã¿ãã¾ããã°ããããããï¼ã¨ããããã§å¾®æ¹é ãã¦ã¿ã¾ããã以ä¸ãã®è¨é²ã
注æï¼ããã¯ãã©ã¼ã å ã«æ¸ããã³ã¼ããevalãã¦å®è¡ãã¦ãwebããã°ã©ã ãªã®ã§æ±ºãã¦å ¬éãµã¼ãã¼ã«è¨ç½®ããªãã§ãã ãããï½½ï¾ï¾ï½°ï¾ï½¶ï½°ã®é¤é£ãé éãªã¢ã³ã³åã«ãªã£ã¦ãã¾ãã¾ããããã¾ã§ãåå¼·ç¨ã®å¤é¨ããç¹ãããªããã¼ã«ã«ç°å¢ã®ã¿ã§å®é¨ãã¦ãã ããã
PHP Interactiveæ¹ã§ãåå¼·ãã¦ãã¤ã¡ã¼ã¸å³ï¼
é«ç´ãªã³ã¼ãã¼ã§ã飲ã¿ãªãã軽ããã«ãµã³ãã«ã³ã¼ãããã©ãã°ã»ã¢ã³ãã»ãããããã¦updateãã¿ã³æ¼ãã¦åä½ç¢ºèªã
ãã§ãã³ã¼ããåããããã®å ´ã§å°ããã¤ãªãã·ã§ã³ãå¤ãå¤ãããããªããç解ãæ·±ãã¦ããã¾ãããªãã¦ã¤ã³ããªã¸ã§ã³ã¹ãªãã§ãããï¼â¦
ã§ã¯å¾®æ¹é ä½æ¥ã«çæãã¾ãããã
以ä¸ãã¼ã«ã«ç°å¢ãå¤ããã¤ãªããé¯ã«æ±ºãã¦è¨ç½®ããªããã¨ãï½½ï¾ï¾ï½°ï¾ï½¶ï½°ã®é¤é£ï¼ä»¥ä¸ç¥
ä½æ¥ï¼ï¼PHP Interactiveã®ã½ã¼ã¹ããã¦ã³ãã¼ãããã
ã½ã¼ã¹ã¯ ttp://www.hping.org/phpinteractive/phpinteractive-0.2.tar.gz ããåå¾ãã¦ãã ãããï¼ãµã¤ã㯠http://www.hping.org/phpinteractive/ ï¼
ä½æ¥ï¼ï¼ã½ã¼ã¹ã解åãã¦public/phpinteractive以ä¸ã«è¨ç½®ããã¼ããã·ã§ã³ãchown,chmodã§æ£ãããã¦ããã
(scripts以ä¸ã¯å±¥æ´ä¿åãã£ã¬ã¯ããªãªã®ã§apacheã¦ã¼ã¶ã¼ã®æ¸ãè¾¼ã¿æ¨©éå¿
é ï¼
ä½æ¥ï¼ï¼public/phpinteractive以ä¸ã«.htaccessãæ°è¨
ã½ã¼ã¹ï¼ public/phpinteractive/.htaccess
php_flag short_open_tag On
phpinteractiveã®ã½ã¼ã¹ãã·ã§ã¼ããªã¼ãã³ã¿ã°ã使ã£ã¦ããã®ã§ãã®ãã£ã¬ã¯ããªé
ä¸ã®ã¿short_open_tagãæå¹ã«ãã¦ãã¾ãã
ï¼ãµããã£ã¬ã¯ããªã§.htaccessã使ããªãå ´åã¯apacheã®confãã¡ã¤ã«ã§ AllowOverride Allã«ãªã£ã¦ãããè¦ç¢ºèªï¼
ä½æ¥ï¼ï¼public/phpinteractive/includefuel.phpãæ°è¨
ã½ã¼ã¹ï¼ public/phpinteractive/includefuel.php
<?php /** * Set error reporting and display errors settings. You will want to change these when in production. */ error_reporting(-1); ini_set('display_errors', 0); /** * Website document root */ define('DOCROOT', dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR); /** * Path to the application directory. */ define('APPPATH', realpath(dirname(dirname(__DIR__)).'/fuel/app/').DIRECTORY_SEPARATOR); /** * Path to the default packages directory. */ define('PKGPATH', realpath(dirname(dirname(__DIR__)).'/fuel/packages/').DIRECTORY_SEPARATOR); /** * The path to the framework core. */ define('COREPATH', realpath(dirname(dirname(__DIR__)).'/fuel/core/').DIRECTORY_SEPARATOR); // Get the start time and memory for use later defined('FUEL_START_TIME') or define('FUEL_START_TIME', microtime(true)); defined('FUEL_START_MEM') or define('FUEL_START_MEM', memory_get_usage()); // bypass shutdown_handler define('MY_INTERACTIVE_MODE', true); // Boot the app require APPPATH.'bootstrap.php'; //FUEL_ENV check if('development'!==\Fuel::$env) { echo 'development mode is required.'; exit; } ?>
oilãåèã«ä½ãã¾ãããMY_INTERACTIVE_MODE ã®å®æ°ã¯ç¬èªã«è¿½å ãã¦ãã¾ãã
ãã£ããæ¬çªã®å
¬éãµã¼ãã¼ã«ã¢ãããã¼ããã¦ãã¾ã£ãæã®æ°ä¼ãã®ãã\Fuel::$envãdevelopment以å¤ã®æã¯ä½ãããããã§çµäºããã¦ãã¾ãã(åè http://docs.fuelphp.com/installation/instructions.html#/setting_the_environment )
ä½æ¥ï¼ï¼public/phpinteractive/index.phpã微修æ£
ä¿®æ£ç®æã¯ä»¥ä¸ã®ã¨ããã§ãã
å¤æ´ç®æã®ç»é¢ã·ã§ããï¼
ãªãªã¸ãã«ã¨ã®å·®åï¼
root@star:/var/vhosts/contact.pizw.net/public/phpinteractive# diff index.php orig_index.php 2d1 < include('./includefuel.php'); 94c93 < if(!@unlink("scripts/$name")) --- > if(!@unlink("scripts/$name")) 135c134 < if (rename_script($current, $newname) == 0) --- > if (rename_script($current, $newname) == 0) 441,442c440 < //echo(htmlentities($code)) < echo(htmlentities($code, ENT_QUOTES, mb_internal_encoding())); --- > echo(htmlentities($code)) 498,499c496 < //$script_output = "<pre>".htmlentities($script_output)."</pre>"; < $script_output = "<pre>".htmlentities($script_output, ENT_QUOTES, mb_internal_encoding())."</pre>"; --- > $script_output = "<pre>".htmlentities($script_output)."</pre>";
2è¡ç®ã§ã¯æ°è¨ããincludefuel.phpãincludeãã¦ãã¾ããããã§FuelPHPã®ã¯ã©ã¹ãå¼ã¹ãããã«ãªãã¾ãã
441,498è¡ç®ä»è¿ã¯æ¥æ¬èªæååãåããªãããã«ããç®çã§ã®ä¿®æ£ã§ãã
ä½æ¥ï¼ï¼fuel/app/bootstrap.phpã§Error::ã®ã¯ã©ã¹ãæ¡å¼µåºæ¥ãããã«å¤æ´
ã½ã¼ã¹ï¼ fuel/app/bootstrap.php
<?php // Load in the Autoloader require COREPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php'; class_alias('Fuel\\Core\\Autoloader', 'Autoloader'); // Bootstrap the framework DO NOT edit this require COREPATH.'bootstrap.php'; Autoloader::add_classes(array( // Add classes you want to override here // Example: 'View' => APPPATH.'classes/view.php', 'Error' => APPPATH.'classes/error.php',//âãã®è¡ã追å )); // Register the autoloader Autoloader::register(); /** * Your environment. Can be set to any of the following: * * Fuel::DEVELOPMENT * Fuel::TEST * Fuel::STAGE * Fuel::PRODUCTION */ Fuel::$env = (isset($_SERVER['FUEL_ENV']) ? $_SERVER['FUEL_ENV'] : Fuel::DEVELOPMENT); // Initialize the framework with the config file. Fuel::init('config.php'); ?>
PHP Interactiveã®å ¥åå´ã§PHPã®ææ³ã¨ã©ã¼ã«ãªããããªæ¸ãæ¹ãããå ´åãfuel/core/classes/error.phpã®shutdown_handler()ãåå¿ã(fuel/core/bootstrap.phpå ã®register_shutdown_functionãåç §)ããã§exitãã¦ãã¾ãã®ã§ãããããã§ã¯PHP Interactiveã®ç»é¢ã表示ãããªããªãã次ã®æä½ã«å°ããã\Error::shutdown_handler()ãå¾®å¤æ´ãã¾ãã
ãããªæãã§æ¢ã¾ãã¨ç¶è¡ã§ããªãã¦å°ãã§ãï¼
ä½æ¥ï¼ï¼fuel/app/classes/error.phpãæ°è¨
ã³ã¢ã®ã¯ã©ã¹ãã¡ã¤ã«ãæ¡å¼µãã¾ãã(ããã¥ã¡ã³ãï¼ http://docs.fuelphp.com/general/extending_core.html)
ã½ã¼ã¹ï¼ fuel/app/classes/error.php
<?php /** * Core Class Extends Example (error.php) * @package * @version 0.01 * @author mataga * @license MIT License * @copyright 2011 mataga * @link http://twitter.com/mataga */ class Error extends Fuel\Core\Error { /** * Native PHP shutdown handler * * @return string */ public static function shutdown_handler() { $last_error = error_get_last(); // Only show valid fatal errors if ($last_error AND in_array($last_error['type'], static::$fatal_levels)) { $severity = static::$levels[$last_error['type']]; logger(Fuel::L_ERROR, $severity.' - '.$last_error['message'].' in '.$last_error['file'].' on line '.$last_error['line']); $error = new \ErrorException($last_error['message'], $last_error['type'], 0, $last_error['file'], $last_error['line']); //MY_INTERACTIVDE_MODE -- ADD BEGIN if(defined('MY_INTERACTIVE_MODE') && MY_INTERACTIVE_MODE) { echo \Debug::dump($last_error); return; } //MY_INTERACTIVDE_MODE -- ADD END if (\Fuel::$env != Fuel::PRODUCTION) { static::show_php_error($error); } else { static::show_production_error($error); } exit(1); } } }//endofclass /* End of file error.php */ ?>
//MY_INTERACTIVDE_MODE ã§æãã 5è¡ã追å ããã ãã§ããããã§ãã¼ã¹ã¨ã©ã¼ãèµ·ãã¦ã PHP Interactiveæ¹çµç±ã®å ´åã«ã¯ãããªæãã§å¦çã¯ç¶è¡ããã¾ãã
ä½æ¥ï¼ï¼åä½ç¢ºèª
ttp://example.com/phpinteractive/ ãéãã¦ãããã試ãã¹ã
ãããªæãã§Arr::ã®å®é¨ããã¦ã¿ããããã¨ãããã«ç´°ããåãã確èªã§ãã¦ä½¿ãæ¹ãè¦ããããã§ãã
ã¨ããããã§FuelPHPã®ããã¥ã¡ã³ããµã¤ã( http://docs.fuelphp.com/ )ã®ãµã³ãã«ã³ã¼ããåããã¾ãã£ã¦ãããªããçæãã¤ã¹ã¿ã¼ã«ãªãã¾ãããï¼
触ãã°è§¦ãã»ã©æ·±ãå³ããããã¾ããã
ææ¥ã¯ @NEKOGET ããã®ãFuelPHPã®å¼ã³æ¹ããã«ãªãã¾ãã
ã§ã¯ã§ã¯ï¼