<?php
/**
* Bootstraps the application
*/
use Phalcon\DI\FactoryDefault as PhDi,
Phalcon\Config as PhConfig,
Phalcon\Session\Adapter\Files as PhSession,
Phalcon\Loader as PhLoader,
Phalcon\Mvc\Url as PhUrl,
Phalcon\Mvc\Router as PhRouter,
Phalcon\Db\Adapter\Pdo\Mysql as PhMysql,
Phalcon\Exception as PhException,
Phalcon\Mvc\Application as PhApplication,
Phalcon\Mvc\View as PhView,
Phalcon\Mvc\View\Engine\Volt as PhVolt,
Phalcon\Mvc\Model\Metadata\Memory as PhMetadataMemory,
Phalcon\Cache\Frontend\Output as PhCacheFront,
Phalcon\Cache\Backend\File as PhCacheBackFile,
Phalcon\Cache\Backend\Apc as PhCacheBackApc; 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(
'session',
'config',
'loader',
'url',
'router',
'view',
'cache',
); try { foreach ($loaders as $service) {
$function = 'init' . ucfirst($service);
$this->$function();
} $application = new PhApplication();
$application->setDI($this->di); return $application->handle()->getContent(); } catch (PhException $e) {
echo $e->getMessage();
} catch (\PDOException $e) {
echo $e->getMessage();
}
} // Protected functions
/**
* Initializes the session
*
* @param array $options
*/
protected function initSession($options = array())
{
$this->di['session'] = function () { $session = new PhSession();
$session->start(); return $session; };
} /**
* Initializes the config. Reads it from its location and
* stores it in the Di container for easier access
*
* @param array $options
*/
protected function initConfig($options = array())
{
$configFile = require(ROOT_PATH . '/app/var/config/config.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
*/
protected function initLoader($options = array())
{
$config = $this->di['config']; // Creates the autoloader
$loader = new PhLoader(); $loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir
)
); $loader->register(); // Dump it in the DI to reuse it
$this->di['loader'] = $loader;
} /**
* Initializes the baseUrl
*
* @param array $options
*/
protected 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->application->baseUri);
return $url;
};
} /**
* Initializes the router
*
* @param array $options
*/
protected function initRouter($options = array())
{
$config = $this->di['config']; $this->di['router'] = function () use ($config) { $router = new PhRouter(false); $router->notFound(
array(
"controller" => "index",
"action" => "notFound",
)
);
$router->removeExtraSlashes(true); foreach ($config['routes'] as $route => $items) {
$router->add($route, $items->params->toArray())
->setName($items->name);
} return $router;
};
} /**
* Initializes the database
*
* @param array $options
*/
protected function initDatabase($options = array())
{
$config = $this->di['config']; $this->di['db'] = function () use ($config) {
return new DbAdapter(
array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
)
);
};
} /**
* Initializes the models metadata
*
* @param array $options
*/
protected function initModelsMetadata($options = array())
{
$this->di['modelsMetadata'] = function () {
return new PhMetadataMemory();
};
} /**
* Initializes the view and Volt
*
* @param array $options
*/
protected function initView($options = array())
{
$config = $this->di['config'];
$di = $this->di; /**
* Setup the view service
*/
$this->di['view'] = function () use ($config, $di) { $view = new PhView();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(
array(
'.volt' => function ($view , $di) use ($config) {
$volt = new PhVolt($view , $di);
$voltOptions = array(
'compiledPath' => $config->application->voltDir ,
'compiledSeparator' => '_',
); if ('1' == $config->application->debug) {
$voltOptions['compileAlways'] = true;
} $volt->setOptions($voltOptions);
$volt->getCompiler()->addFunction(
'tr',
function ($key) {
return "Bootstrap::translate({$key})";
}
); return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php', // Generate Template files uses PHP itself as the template engine
)
); return $view;
};
} /**
* Initializes the cache
*
* @param array $options
*/
protected function initCache($options = array())
{
$config = $this->di['config']; $this->di['viewCache'] = function () use ($config) { // Get the parameters
$frontCache = new PhCacheFront(array('lifetime' => $config->cache->lifetime)); if (function_exists('apc_store')) {
$cache = new PhCacheBackApc($frontCache);
} else {
$backEndOptions = array('cacheDir' => $config->cache->cacheDir);
$cache = new PhCacheBackFile($frontCache, $backEndOptions);
} return $cache;
};
} /**
* Translates a string
*
* @return string
*/
public static function translate()
{
$return = '';
$messages = array();
$argCount = func_num_args();
$di = PhDi::getDefault();
$session = $di['session'];
$config = $di['config'];
$dispatcher = $di['dispatcher'];
$lang = $dispatcher->getParam('language'); if (function_exists('apc_store')) {
$phrases = apc_fetch($lang . '-phrases');
$language = apc_fetch($lang . '-language');
} else {
$phrases = $session->get('phrases');
$language = $session->get('language');
} $changed = false;
if (!$phrases || $language != $lang || ('1' == $config->application->debug)) { require ROOT_PATH . '/app/var/languages/en.php'; /**
* Messages comes from the above require statement. Not the best
* way of doing it but we need this for Transilex
*/
$english = $messages;
$phrases = $english;
if ('en' !== $lang) {
if (file_exists(ROOT_PATH . '/app/var/languages/' . $lang . '.php')) { /**
* Cleanup
*/
$messages = array();
require ROOT_PATH . '/app/var/languages/' . $lang . '.php'; /**
* Messages comes from the above require statement. Not
* the best way of doing it but we need this for Transilex
*/
$custom = $messages; foreach ($english as $key => $value) {
$phrases[$key] = (!empty($custom[$key])) ? $custom[$key] : $value;
}
} $changed = true;
} if ($changed) {
if (function_exists('apc_store')) {
apc_store($lang . '-phrases', $phrases);
apc_store($lang . '-language', $lang);
} else {
$session->set('phrases', $phrases);
$session->set('language', $lang);
}
} } // If parameters were passed process them, otherwise return an
// empty string
if ($argCount > 0) {
$arguments = func_get_args(); // The first argument is the key
$key = $arguments[0]; if (isset($phrases[$key])) {
$return = $phrases[$key]; // Any subsequent arguments need to replace placeholders
// in the target string. Unset the key and process the
// rest of the arguments one by one.
unset($arguments[0]); foreach ($arguments as $key => $argument) {
$return = str_replace(":{$key}:", $argument, $return);
}
}
} return $return;
}
}

