Command line mode, the different controllers are invoked according to the arguments. The specified method is executed periodically in the controller according to the configuration
application.php
<?php class application{public static function main () {header ("content-type:text/html;charset=utf-8");
Self::register ();
Self::commandline ();
Self::p athinfo ();
///Automatically load public static function LoadClass ($class) {$class =str_replace (' \ \ ', '/', $class);
$dir =str_replace (' \ \ ', '/', __dir__); $class = $dir. " /". $class.".
PHP ";
Require_once $class;
The//command line public static function CommandLine () {if (php_sapi_name () = "cli") {$_server[' path_info ']= ';
foreach ($_server[' argv '] as $k => $v) {if ($k ==0) continue;
$_server[' path_info '].= '/'. $v; //pathinfo processing public static function PathInfo () {if (Isset ($_server[' path_info ')) {$pathinfo =arra
Y_filter (Explode ("/", $_server[' path_info '));
For ($i =1 $i <=count ($pathinfo), $i + +) {$key =isset ($pathinfo [$i])? $pathinfo [$i]: '; $value =isset ($pathinfo [$i +1])?
$pathinfo [$i +1]: "";
Switch ($i) {Case 1: $_get[' m ']=ucfirst ($key);
Break
Case 2: $_get[' C ']=ucfirst ($key);
Break
Case 3: $_get[' a ']= $key;
Break
Default:if ($i >3) {if ($i%2==0) {$_get[$key]= $value;
}} break; }} $_get[' m ']=!empty ($_get[' m '))?
Ucfirst ($_get[' m ']): ' Index '; $_get[' C ']=!empty ($_get[' C '])?
Ucfirst ($_get[' C ']): ' Index '; $_get[' A ']=!empty ($_get[' a '])?
$_get[' A ']: ' Index ';
$class = "\\controller\\{$_get[' m ']}\\{$_get[' C ']}";
$controller =new $class;
$controller->$_get[' a '] ();
}//Fatal error callback public static function Shutdowncallback () {$e =error_get_last ();
if (! $e) return; Self::errorhandler ($e [' type '], ' <font color= ' red ' >fatal error</font> '. $e [' message '], $e [' file '], $e ['
Line ']); }//error handling protected static function MyErrorHandler ($errno, $errstr, $errfile, $ERRLINE) {list ($micseconds, $seconds) =explode ("", Microtime ());
$micseconds =round ($micseconds *1000); $micseconds =strlen ($micseconds) ==1?
' 0 '. $micseconds: $micseconds;
if (php_sapi_name () = = "CLI") {$break = "\ r \ n";
}else{$break = "<br/>"; $mes = "[". Date ("Y-m-d h:i:s", $seconds). ": {$micseconds}]". $errfile. "
". $errline." Line ". $errstr. $break;
Echo $mes;
//Register public static function register () {error_reporting (0); Set_error_handler (function ($errno, $ERRSTR, $errfile, $errline) {Self::myerrorhandler ($errno, $errstr, $errfile, $
Errline);
});
Register_shutdown_function (function () {self::shutdowncallback ();
});
Spl_autoload_register ("Self::loadclass"); } application::main ();
\controller\client\cron.php
<?php
namespace Controller\client;
Class cron{
private $second =0;
Private $tasks =array (
Array ("duration" =>5, "method" => "DoSomething"),
Array ("duration" =>2, " Method "=>" doSomething2),
);
Public Function Index () {while
(true)
: Sleep (1);
$this->second++;
foreach ($this->tasks as $task) {
if ($this->second% $task [' duration ']==0) {
$this-> $task [' method '] ]();
}
}
}
}
Public Function dosomething () {
echo ' ['. Date (' y-m-d h:i:s ', Time ()). "] DoSomething1 ok!\r\n ";
}
Public Function DoSomething2 () {
echo ' ['. Date (' y-m-d h:i:s ', Time ()). "] DoSomething2 ok!\r\n ";
}
}
Effect:
Method DoSomething is executed every 2 seconds
Method DoSomething2 is executed every 5 seconds
Now do other methods are synchronized, can be optimized to open a new thread to execute these methods, will not block the main thread of timing
Above this PHP command line to perform the integration PathInfo simulation Timing Task example is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.