Esempio n. 1
0
 public function handle($uri = null)
 {
     /** @var Response $response */
     $response = parent::handle($uri);
     if ($response->getContent()) {
         return $response;
     }
     switch ($response->getStatusCode()) {
         case Response::HTTP_NOT_FOUND:
         case Response::HTTP_METHOD_NOT_ALLOWED:
         case Response::HTTP_UNAUTHORIZED:
         case Response::HTTP_FORBIDDEN:
         case Response::HTTP_INTERNAL_SERVER_ERROR:
             if ($this->request->isAjax() || $response instanceof JsonResponse) {
                 $response->setContent(array('error' => $response->getStatusMessage()));
             } else {
                 $template = new Template($this->view, 'error');
                 $template->set('code', $response->getStatusCode());
                 $template->set('message', $response->getStatusMessage());
                 $response->setContent($template->render());
             }
             break;
         default:
             break;
     }
     return $response;
 }
Esempio n. 2
0
 public function __construct()
 {
     // 创建swoole_http_server对象
     $http = new swoole_http_server("0.0.0.0", 9501);
     // 设置参数
     $http->set(array('worker_num' => 16, 'daemonize' => false, 'max_request' => 10000, 'dispatch_mode' => 1));
     $http->setGlobal(HTTP_GLOBAL_ALL);
     // 绑定WorkerStart
     $http->on('WorkerStart', array($this, 'onWorkerStart'));
     // 绑定request
     $http->on('request', function ($request, $response) {
         ob_start();
         try {
             //注入uri
             $_GET['_url'] = $request->server['request_uri'];
             $application = new \Phalcon\Mvc\Application($this->di);
             echo $application->handle()->getContent();
         } catch (Exception $e) {
             echo $e->getMessage();
         }
         $result = ob_get_contents();
         ob_end_clean();
         $response->end($result);
     });
     $http->start();
 }
Esempio n. 3
0
 public static function run()
 {
     global $di;
     $di = new \Phalcon\DI\FactoryDefault();
     self::initSystemConst();
     self::initSystemService();
     self::initAutoloaders();
     $application = new \Phalcon\Mvc\Application($di);
     //Register the installed modules
     $application->registerModules(array('home' => array('className' => 'Application\\Home\\Module', 'path' => '../application/modules/home/module.php'), 'admin' => array('className' => 'Application\\Admin\\Module', 'path' => '../application/modules/admin/module.php'), 'api' => array('className' => 'Application\\Api\\Module', 'path' => '../application/modules/Api/module.php')));
     echo $application->handle()->getContent();
 }
Esempio n. 4
0
<?php

error_reporting(E_ALL);
define('APP_PATH', realpath('..'));
try {
    /**
     * Read the configuration
     */
    $config = (include APP_PATH . "/app/config/config.php");
    /**
     * Read auto-loader
     */
    include APP_PATH . "/app/config/loader.php";
    /**
     * Read services
     */
    include APP_PATH . "/app/config/services.php";
    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);
    echo $application->handle()->getContent();
} catch (\Exception $e) {
    echo $e->getMessage();
}
Esempio n. 5
0
 /**
  * Execute Phalcon Developer Tools
  *
  * @param  string             $path The path to the Phalcon Developer Tools
  * @param  string             $ip   Optional IP address for securing Developer Tools
  * @return void
  * @throws \Exception         if Phalcon extension is not installed
  * @throws \Exception         if Phalcon version is not compatible Developer Tools
  * @throws \Phalcon\Exception if Application config could not be loaded
  */
 public static function main($path, $ip = null)
 {
     if (!extension_loaded('phalcon')) {
         throw new \Exception('Phalcon extension is not installed, follow these instructions to install it: http://phalconphp.com/documentation/install');
     }
     if ($ip !== null) {
         self::$ip = $ip;
     }
     if (!defined('TEMPLATE_PATH')) {
         define('TEMPLATE_PATH', $path . '/templates');
     }
     chdir('..');
     // Read configuration
     $configPaths = array('config', 'app/config', 'apps/frontend/config');
     $readed = false;
     foreach ($configPaths as $configPath) {
         $cpath = $configPath . '/config.ini';
         if (file_exists($cpath)) {
             $config = new \Phalcon\Config\Adapter\Ini($cpath);
             $readed = true;
             break;
         } else {
             $cpath = $configPath . '/config.php';
             if (file_exists($cpath)) {
                 $config = (require $cpath);
                 $readed = true;
                 break;
             }
         }
     }
     if ($readed === false) {
         throw new \Phalcon\Exception('Configuration file could not be loaded!');
     }
     $loader = new \Phalcon\Loader();
     $loader->registerDirs(array($path . '/scripts/', $path . '/scripts/Phalcon/Web/Tools/controllers/'));
     $loader->registerNamespaces(array('Phalcon' => $path . '/scripts/'));
     $loader->register();
     if (Version::getId() < Script::COMPATIBLE_VERSION) {
         throw new \Exception('Your Phalcon version is not compatible with Developer Tools, download the latest at: http://phalconphp.com/download');
     }
     try {
         $di = new FactoryDefault();
         $di->set('view', function () use($path) {
             $view = new View();
             $view->setViewsDir($path . '/scripts/Phalcon/Web/Tools/views/');
             return $view;
         });
         $di->set('config', $config);
         $di->set('url', function () use($config) {
             $url = new \Phalcon\Mvc\Url();
             $url->setBaseUri($config->application->baseUri);
             return $url;
         });
         $di->set('flash', function () {
             return new \Phalcon\Flash\Direct(array('error' => 'alert alert-error', 'success' => 'alert alert-success', 'notice' => 'alert alert-info'));
         });
         $di->set('db', function () use($config) {
             if (isset($config->database->adapter)) {
                 $adapter = $config->database->adapter;
             } else {
                 $adapter = 'Mysql';
             }
             if (is_object($config->database)) {
                 $configArray = $config->database->toArray();
             } else {
                 $configArray = $config->database;
             }
             $className = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
             unset($configArray['adapter']);
             return new $className($configArray);
         });
         self::$di = $di;
         $app = new \Phalcon\Mvc\Application();
         $app->setDi($di);
         echo $app->handle()->getContent();
     } catch (\Phalcon\Exception $e) {
         echo get_class($e), ': ', $e->getMessage(), "<br>";
         echo nl2br($e->getTraceAsString());
     } catch (\PDOException $e) {
         echo get_class($e), ': ', $e->getMessage(), "<br>";
         echo nl2br($e->getTraceAsString());
     }
 }
