Created
October 10, 2013 16:05
-
-
Save niden/6920924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* \NDN\Api\Bootstrap | |
* Bootstrap.php | |
* | |
* Bootstraps the API application | |
* | |
* @author Nikos Dimopoulos <nikos@niden.net> | |
* @since 2012-12-03 | |
* @category Library | |
* | |
*/ | |
namespace NDN; | |
use Phalcon\Exception as PhException; | |
use Phalcon\Mvc\Application as PhApplication; | |
use Phalcon\Config as PhConfig; | |
use Phalcon\Loader as PhLoader; | |
use Phalcon\Mvc\Url as PhUrl; | |
use Phalcon\Mvc\Router as PhRouter; | |
use Phalcon\Mvc\Dispatcher as PhDispatcher; | |
use Phalcon\Mvc\View as PhView; | |
use Phalcon\Mvc\View\Engine\Volt as PhVolt; | |
use Phalcon\Mvc\Model\Metadata\Memory as PhMetadataMemory; | |
use Phalcon\Mvc\Model\Metadata\Files as PhMetadataFiles; | |
use Phalcon\Events\Manager as PhEventsManager; | |
use Phalcon\Logger as PhLogger; | |
use Phalcon\Logger\Adapter\File as PhLoggerFile; | |
use Phalcon\Logger\Formatter\Line as PhLoggerFormatter; | |
use Phalcon\Session\Adapter\Files as PhSession; | |
use Phalcon\Cache\Frontend\Data as PhCacheFront; | |
use Phalcon\Cache\Backend\File as PhCacheBack; | |
use Phalcon\Db\Adapter\Pdo\Mysql as PhMysql; | |
use Phalcon\Security as PhSecurity; | |
class Bootstrap | |
{ | |
private $di; | |
/** | |
* Constructor | |
* | |
* @param $di | |
*/ | |
public function __construct($di) | |
{ | |
$this->di = $di; | |
} | |
/** | |
* Runs the application performing all initializations | |
* | |
* @param $options | |
* | |
* @return mixed | |
*/ | |
public function run($options) | |
{ | |
$loaders = array( | |
'config', | |
'loader', | |
'environment', | |
'session', | |
'timezone', | |
'url', | |
'router', | |
'dispatcher', | |
'view', | |
'logger', | |
'database', | |
'cache', | |
'security', | |
); | |
foreach ($loaders as $service) { | |
$function = 'init' . $service; | |
$this->$function($options); | |
} | |
$application = new PhApplication(); | |
$application->setDI($this->di); | |
return $application->handle()->getContent(); | |
} | |
// Protected functions | |
/** | |
* Initializes the config. Reads it from its location and | |
* stores it in the Di container for easier access | |
* | |
* @param array $options | |
*/ | |
public function initConfig($options = array()) | |
{ | |
$configFile = require(ROOT_PATH . '/api/config/global.php'); | |
// Create the new object | |
$config = new PhConfig($configFile); | |
// Store it in the Di container | |
// Settings cones from the include | |
$this->di['config'] = $config; | |
} | |
/** | |
* Initializes the loader | |
* | |
* @param array $options | |
*/ | |
public function initLoader($options = array()) | |
{ | |
$config = $this->di['config']; | |
// Creates the autoloader | |
$loader = new PhLoader(); | |
$loader->registerDirs( | |
array($config->app_path->library) | |
); | |
$paths = array( | |
'NDNAPI' => $config->app_path->library, | |
'NDNAPI\Controllers' => $config->app_path->controllers, | |
'NDNAPI\Models' => $config->app_path->models, | |
); | |
// Register the Library namespace | |
$loader->registerNamespaces($paths); | |
// $eventsManager = new \Phalcon\Events\Manager(); | |
// | |
// //Listen all the loader events | |
// $eventsManager->attach('loader', function($event, $loader, $data) { | |
// //if ($event->getType() == 'beforeCheckPath') { | |
// if ($event->getType() == 'pathFound') { | |
// echo '<span style="color:green">', $event->getType(), ' ', $loader->getCheckedPath(), ' (', $data, ')</span><br>'; | |
// } else { | |
// echo $event->getType(), ' ', $loader->getCheckedPath(), ' ', $data, '<br>'; | |
// } | |
// //} | |
// }); | |
// | |
// $loader->setEventsManager($eventsManager); | |
$loader->register(); | |
// Dump it in the DI to reuse it | |
$this->di['loader'] = $loader; | |
} | |
/** | |
* Initializes the environment | |
* | |
* @param array $options | |
*/ | |
public function initEnvironment($options = array()) | |
{ | |
$config = $this->di['config']; | |
$debug = (isset($config->app_debug)) ? | |
(bool) $config->app_debug : | |
false; | |
if ($debug) { | |
ini_set('display_errors', true); | |
error_reporting(E_ALL); | |
} else { | |
ini_set('display_errors', false); | |
error_reporting(-1); | |
} | |
} | |
/** | |
* Initializes the timezone | |
* | |
* @param array $options | |
*/ | |
public function initTimezone($options = array()) | |
{ | |
$config = $this->di['config']; | |
$registry = $this->di['registry']; | |
$timezone = (isset($config->app_timezone)) ? | |
$config->app_timezone : | |
'US/Eastern'; | |
date_default_timezone_set($timezone); | |
$registry->set('timezone_default', $timezone); | |
} | |
/** | |
* Initializes the baseUrl | |
* | |
* @param array $options | |
*/ | |
public function initUrl($options = array()) | |
{ | |
$config = $this->di['config']; | |
/** | |
* The URL component is used to generate all kind of urls in the | |
* application | |
*/ | |
$this->di['url'] = function () use ($config) { | |
$url = new PhUrl(); | |
$url->setBaseUri($config->app_baseUri); | |
return $url; | |
}; | |
} | |
/** | |
* Initializes the router | |
* | |
* @param array $options | |
*/ | |
public function initRouter($options = array()) | |
{ | |
$registry = $this->di['registry']; | |
$config = $registry->get('config-global'); | |
$this->di['router'] = function () use ($config) { | |
$router = new PhRouter(false); | |
$router->removeExtraSlashes(true); | |
foreach ($config['app_routes'] as $item) { | |
$method = $item['action']; | |
$route = $item['route']; | |
$params = $item['params']; | |
$router->$method($route, $params); | |
} | |
return $router; | |
}; | |
} | |
/** | |
* Initializes the dispatcher | |
* | |
* @param array $options | |
*/ | |
public function initDispatcher($options = array()) | |
{ | |
$di = $this->di; | |
$this->di['dispatcher'] = function () use ($di) { | |
$evManager = $di->getShared('eventsManager'); | |
// $evManager->attach( | |
// "dispatch:beforeException", | |
// function($event, $dispatcher, $exception) | |
// { | |
// switch ($exception->getCode()) { | |
// case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND: | |
// case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND: | |
// $dispatcher->forward( | |
// [ | |
// 'controller' => 'error', | |
// 'action' => 'show404', | |
// ) | |
// ); | |
// return false; | |
// } | |
// } | |
// ); | |
$dispatcher = new PhDispatcher(); | |
$dispatcher->setEventsManager($evManager); | |
$dispatcher->setDefaultNamespace('NDNAPI\\Controllers\\'); | |
return $dispatcher; | |
}; | |
} | |
/** | |
* Initializes the view and Volt | |
* | |
* @param array $options | |
*/ | |
public function initView($options = array()) | |
{ | |
$di = $this->di; | |
$config = $di['config']; | |
$this->di['volt'] = function ($view, $di) use ($config) { | |
$volt = new PhVolt($view, $di); | |
$volt->setOptions( | |
array( | |
'compiledPath' => $config->app_volt->path, | |
'compiledExtension' => $config->app_volt->extension, | |
'compiledSeparator' => $config->app_volt->separator, | |
'compileAlways' => (bool) $config->app_volt->compileAlways, | |
'stat' => (bool) $config->app_volt->stat, | |
) | |
); | |
return $volt; | |
}; | |
/** | |
* Setup the view service | |
*/ | |
$this->di['view'] = function () use ($config, $di) { | |
$view = new PhView(); | |
$view->setViewsDir($config->app_path->views); | |
$view->registerEngines( | |
array('.volt' => 'volt') | |
); | |
return $view; | |
}; | |
} | |
/** | |
* Initializes the logger | |
* | |
* @param array $options | |
*/ | |
public function initLogger($options = array()) | |
{ | |
$config = $this->di['config']; | |
$this->di['logger'] = function () use ($config) { | |
$name = '/ndn-' . date('Ymd') . '.log'; | |
$logger = new PhLoggerFile( | |
$config->app_logger->path . $name | |
); | |
$formatter = new PhLoggerFormatter($config->app_logger->format); | |
$logger->setFormatter($formatter); | |
return $logger; | |
}; | |
} | |
/** | |
* Initializes the database and netadata adapter | |
* | |
* @param array $options | |
*/ | |
public function initDatabase($options = array()) | |
{ | |
$di = $this->di; | |
$config = $di['config']; | |
$debug = (isset($config->app_debug)) ? | |
(bool) $config->app_debug : | |
false; | |
$this->di['db'] = function () use ($debug, $config, $di) { | |
if ($debug) { | |
$logger = $di['logger']; | |
$eventsManager = new PhEventsManager(); | |
// Listen all the database events | |
$eventsManager->attach( | |
'db', | |
function ($event, $connection) use ($logger) { | |
if ($event->getType() == 'beforeQuery') { | |
$logger->log( | |
$connection->getSQLStatement(), | |
PhLogger::INFO | |
); | |
} | |
} | |
); | |
} | |
$params = array( | |
"host" => $config->app_db->host, | |
"username" => $config->app_db->username, | |
"password" => $config->app_db->password, | |
"dbname" => $config->app_db->name, | |
"charset" => 'utf8', | |
); | |
$conn = new PhMysql($params); | |
// Set everything to UTF8 | |
$conn->execute('SET NAMES UTF8', array()); | |
if ($debug) { | |
// Assign the eventsManager to the db adapter instance | |
$conn->setEventsManager($eventsManager); | |
} | |
return $conn; | |
}; | |
/** | |
* If the configuration specify the use of metadata adapter use it | |
* or use memory otherwise | |
*/ | |
$this->di['modelsMetadata'] = function () use ($config) { | |
return new PhMetadataFiles( | |
array('metaDataDir' => ROOT_PATH . $config->app_model->metadata) | |
); | |
}; | |
} | |
/** | |
* Initializes the session | |
* | |
* @param array $options | |
*/ | |
public function initSession($options = array()) | |
{ | |
$this->di['session'] = function () { | |
$session = new PhSession(); | |
$session->start(); | |
return $session; | |
}; | |
} | |
/** | |
* Initializes the cache | |
* | |
* @param array $options | |
*/ | |
public function initCache($options = array()) | |
{ | |
$config = $this->di['config']; | |
$this->di['cache'] = function () use ($config) { | |
// Get the parameters | |
$lifetime = $config->app_cache->lifetime; | |
$cacheDir = $config->app_cache->path; | |
$frontEndOptions = array('lifetime' => $lifetime); | |
$backEndOptions = array('cacheDir' => $cacheDir); | |
$frontCache = new PhCacheFront($frontEndOptions); | |
$cache = new PhCacheBack($frontCache, $backEndOptions); | |
return $cache; | |
}; | |
} | |
/** | |
* Initializes the Security component | |
* | |
* @param array $options | |
*/ | |
public function initSecurity($options = array()) | |
{ | |
$this->di['security'] = function () { | |
$security = new PhSecurity(); | |
$security->setWorkFactor(10); | |
return $security; | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
'app_version' => '2.0', | |
'app_env' => 'development', | |
'app_db' => array( | |
'adapter' => 'Mysql', | |
'host' => 'localhost', | |
'username' => 'root', | |
'password' => '', | |
'name' => 'mydb', | |
), | |
'app_name' => "NDN", | |
'app_baseUri' => '/', | |
'app_timezone' => 'US/Eastern', | |
'app_path' => array( | |
'library' => ROOT_PATH . '/api/library/', | |
'controllers' => ROOT_PATH . '/api/controllers/', | |
'models' => ROOT_PATH . '/api/models/', | |
'views' => ROOT_PATH . '/api/views/' | |
), | |
'app_debug' => '1', | |
'app_auth' => '0', | |
'app_volt' => array( | |
'path' => ROOT_PATH . '/api/var/volt/', | |
'extension' => '.php', | |
'separator' => '%%', | |
'compileAlways' => '1', | |
'stat' => '1', | |
), | |
'app_cache' => array( | |
'path' => ROOT_PATH . '/api/var/cache/', | |
'lifetime' => 3600, | |
), | |
'app_logger' => array( | |
'adapter' => 'File', | |
'path' => ROOT_PATH . '/api/var/logs/', | |
'format' => '[%date%][%type%] %message%', | |
), | |
'app_model' => array( | |
'metadata' => '/api/var/metadata/', | |
), | |
'app_routes' => array( | |
array( | |
'action' => 'add', | |
'route' => '/', | |
'params' => array( | |
'controller' => 'index', | |
'action' => 'errors', | |
'code' => 4004, | |
) | |
), | |
array( | |
'action' => 'addGet', | |
'route' => '/{version:[0-9\.]+}/:controller', | |
'params' => array( | |
'version' => 1, | |
'controller' => 2, | |
'action' => 'get' | |
) | |
), | |
array( | |
'action' => 'addPost', | |
'route' => '/{version:[0-9\.]+}/:controller', | |
'params' => array( | |
'version' => 1, | |
'controller' => 2, | |
'action' => 'post' | |
) | |
), | |
array( | |
'action' => 'addPut', | |
'route' => '/{version:[0-9\.]+}/:controller', | |
'params' => array( | |
'version' => 1, | |
'controller' => 2, | |
'action' => 'put' | |
) | |
), | |
array( | |
'action' => 'addDelete', | |
'route' => '/{version:[0-9\.]+}/:controller', | |
'params' => array( | |
'version' => 1, | |
'controller' => 2, | |
'action' => 'delete' | |
) | |
), | |
), | |
'app_css' => array( | |
'bootstrap' => array( | |
'prefix' => '//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/', | |
'file' => 'css/bootstrap-combined.min.css', | |
), | |
'bootstrap_responsive' => array( | |
'prefix' => '', | |
'file' => 'css/bootstrap-responsive.min.css', | |
), | |
'master' => array( | |
'prefix' => '', | |
'file' => 'css/master.css', | |
), | |
), | |
'app_js' => array( | |
'bootstrap' => array( | |
'prefix' => '//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/', | |
'file' => 'js/bootstrap.min.js', | |
), | |
) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
/** | |
* Adjust the dirname() below according to your path | |
*/ | |
if (!defined('ROOT_PATH')) { | |
define('ROOT_PATH', dirname(dirname(dirname(__FILE__)))); | |
} | |
// Using require once because I want to get the specific | |
// bootloader class here. The loader will be initialized | |
// in my bootstrap class | |
require_once ROOT_PATH . '/api/library/NDN/Bootstrap.php'; | |
$di = new \Phalcon\DI\FactoryDefault(); | |
$app = new \NDN\Bootstrap($di); | |
echo $app->run(array()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment