Esempio n. 1
0
function __ini_app(\Owl\Application $app)
{
    $app->middleware(function ($request, $response) {
        $start = microtime(true);
        yield;
        $use_time = (microtime(true) - $start) * 1000;
        $response->withHeader('x-run-time', (int) $use_time);
    });
    $router = new \Owl\Mvc\Router(['namespace' => '\\Controller']);
    $app->middleware(function ($request, $response) use($router) {
        $router->execute($request, $response);
    });
    $app->setExceptionHandler(function ($exception, $request, $response) {
        if ($exception instanceof \Owl\Http\Exception) {
            $status = $exception->getCode();
        } else {
            $status = 500;
            log_exception(get_logger('default'), $exception);
        }
        $response->withStatus($status);
        if (DEBUG) {
            foreach (__exception_headers($exception, 8) as $key => $value) {
                $response->withHeader($key, $value);
            }
        }
        if (!$request->isAjax()) {
            $view = new \Owl\Mvc\View(ROOT_DIR . '/View');
            $response->write($view->render('_error', ['exception' => $exception]));
        }
    });
    return $app;
}
Esempio n. 2
0
 public function __construct($ip, $port)
 {
     parent::__construct();
     $this->server = $server = new \swoole_http_server($ip, $port);
     $server->on('Request', function ($request, $response) {
         $request = new \Owl\Swoole\Request($request);
         $response = new \Owl\Swoole\Response($response);
         $this->execute($request, $response);
     });
 }
Esempio n. 3
0
 public function disconnect()
 {
     if ($this->client) {
         $parameters = $this->getConfig('parameters');
         $is_persistent = isset($parameters['persistent']) && $parameters['persistent'];
         if (!$is_persistent) {
             $this->client->disconnect();
         }
         $this->client = null;
         App::log('debug', 'redis disconnected', ['parameters' => $parameters]);
     }
 }
Esempio n. 4
0
function __ini_app(\Owl\Application $app)
{
    $app->middleware(function ($request, $response) {
        $start = microtime(true);
        yield;
        $use_time = (microtime(true) - $start) * 1000;
        $response->setHeader('use-time', (int) $use_time . 'ms');
    });
    $router = new \Owl\Mvc\Router(['namespace' => '\\Controller']);
    $app->middleware(function ($request, $response) use($router) {
        $router->execute($request, $response);
    });
    $app->setExceptionHandler(function ($exception, $request, $response) {
        $status = 500;
        if ($exception instanceof \Owl\Http\Exception) {
            $status = $exception->getCode();
        }
        $response->setStatus($status);
        $view = new \Owl\Mvc\View(ROOT_DIR . '/View');
        $response->setBody($view->render('_error', ['exception' => $exception]));
    });
    return $app;
}
Esempio n. 5
0
File: index.php Progetto: noikiy/owl
<?php

/**
 * @author Patsura Dmitry http://github.com/ovr <*****@*****.**>
 */
use Owl\Application;
use Owl\Http\Request;
use Owl\Router\Route;
use Owl\Router\Router;
include_once __DIR__ . '/../vendor/autoload.php';
$serviceManager = new Owl\Service\Manager();
$router = new Router();
/**
 * Frontend
 */
$router->add('/', ['module' => 'Frontend', 'controller' => 'Index', 'action' => 'index']);
/**
 * Api module - REST API
 */
$router->add('/api', ['module' => 'Api', 'controller' => 'Index', 'action' => 'index']);
$router->add('/api/users', ['module' => 'Api', 'controller' => 'User', 'action' => 'list']);
$router->add('/api/users/:id', ['module' => 'Api', 'controller' => 'User', 'action' => 'index']);
$router->add('/api/users/:id', ['module' => 'Api', 'controller' => 'User', 'action' => 'create'], Route::POST);
$router->add('/api/users/:id', ['module' => 'Api', 'controller' => 'User', 'action' => 'delete'], Route::DELETE);
$serviceManager->set('router', $router);
$application = new Application($serviceManager);
$response = $application->handle(Request::createFromGlobals());
$response->send();
Esempio n. 6
0
<?php