Esempio n. 6
0
 public function testApplicationModulesDefinitionClosure()
 {
     // Creates the autoloader
     $loader = new \Phalcon\Loader();
     $loader->registerNamespaces(array('Frontend\\Controllers' => __DIR__ . '/modules/frontend/controllers/', 'Backend\\Controllers' => __DIR__ . '/modules/backend/controllers/'));
     $loader->register();
     $_GET['_url'] = '/login';
     Phalcon\DI::reset();
     $di = new Phalcon\DI\FactoryDefault();
     $di->set('router', function () {
         $router = new Phalcon\Mvc\Router(false);
         $router->add('/index', array('controller' => 'index', 'module' => 'frontend', 'namespace' => 'Frontend\\Controllers\\'));
         $router->add('/login', array('controller' => 'login', 'module' => 'backend', 'namespace' => 'Backend\\Controllers\\'));
         return $router;
     });
     $application = new Phalcon\Mvc\Application();
     $view = new \Phalcon\Mvc\View();
     $application->registerModules(array('frontend' => function ($di) use($view) {
         $di->set('view', function () use($view) {
             $view = new \Phalcon\Mvc\View();
             $view->setViewsDir(__DIR__ . '/modules/frontend/views/');
             return $view;
         });
     }, 'backend' => function ($di) use($view) {
         $di->set('view', function () use($view) {
             $view->setViewsDir(__DIR__ . '/modules/backend/views/');
             return $view;
         });
     }));
     $application->setDi($di);
     $this->assertEquals($application->handle()->getContent(), '<html>here</html>' . PHP_EOL);
     $loader->unregister();
 }
Esempio n. 7
0
<?php

