> php教程 > php手册 > 본문

[PHP] 명령줄 실행 및 pathinfo 시뮬레이션 예약 작업 통합

WBOY
풀어 주다: 2016-08-15 16:49:47
원래의
1602명이 탐색했습니다.

命令行模式下,根据传参,调用不同控制器。控制器中根据配置定时执行指定方法

Application.php

<?<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Application{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> main(){
        </span><span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">);
        self</span>::<span style="color: #000000;">register();
        self</span>::<span style="color: #000000;">commandLine();
        self</span>::<span style="color: #008080;">pathInfo</span><span style="color: #000000;">();
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">自动加载</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> loadClass(<span style="color: #800080;">$class</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$class</span>=<span style="color: #008080;">str_replace</span>('\\', '/', <span style="color: #800080;">$class</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$dir</span>=<span style="color: #008080;">str_replace</span>('\\', '/',<span style="color: #000000;"> __DIR__);
        </span><span style="color: #800080;">$class</span>=<span style="color: #800080;">$dir</span>."/".<span style="color: #800080;">$class</span>.".php"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">require_once</span> <span style="color: #800080;">$class</span><span style="color: #000000;">;        
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">命令行下</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> commandLine(){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">php_sapi_name</span>()=="cli"<span style="color: #000000;">){
            </span><span style="color: #800080;">$_SERVER</span>['PATH_INFO']=""<span style="color: #000000;">;
            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$_SERVER</span>['argv'] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$k</span>=><span style="color: #800080;">$v</span><span style="color: #000000;">) {
                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$k</span>==0) <span style="color: #0000ff;">continue</span><span style="color: #000000;">;
                </span><span style="color: #800080;">$_SERVER</span>['PATH_INFO'].="/".<span style="color: #800080;">$v</span><span style="color: #000000;">;
            }
        }
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">pathinfo处理</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> <span style="color: #008080;">pathInfo</span><span style="color: #000000;">(){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SERVER</span>['PATH_INFO'<span style="color: #000000;">])){
            </span><span style="color: #800080;">$pathinfo</span>=<span style="color: #008080;">array_filter</span>(<span style="color: #008080;">explode</span>("/", <span style="color: #800080;">$_SERVER</span>['PATH_INFO'<span style="color: #000000;">]));
            </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1;<span style="color: #800080;">$i</span><=<span style="color: #008080;">count</span>(<span style="color: #800080;">$pathinfo</span>);<span style="color: #800080;">$i</span>++<span style="color: #000000;">){
                </span><span style="color: #800080;">$key</span>=<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$pathinfo</span>[<span style="color: #800080;">$i</span>]) ? <span style="color: #800080;">$pathinfo</span>[<span style="color: #800080;">$i</span>] : ''<span style="color: #000000;">;
                </span><span style="color: #800080;">$value</span>=<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$pathinfo</span>[<span style="color: #800080;">$i</span>+1]) ? <span style="color: #800080;">$pathinfo</span>[<span style="color: #800080;">$i</span>+1] :""<span style="color: #000000;">;
                </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$i</span><span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">case</span> 1:
                        <span style="color: #800080;">$_GET</span>['m']=<span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$key</span><span style="color: #000000;">);
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 2:
                        <span style="color: #800080;">$_GET</span>['c']=<span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$key</span><span style="color: #000000;">);
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 3:
                        <span style="color: #800080;">$_GET</span>['a']=<span style="color: #800080;">$key</span><span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">default</span>:
                        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span>>3<span style="color: #000000;">){
                            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span>%2==0<span style="color: #000000;">){
                                </span><span style="color: #800080;">$_GET</span>[<span style="color: #800080;">$key</span>]=<span style="color: #800080;">$value</span><span style="color: #000000;">;
                            }
                        }
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                }
            }
        }
        </span><span style="color: #800080;">$_GET</span>['m']=!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['m']) ? <span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$_GET</span>['m']) : 'Index'<span style="color: #000000;">;
        </span><span style="color: #800080;">$_GET</span>['c']=!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['c']) ? <span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$_GET</span>['c']) : 'Index'<span style="color: #000000;">;
        </span><span style="color: #800080;">$_GET</span>['a']=!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['a']) ? <span style="color: #800080;">$_GET</span>['a'] : 'index'<span style="color: #000000;">;
        </span><span style="color: #800080;">$class</span>="\\Controller\\{<span style="color: #800080;">$_GET</span>['m']}\\{<span style="color: #800080;">$_GET</span>['c']}"<span style="color: #000000;">;
        </span><span style="color: #800080;">$controller</span>=<span style="color: #0000ff;">new</span> <span style="color: #800080;">$class</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$controller</span>-><span style="color: #800080;">$_GET</span>['a'<span style="color: #000000;">]();
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">致命错误回调</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> shutdownCallback(){
        </span><span style="color: #800080;">$e</span>=<span style="color: #000000;">error_get_last();
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$e</span>) <span style="color: #0000ff;">return</span><span style="color: #000000;">;
        self</span>::errorHandler(<span style="color: #800080;">$e</span>['type'],'<font color="red">Fatal Error</font> '.<span style="color: #800080;">$e</span>['message'],<span style="color: #800080;">$e</span>['file'],<span style="color: #800080;">$e</span>['line'<span style="color: #000000;">]);
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">错误处理</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> myErrorHandler(<span style="color: #800080;">$errno</span>,<span style="color: #800080;">$errstr</span>,<span style="color: #800080;">$errfile</span>,<span style="color: #800080;">$errline</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">list</span>(<span style="color: #800080;">$micseconds</span>,<span style="color: #800080;">$seconds</span>)=<span style="color: #008080;">explode</span>(" ",<span style="color: #008080;">microtime</span><span style="color: #000000;">());
        </span><span style="color: #800080;">$micseconds</span>=<span style="color: #008080;">round</span>(<span style="color: #800080;">$micseconds</span>*1000<span style="color: #000000;">);
        </span><span style="color: #800080;">$micseconds</span>=<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$micseconds</span>)==1 ? '0'.<span style="color: #800080;">$micseconds</span> : <span style="color: #800080;">$micseconds</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">php_sapi_name</span>()=="cli"<span style="color: #000000;">){
            </span><span style="color: #800080;">$break</span>="\r\n"<span style="color: #000000;">;
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #800080;">$break</span>="<br/>"<span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$mes</span>="[".<span style="color: #008080;">date</span>("Y-m-d H:i:s",<span style="color: #800080;">$seconds</span>).":{<span style="color: #800080;">$micseconds</span>}] ".<span style="color: #800080;">$errfile</span>." ".<span style="color: #800080;">$errline</span>." line ".<span style="color: #800080;">$errstr</span>.<span style="color: #800080;">$break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$mes</span><span style="color: #000000;">;        
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">注册</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> register(){
        </span><span style="color: #008080;">error_reporting</span>(0<span style="color: #000000;">);
        </span><span style="color: #008080;">set_error_handler</span>(<span style="color: #0000ff;">function</span>(<span style="color: #800080;">$errno</span>,<span style="color: #800080;">$errstr</span>,<span style="color: #800080;">$errfile</span>,<span style="color: #800080;">$errline</span><span style="color: #000000;">){
            self</span>::myErrorHandler(<span style="color: #800080;">$errno</span>,<span style="color: #800080;">$errstr</span>,<span style="color: #800080;">$errfile</span>,<span style="color: #800080;">$errline</span><span style="color: #000000;">);
        });
        </span><span style="color: #008080;">register_shutdown_function</span>(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
            self</span>::<span style="color: #000000;">shutdownCallback();
        });
        spl_autoload_register(</span>"self::loadClass"<span style="color: #000000;">);
    }
}