Phalcon 的 bootstrap.php 自动加载完成;非常人性化的设计的更多相关文章

  1. ListView下拉刷新,上拉自动加载更多

    下拉刷新,Android中非常普遍的功能.为了方便便重写的ListView来实现下拉刷新,同时添加了上拉自动加载更多的功能.设计最初是参考开源中国的Android客户端源码.先看示例图.       ...

  2. Phalcon自动加载(PHP自动加载)

    自动加载(phalcon\Loader) 转载请注明来源 一.php文件引入 通过 include() 或 require() 函数,可以在PHP程序执行之前在该文件中插入一个文件的内容. 区别:处理 ...

  3. Composer概述及其自动加载探秘

    composer概述 一开始,最吸引我的当属 Composer 了,因为之前从没用过 Composer . Composer 是PHP中用来管理依赖关系的工具,你只需在自己的项目中声明所依赖的外部工具 ...

  4. Yii2的深入学习--自动加载机制

    Yii2 的自动加载分两部分,一部分是 Composer 的自动加载机制,另一部分是 Yii2 框架自身的自动加载机制. Composer自动加载 对于库的自动加载信息,Composer 生成了一个  ...

  5. Yii2的深入学习--自动加载机制(转)

    Yii2 的自动加载分两部分,一部分是 Composer 的自动加载机制,另一部分是 Yii2 框架自身的自动加载机制. Composer自动加载 对于库的自动加载信息,Composer 生成了一个  ...

  6. 在运行jar时自动加载指定的jar包

    初学Java的人经常遇到的一个问题是:如果一个程序依赖某个文件夹下的一堆jar包,那么启动它的时候就需要在java -cp参数后面一个一个的加上jar包的名称,很不方便. 比如主程序类叫Main,在目 ...

  7. Yii2之类自动加载

    在yii中,程序中需要使用到的类无需事先加载其类文件,在使用的时候才自动定位类文件位置并加载之,这么高效的运行方式得益于yii的类自动加载机制. Yii的类自动加载实际上使用的是PHP的类自动加载,所 ...

  8. Laravel 执行流程(一)之自动加载

    定位 从 public/index.php 定位到 bootstrap/autoload.php 从 bootstrap/autoload.php 定位到 vendor/autoload.php 从 ...

  9. 深入解析 composer 的自动加载原理 (转)

    深入解析 composer 的自动加载原理 转自:https://segmentfault.com/a/1190000014948542 前言 PHP 自5.3的版本之后,已经重焕新生,命名空间.性状 ...

随机推荐

  1. JavaScript引擎研究与C、C++与互调用(转)

    本文转自:ice6015的专栏.为什么有些招聘需要熟悉JS和C++,这或许就是原因. 1.  概要 JavaScript是一种广泛用于Web客户端开发的脚本语言,常用来控制浏览器的DOM树,给HTML ...

  2. 数据结构——N皇后放置方法种数

    Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合 ...

  3. SRM 506(2-1000pt)

    DIV2 1000pt 题意:一个由n*m的网格组成的棋盘,有四种点,'.'表示空点,'#'表示是墙不能走,'$'表示起点(同样是空点),'1'~'9'表示该点有复活时间为t的怪兽.每次,可以从一个点 ...

  4. 跳跃表Skip List【附java实现】

    skip list的原理 Java中的LinkedList是一种常见的链表结构,这种结构支持O(1)的随机插入及随机删除, 但它的查找复杂度比较糟糕,为O(n). 假如我们有一个有序链表如下,如果我们 ...

  5. I - Fire Game

    题目大意: 火焰游戏 在一个];][] = { {,},{,},{-,},{,-} };; i<M; i++)    ; j<N; j++)    {        )           ...

  6. Hibernate四 批量处理

    Hibernate批量处理一 批量插入将很多条记录插入数据库时,Hibernate通常会采用以下做法:public void test() { for(int i=0;i<1000;i++){ ...

  7. J2EE之WebLogic Server

    WebLogic是用于开发.集成.部署和管理大型分布式Web应用. 网络应用和数据库应 用的Java应用server. 将Java的动态功能和Java Enterprise标准的安全性引入大型网络应用 ...

  8. vim 学习相关记录

    VIM 相关内容****************** vim 的三个模式: 编辑模式 --> 输入模式 --> 末行模式 编辑模式: 通常键入键盘值被理解成一个操作; 如: dd(删除行) ...

  9. 我也要这样写define、、

    今天在TCO1B看到这位大神的代码,简直醉了,当需要手速的时候可以考虑使用一下.. #define V(x) vector<x > #define vs V(string) #define ...

  10. NPOI从数据库中导出数据到Excel

    首先要添加NPOI.dll程序集 https://yunpan.cn/cMeSTELJSXmJJ  访问密码 8d83 把里面的程序集都添加到引用里 下面的代码是从数据库导出到Excel { //pa ...