error_reporting(E_ALL);
define('APP_PATH', dirname(__DIR__));
$loader = (require APP_PATH . '/vendor/autoload.php');
$config = (require APP_PATH . "/config/config.php");
$di = (require APP_PATH . "/config/services.php");
try {
    $app = new \Phalcon\Mvc\Application($di);
    $app->setEventsManager($di['eventsManager']);
    $app->useImplicitView(false);
    echo $app->handle()->getContent();
} catch (\Exception $e) {
    $di['logger']->error($e->getMessage() . "\n" . $e->getTraceAsString());
    $di['response']->setStatusCode(500)->setContent("Internal error")->send();
}
Esempio n. 8
0
    /** @var Phalcon\Session\AdapterInterface $session */
    $session = new $class(['path' => Config::instance()->session->save_path, 'lifetime' => Config::instance()->session->lifetime]);
    $session->start();
    return $session;
}, true);
$di->set('view', function () {
    return new TestView();
}, true);
$di->set('dispatcher', function () use($di) {
    return new Phalcon\Mvc\Dispatcher();
}, true);
$di->set('url', function () use($di) {
    $url = new Phalcon\Mvc\Url();
    //$url->setBaseUri($di['config']->base_uri);
    return $url;
}, true);
$di->set('response', new Phalcon\Http\Response());
$requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
if (false !== ($pos = strpos($requestUri, '?'))) {
    $requestUri = substr($requestUri, 0, $pos);
}
$requestUri = rtrim($requestUri, '/');
if (strlen($requestUri) == 0 || $requestUri[0] != '/') {
    $requestUri = '/';
}
$app = new Phalcon\Mvc\Application($di);
$di->set('request_uri', function () use($requestUri) {
    return urldecode($requestUri);
});
echo $app->handle($di['request_uri'])->getContent();
Esempio n. 9
0
function run_cgi_application()
{
    $di = get_app_di();
    $application = new \Phalcon\Mvc\Application($di);
    echo $application->handle()->getContent();
}
Esempio n. 10
0
<?php

$loader = (require __DIR__ . '/../app/autoload.php');
$kernel = new AppKernel('dev');
$kernel->boot();
$kernel->registerRoutes();
$app = new \Phalcon\Mvc\Application($kernel->getDI());
/**
 * Handle the request
 */
$response = $app->handle();
$response->send();
Esempio n. 11
0
 public static function run()
 {
     if (in_array(APPLICATION_ENV, array('development'))) {
         $debug = new \Phalcon\Debug();
         $debug->listen();
     }
     $di = new \Phalcon\DI\FactoryDefault();
     $config = (include APPLICATION_PATH . '/config/application.php');
     $di->set('config', $config);
     $loader = new \Phalcon\Loader();
     $loader->registerNamespaces($config->loader->namespaces->toArray());
     $loader->register();
     $loader->registerDirs(array(APPLICATION_PATH . "/plugins/"));
     $db = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "charset" => $config->database->charset));
     $di->set('db', $db);
     $view = new View();
     /* $view->disableLevel(array(
        View::LEVEL_BEFORE_TEMPLATE => true,
        View::LEVEL_LAYOUT          => true,
        View::LEVEL_AFTER_TEMPLATE  => true
        )); */
     define('MAIN_VIEW_PATH', '../../../views/');
     $view->setMainView(MAIN_VIEW_PATH . 'main');
     $view->setLayoutsDir(MAIN_VIEW_PATH . '/layouts/');
     $view->setPartialsDir(MAIN_VIEW_PATH . '/partials/');
     $volt = new \Application\Mvc\View\Engine\Volt($view, $di);
     $volt->setOptions(array('compiledPath' => APPLICATION_PATH . '/cache/volt/'));
     $volt->initCompiler();
     $viewEngines = array(".volt" => $volt);
     $view->registerEngines($viewEngines);
     $di->set('view', $view);
     $viewSimple = new \Phalcon\Mvc\View\Simple();
     $viewSimple->registerEngines($viewEngines);
     $di->set('viewSimple', $viewSimple);
     $url = new \Phalcon\Mvc\Url();
     $url->setBasePath('/');
     $url->setBaseUri('/');
     $application = new \Phalcon\Mvc\Application();
     $application->registerModules($config->modules->toArray());
     $router = new \Application\Mvc\Router\DefaultRouter();
     foreach ($application->getModules() as $module) {
         $className = str_replace('Module', 'Routes', $module['className']);
         if (class_exists($className)) {
             $class = new $className();
             $router = $class->init($router);
         }
     }
     $di->set('router', $router);
     $eventsManager = new \Phalcon\Events\Manager();
     $dispatcher = new \Phalcon\Mvc\Dispatcher();
     $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
         new ExceptionPlugin($dispatcher, $exception);
     });
     $eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) {
         new LocalizationPlugin($dispatcher);
     });
     $eventsManager->attach("acl", function ($event, $acl) {
         if ($event->getType() == 'beforeCheckAccess') {
             echo $acl->getActiveRole(), $acl->getActiveResource(), $acl->getActiveAccess();
         }
     });
     $dispatcher->setEventsManager($eventsManager);
     $di->set('dispatcher', $dispatcher);
     $session = new \Phalcon\Session\Adapter\Files();
     $session->start();
     $di->set('session', $session);
     $acl = new \Application\Acl\DefaultAcl();
     $acl->setEventsManager($eventsManager);
     $di->set('acl', $acl);
     $assets = new \Phalcon\Assets\Manager();
     $di->set('assets', $assets);
     $flash = new \Phalcon\Flash\Session(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning'));
     $di->set('flash', $flash);
     $di->set('helper', new \Application\Mvc\Helper());
     $application->setDI($di);
     echo $application->handle()->getContent();
 }