defined('TEST') or define('TEST', true);
require __DIR__ . '/../src/autoload.php';
\Owl\Application::registerNamespace('\\Tests', __DIR__);
Esempio n. 7
0
 public function execute($sql, $params = null)
 {
     $params = $params === null ? [] : is_array($params) ? $params : array_slice(func_get_args(), 1);
     App::log('debug', 'database execute', ['sql' => $sql instanceof \PDOStatement ? $sql->queryString : $sql, 'parameters' => $params]);
     $sth = $sql instanceof \PDOStatement ? $sql : $this->connect()->prepare($sql);
     $sth->execute($params);
     $sth->setFetchMode(\PDO::FETCH_ASSOC);
     return $sth;
 }
Esempio n. 8
0
 /**
  * @param \Owl\Http\Request $request
  * @param \Owl\Http\Response $response
  * @return \Owl\Http\Response $response
  *
  * @throws \Owl\Http\Exception 404
  * @throws \Owl\Http\Exception 501
  */
 protected function respond(\Owl\Http\Request $request, \Owl\Http\Response $response)
 {
     App::log('debug', 'router respond', ['url' => (string) $request->getUri(), 'method' => $request->getMethod()]);
     $path = $this->getRequestPath($request);
     list($class, $parameters) = $this->byRewrite($path) ?: $this->byPath($path);
     App::log('debug', 'router dispatch', ['class' => $class, 'parameters' => $parameters]);
     if (!class_exists($class)) {
         throw \Owl\Http\Exception::factory(404);
     }
     $controller = new $class($request, $response);
     $controller->request = $request;
     $controller->response = $response;
     // 如果__beforeExecute()返回了内容就直接返回内容
     if (method_exists($controller, '__beforeExecute') && ($data = call_user_func_array([$controller, '__beforeExecute'], $parameters))) {
         if ($data instanceof \Psr\Http\Message\StreamInterface) {
             $response->withBody($data);
         } elseif (!$data instanceof \Owl\Http\Response) {
             $response->write($data);
         }
         return $response;
     }
     $method = $request->getMethod();
     if ($method === 'HEAD') {
         $method = 'GET';
     }
     if (!in_array($method, ['HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'])) {
         throw \Owl\Http\Exception::factory(501);
     }
     if (!is_callable([$controller, $method])) {
         throw \Owl\Http\Exception::factory(405);
     }
     $data = call_user_func_array([$controller, $method], $parameters);
     if ($data instanceof \Psr\Http\Message\StreamInterface) {
         $response->withBody($data);
     } elseif ($data !== null && !$data instanceof \Owl\Http\Response) {
         $response->write($data);
     }
     if (method_exists($controller, '__afterExecute')) {
         $controller->__afterExecute($request, $response);
     }
     return $response;
 }
Esempio n. 9
0
File: index.php Progetto: noikiy/owl
 * @author Patsura Dmitry http://github.com/ovr <*****@*****.**>
 */
use Owl\Application;
use Owl\Http\Request;
use Owl\Router\Route;
use Owl\Router\Router;
error_reporting(-1);
ini_set('display_errors', 1);
include_once __DIR__ . '/../vendor/autoload.php';
$eventManager = new \Owl\Event\Manager();
$profiler = new Owl\Debug\Profiler();
$profiler->setAssetsUri('//assets.owl.local/profiler/min.css');
$profiler->listen($eventManager);
$serviceManager = new Owl\Service\Manager();
$router = new Router();
$router->add('/', ['module' => 'Common', 'controller' => 'Index', 'action' => 'index']);
$serviceManager->set('router', $router);
$driver = new \Owl\DBAl\Driver\Mysql('mysql:host=127.0.0.1;dbname=phalcon-module-skeleton;port=49153', 'root', 'root', array(PDO::ATTR_PERSISTENT => true, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "UTF8"'));
$connection = new \Owl\DBAL\Connection(['driver' => $driver], $eventManager);
$serviceManager->set('connection', $connection);
$cache = new \Owl\Cache\Driver\Memcached();
$serviceManager->set('cache', $cache);
$view = new \Owl\Mvc\View();
$view->setPath(__DIR__ . '/../app/modules/common/resources/views/');
$serviceManager->set('view', $view);
$application = new Application($serviceManager, $eventManager);
$application->setErrorHandlerParameters(array('module' => 'Common', 'controller' => 'Index', 'action' => 'error'));
$application->setExceptionHandlerParameters(array('module' => 'Common', 'controller' => 'Index', 'action' => 'exception'));
$response = $application->handle(Request::createFromGlobals(), new \Owl\Http\Response());
$response = $profiler->setContent($response);
$response->send();