Example #1
0
File: App.php Project: miper/miper
 /**
  * 获取唯一实例
  * @return Miper_App
  */
 public static function getAppInstance()
 {
     if (!self::$app) {
         self::$app = new Miper_App();
     }
     return self::$app;
 }
Example #2
0
<?php

$root = dirname(__DIR__);
define('VENDOR_DIR', $root . '/vendor/');
define('LIBRARY_DIR', $root . '/src/');
require VENDOR_DIR . '/Miper/Autoload.php';
Miper_Autoload::autoload(VENDOR_DIR, LIBRARY_DIR);
$app = Miper_App::getAppInstance();
$app->get(['/test/#{foo:?string}/#{userid}', function ($req) {
    return array('args' => $req->args);
}])->output()->end();
$app->get(['/hello/world', function ($req) {
    return 'Hello,World';
}])->output()->end();
$app->delegate('/user/', 'Happy_Delegate_User');
$app->error('msful.notfound', function ($msg, $detail) use($app) {
    header('HTTP/1.1 404 Not Found');
    return array('msg' => $msg, 'detail' => $detail);
});
Example #3
0
File: User.php Project: miper/miper
 function delegate(Miper_App $app)
 {
     require_once LIBRARY_DIR . '/app/user/UserExport.php';
     $app->get('/user/#{uid}')->call(['UserExport', 'getUser'])->output()->end();
     $app->get('/user/recommends')->call(['UserExport', 'getRecommendUserIds'], 'userIds')->call(['UserExport', 'batchGetUsers'], array('@item' => array('uid' => 'user_id', 'flag' => '@unset')))->output()->end();
 }
Example #4
0
 private function handleClass($path)
 {
     $className = '';
     $fh = fopen($path, 'r');
     while (($line = fgets($fh)) !== false) {
         if (preg_match('#\\bclass\\s+([a-z][a-z0-9\\_]+)\\b#i', $line, $ms)) {
             $className = $ms[1];
             break;
         }
     }
     fclose($fh);
     if (!$className) {
         return;
     }
     $routers = array();
     // 虚构请求环境
     $_GET = array();
     $_POST = array();
     $_SERVER = array('REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '*');
     if (!self::$initedApp) {
         Miper_App::hook('pipe.request.before', function ($app, $func, $options) use(&$routers) {
             $routers[$app->pipeId][$func] = $options;
             return false;
         });
         Miper_App::hook('pipe.call.before', function ($app, $func, $options) use(&$routers) {
             if (!isset($routers[$app->pipeId][$func])) {
                 $routers[$app->pipeId][$func] = $options;
             }
             return false;
         });
         self::$initedApp = true;
     }
     require_once $path;
     $refClass = new ReflectionClass($className);
     $interNames = $refClass->getInterfaceNames();
     if (in_array('Miper_Delegate_Interface', $interNames)) {
         if (!self::$app) {
             $app = self::$app = Miper_App::getAppInstance();
         }
         $cls = new $className();
         $cls->delegate(self::$app);
     }
     $result = array();
     foreach ($routers as $route) {
         $result[] = array('method' => $route['request'][0], 'url' => $route['request'][1], 'class' => $route['call'][0], 'func' => $route['call'][1]);
     }
     return $result;
 }