| #2 | Phalcon\Mvc\Application->handle
/home/dev/public_html/apps/Bootstrap.php (144) <?php
require_once __DIR__ . '/../common/lib/global-functions.php';
class Bootstrap
{
private $app;
public function __construct()
{
// Dependency Injector
$di = new \Phalcon\DI\FactoryDefault();
// Configuration
$config = loadConfigFile('config');
$databaseCredentials = $config->sensitive->database;
unset($config->sensitive);
$di->setShared('config', $config);
// 301 Redirect if /public/index.php
$currentUrl = getCurrentUrl();
if (strpos($currentUrl, 'public/index.php') !== false) {
return $di->getResponse()->redirect($config->site->url, true, 301)->send();
}
// 301 Redirect if /public/ - Replace only the first occurance of /public/
$currentPath = str_replace($config->site->url, '', $currentUrl);
if (substr($currentPath, 0, 6) == 'public') {
$needle = '/public/';
$redirect = substr_replace($currentUrl, '/', strpos($currentUrl, $needle), strlen($needle));
return $di->getResponse()->redirect($redirect, true, 301)->send();
}
// Router
$di->setShared('router', function() use ($di, $config) {
$router = new \Phalcon\Mvc\Router();
$uri = $di->getRequest()->getURI();
$uriHint = substr($uri, 0, 8);
$module = 'Frontend';
if ($uriHint == '/api/v1/') {
$module = 'Api';
}
$router->setDefaultModule(strtolower($module));
$router->setDefaultNamespace($module . '\Controllers');
$router->setDefaultController('index');
$router->setDefaultAction('index');
$router->removeExtraSlashes(true);
require_once $config->site->path->configs . 'routes.php';
return $router;
});
// Crypt
$di->setShared('crypt', function () use ($config) {
$crypt = new \Phalcon\Crypt();
$crypt->setKey('i$4^&/:%2@k%0ROQ<@{(e=*!<2u|rI~4');
return $crypt;
});
// URL component
$di->setShared('url', function() use ($config) {
$url = new \Phalcon\Url();
$url->setBaseUri($config->site->url);
$url->setStaticBaseUri($config->site->staticUrl);
return $url;
});
if (IS_CLI_EXECUTE === false) {
$di->set('session', function () use ($config) {
$session = new \Phalcon\Session\Manager;
$adapter = new \Phalcon\Session\Adapter\Stream([
// 'uniqueId' => 'drooble-' . $config->module,
'savePath' => $config->site->path->cache . 'sessions/'
]);
$session->setAdapter($adapter);
$session->start();
return $session;
});
}
// Cache
$di->setShared('cache', function() use ($config) {
$serializerFactory = new \Phalcon\Storage\SerializerFactory();
$adapter = new \Phalcon\Cache\Adapter\Stream($serializerFactory, [
'defaultSerializer' => 'Json',
'storageDir' => $config->site->path->cache . 'site',
'lifetime' => 172800, // 2d
'serializer' => new \Phalcon\Storage\Serializer\Json()
]);
return new \Phalcon\Cache($adapter);
});
// Database connection. Use set() if Transactions are needed
if ($databaseCredentials->DATABASE_DISABLED !== true) {
$di->setShared('db', function() use ($databaseCredentials) {
return new \Phalcon\Db\Adapter\Pdo\Mysql([
'host' => $databaseCredentials->host,
'username' => $databaseCredentials->username,
'password' => $databaseCredentials->password,
'dbname' => $databaseCredentials->name,
// 'charset' => 'utf8',
'options' => [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
\PDO::ATTR_PERSISTENT => true,
\PDO::ATTR_STRINGIFY_FETCHES => false
]
]);
});
}
$this->app = new \Phalcon\Mvc\Application();
$this->app->setDI($di);
}
public function run()
{
$modules = [
'frontend' => [
'className' => 'Frontend\Module',
'path' => __DIR__ .'/frontend/Module.php'
],
];
// Is there an API?
$uri = $this->app->request->getURI();
if ($this->app->config->dev->hasApi === true AND substr($uri, 0, 5) == '/api/') {
// Extract api version from url: /api/v1/robots...
$parts = array_filter(explode('/', $uri));
// Save API version
$this->app->config->apiVersion = $parts[2];
// Add to modules
$modules['api'] = [
'className' => 'Api\Module',
'path' => __DIR__ .'/api/'. $this->app->config->apiVersion .'/Module.php'
];
}
// Register application modules
$this->app->registerModules($modules);
echo $this->app->handle($uri)->getContent();
}
public function getApp()
{
return $this->app;
}
} |
| #3 | Bootstrap->run
/home/dev/public_html/public/index.php (24) <?php
try {
define('DEV', in_array($_SERVER['REMOTE_ADDR'], ['77.70.108.105', '::1']));
define('IS_CLI_EXECUTE', false);
$root = str_replace('\\', '/', dirname(dirname(__FILE__))) . '/';
require_once $root . 'apps/Bootstrap.php';
$bootstrap = new Bootstrap();
// Listen for all notices, warnings and exceptions in Dev mode
if ($bootstrap->getApp()->config->debug === true) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
$debug = new \Phalcon\Debug();
$debug->listenLowSeverity();
} else {
error_reporting(0);
}
$isWebpSupported = (isset($_SERVER['HTTP_ACCEPT']) && ($_SERVER['HTTP_ACCEPT'] == '*/*' OR strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false));
define('WEBP_SUPPORT', $isWebpSupported);
// Run
$bootstrap->run();
} catch (\Exception $e) {
if ($bootstrap->getApp()->config->debug === true) {
$debug = new \Phalcon\Debug();
die($debug->listen()->onUncaughtException($e));
} else {
// Log the error
// $logFile = $bootstrap->getApp()->config->site->path->logs . 'errors/error-500_'. date('YmdHis') .'_'. mt_rand(1, 1000000) .'.txt';
// file_put_contents($logFile, var_export($e, true));
error_log($e);
// Output for user
header('HTTP/1.1 500 Internal Server Error');
die(file_get_contents('../apps/frontend/views/_layouts/error-500.phtml'));
}
} |