Application</span>::main();
로그인 후 복사

\Controller\Client\Cron.php

<?<span style="color: #000000;">php
namespace Controller\Client;

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Cron{
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$second</span>=0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$tasks</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(
        </span><span style="color: #0000ff;">array</span>("duration"=>5,"method"=>"doSomething"),
        <span style="color: #0000ff;">array</span>("duration"=>2,"method"=>"doSomething2"),<span style="color: #000000;">
    );
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> index(){
        </span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">true</span><span style="color: #000000;">) {
            </span><span style="color: #008080;">sleep</span>(1<span style="color: #000000;">);
            </span><span style="color: #800080;">$this</span>->second++<span style="color: #000000;">;
            </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$this</span>->tasks <span style="color: #0000ff;">as</span> <span style="color: #800080;">$task</span><span style="color: #000000;">){
                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->second%<span style="color: #800080;">$task</span>['duration']==0<span style="color: #000000;">){
                    </span><span style="color: #800080;">$this</span>-><span style="color: #800080;">$task</span>['method'<span style="color: #000000;">]();
                }
            }
        }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> doSomething(){
        </span><span style="color: #0000ff;">echo</span> "[".<span style="color: #008080;">date</span>("Y-m-d H:i:s",<span style="color: #008080;">time</span>())."] doSomething1 ok!\r\n"<span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> doSomething2(){
        </span><span style="color: #0000ff;">echo</span> "[".<span style="color: #008080;">date</span>("Y-m-d H:i:s",<span style="color: #008080;">time</span>())."] doSomething2 ok!\r\n"<span style="color: #000000;">;
    }
}</span>
로그인 후 복사

效果:

方法doSomething每隔2秒执行一次

方法doSomething2每隔5秒执行一次

现在执行其他方法是同步的,可以再优化成开新线程执行这些方法,就不会阻塞主线程的定时了

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