Esempio n. 12
0
        throw $e;
    }
}
set_exception_handler(function (Exception $e) {
    logException($e);
    if (!Config::instance()->production || PHP_SAPI == 'cli') {
        throw $e;
    } else {
        $app = new Phalcon\Mvc\Application(Phalcon\DI::getDefault());
        switch (true) {
            case $e instanceof PageNotFound:
            case $e instanceof Phalcon\Mvc\Dispatcher\Exception:
                header('HTTP/1.1 404 Not Found');
                header('Status: 404 Not Found');
                $app->di->set('last_exception', $e);
                exit($app->handle('/error/show404')->getContent());
            default:
                header('HTTP/1.1 503 Service Temporarily Unavailable');
                header('Status: 503 Service Temporarily Unavailable');
                header('Retry-After: 3600');
                exit($app->handle('/error/show503')->getContent());
        }
    }
});
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    switch ($errno) {
        case E_USER_NOTICE:
        case E_STRICT:
            break;
        default:
            Rj\Logger::messages()->error(sprintf("Error: #%d %s at %s:%d", $errno, $errstr, $errfile, $errline));
Esempio n. 13
0
 /**
  * Executes the web tool application
  *
  * @param string $path
  */
 public static function main($path)
 {
     chdir('..');
     if (!extension_loaded('phalcon')) {
         throw new \Exception('Phalcon extension isn\'t installed, follow these instructions to install it: http://phalconphp.com/documentation/install');
     }
     //Read configuration
     $configPath = "app/config/config.ini";
     if (file_exists($configPath)) {
         $config = new \Phalcon\Config\Adapter\Ini($configPath);
     } else {
         $configPath = "app/config/config.php";
         if (file_exists($configPath)) {
             $config = (require $configPath);
         } else {
             throw new \Phalcon\Exception('Configuration file could not be loaded');
         }
     }
     $loader = new \Phalcon\Loader();
     $loader->registerDirs(array($path . '/scripts/', $path . '/scripts/Phalcon/Web/Tools/controllers/'));
     $loader->registerNamespaces(array('Phalcon' => $path . '/scripts/'));
     $loader->register();
     if (Version::getId() < Script::COMPATIBLE_VERSION) {
         throw new \Exception('Your Phalcon version isn\'t compatible with Developer Tools, download the latest at: http://phalconphp.com/download');
     }
     if (!defined('TEMPLATE_PATH')) {
         define('TEMPLATE_PATH', $path . '/templates');
     }
     try {
         $di = new \Phalcon\Di\FactoryDefault();
         $di->set('view', function () use($path) {
             $view = new \Phalcon\Mvc\View();
             $view->setViewsDir($path . '/scripts/Phalcon/Web/Tools/views/');
             return $view;
         });
         $di->set('config', $config);
         $di->set('url', function () use($config) {
             $url = new \Phalcon\Mvc\Url();
             $url->setBaseUri($config->application->baseUri);
             return $url;
         });
         $di->set('flash', function () {
             return new \Phalcon\Flash\Direct(array('error' => 'alert alert-error', 'success' => 'alert alert-success', 'notice' => 'alert alert-info'));
         });
         $di->set('db', function () use($config) {
             return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name));
         });
         self::$_di = $di;
         $app = new \Phalcon\Mvc\Application();
         $app->setDi($di);
         echo $app->handle()->getContent();
     } catch (\Phalcon\Exception $e) {
         echo get_class($e), ': ', $e->getMessage(), "<br>";
         echo nl2br($e->getTraceAsString());
     } catch (\PDOException $e) {
         echo get_class($e), ': ', $e->getMessage(), "<br>";
         echo nl2br($e->getTraceAsString());
     }
 }
Esempio n. 14
0
 * Sets locale information
 */
setlocale(LC_ALL, 'ru_RU.UTF-8');
/**
 * Enable framework debugger
 */
(new \Phalcon\Debug())->listen();
try {
    /**
     * Read the configuration
     */
    $config = (include __DIR__ . "/../app/config/config.php");
    /**
     * Read auto-loader
     */
    include __DIR__ . "/../app/config/loader.php";
    /**
     * Read services
     */
    include __DIR__ . "/../app/config/services.php";
    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);
    $response = $application->handle()->getContent();
} catch (Phalcon\Exception $e) {
    $response = $e->getMessage();
} catch (PDOException $e) {
    $response = $e->getMessage();
}
echo $response;