Phalcon Framework 1.3.4

Phalcon\Exception: 数据库连接失败

/app/wwwroot/api/framework/Application.php (521)
#0Joy\Application->Joy\{closure}()
#1Phalcon\DI\Service->resolve(null, Object(Phalcon\DI\FactoryDefault))
#2Phalcon\DI->getShared(dbCmsRead)
#3Phalcon\Mvc\Model\Manager->getReadConnection(Object(Joy\Common\Models\Basic\SiteMenu: 11))
#4Phalcon\Mvc\Model->getReadConnection()
#5Phalcon\Mvc\Model\Query->_executeSelect(Array([models] => Array([0] => Joy\Common\Models\Basic\SiteMenu), [tables] => Array([0] => site_menu), [columns] => Array([joy\Common\Models\Basic\SiteMenu] => Array([type] => object, [model] => Joy\Common\Models\Basic\SiteMenu, [column] => site_menu, [balias] => joy\Common\Models\Basic\SiteMenu)), [where] => Array([type] => binary-op, [op] => &, [left] => Array([type] => qualified, [domain] => site_menu, [name] => state, [balias] => state), [right] => Array([type] => literal, [value] => 2)), [order] => Array([0] => Array([0] => Array(), [1] => ASC))), null, null)
#6Phalcon\Mvc\Model\Query->execute(null, null)
#7Phalcon\Mvc\Model::find(Array([conditions] => state&2, [order] => id ASC))
<?php
 
namespace Joy\Common\Models\Basic;
 
class SiteMenu extends \Joy\Common\Models\CmsBaseModel
{
 
    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer",nullable=false,column='id')
     */
    public $id;
 
    /**
     *
     * @var integer
     * @Column(type="integer",nullable=false,column='pid')
     */
    public $pid;
 
    /**
     *
     * @var string
     * @Column(type="string",nullable=false,column='relation')
     */
    public $relation;
 
    /**
     *
     * @var string
     * @Column(type="string",nullable=false,column='title')
     */
    public $title;
 
    /**
     *
     * @var string
     * @Column(type="string",nullable=true,column='url')
     */
    public $url;
 
    /**
     *
     * @var integer
     * @Column(type="integer",nullable=false,column='state')
     */
    public $state;
 
    /**
     *
     * @var integer
     * @Column(type="integer",nullable=false,column='sort')
     */
    public $sort;
 
    /**
     *
     * @var integer
     * @Column(type="integer",nullable=false,column='muid')
     */
    public $muid;
 
    /**
     *
     * @var integer
     * @Column(type="integer",nullable=false,column='mtime')
     */
    public $mtime;
    
    /**
     * Method to set the value of field id
     *
     * @param integer $id
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
 
        return $this;
    }
 
    /**
     * Method to set the value of field pid
     *
     * @param integer $pid
     * @return $this
     */
    public function setPid($pid)
    {
        $this->pid = $pid;
 
        return $this;
    }
 
    /**
     * Method to set the value of field relation
     *
     * @param string $relation
     * @return $this
     */
    public function setRelation($relation)
    {
        $this->relation = $relation;
 
        return $this;
    }
 
    /**
     * Method to set the value of field title
     *
     * @param string $title
     * @return $this
     */
    public function setTitle($title)
    {
        $this->title = $title;
 
        return $this;
    }
 
    /**
     * Method to set the value of field url
     *
     * @param string $url
     * @return $this
     */
    public function setUrl($url)
    {
        $this->url = $url;
 
        return $this;
    }
 
    /**
     * Method to set the value of field state
     *
     * @param integer $state
     * @return $this
     */
    public function setState($state)
    {
        $this->state = $state;
 
        return $this;
    }
 
    /**
     * Method to set the value of field sort
     *
     * @param integer $sort
     * @return $this
     */
    public function setSort($sort)
    {
        $this->sort = $sort;
 
        return $this;
    }
 
    /**
     * Method to set the value of field muid
     *
     * @param integer $muid
     * @return $this
     */
    public function setMuid($muid)
    {
        $this->muid = $muid;
 
        return $this;
    }
 
    /**
     * Method to set the value of field mtime
     *
     * @param integer $mtime
     * @return $this
     */
    public function setMtime($mtime)
    {
        $this->mtime = $mtime;
 
        return $this;
    }
 
    /**
     * Returns the value of field id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
 
    /**
     * Returns the value of field pid
     *
     * @return integer
     */
    public function getPid()
    {
        return $this->pid;
    }
 
    /**
     * Returns the value of field relation
     *
     * @return string
     */
    public function getRelation()
    {
        return $this->relation;
    }
 
    /**
     * Returns the value of field title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }
 
    /**
     * Returns the value of field url
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }
 
    /**
     * Returns the value of field state
     *
     * @return integer
     */
    public function getState()
    {
        return $this->state;
    }
 
    /**
     * Returns the value of field sort
     *
     * @return integer
     */
    public function getSort()
    {
        return $this->sort;
    }
 
    /**
     * Returns the value of field muid
     *
     * @return integer
     */
    public function getMuid()
    {
        return $this->muid;
    }
 
    /**
     * Returns the value of field mtime
     *
     * @return integer
     */
    public function getMtime()
    {
        return $this->mtime;
    }
 
    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'site_menu';
    }
 
    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return SiteMenu[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }
 
    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return SiteMenu
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }
 
}
#8Joy\Common\Models\Basic\SiteMenu::find(Array([conditions] => state&2, [order] => id ASC))
<?php
namespace Joy\Common\Daos;
use Joy\Common\Models\Basic\SiteMenu;
 
/**
 * @desc 业务处理类的基类
 * @author hws
 * @version 2015-06-23
 */
 
class SiteMenuD{
    
    /**
     * @desc 获取地图信息数组
     * @author hws
     * @version 2015-06-23
     */
    public static function getSiteMenu($conditions = array()){
        $siteMenu = SiteMenu::find($conditions);
        $siteMenuArr = array();
        foreach($siteMenu as $menu){
            $siteMenuArr[] = array(
                'id'  => $menu->id,
                'pid' => $menu->pid,
                'irelationd' => $menu->relation,
                'title' => $menu->title,
                'url' => $menu->url,
                'state' => $menu->state,
                'sort' => $menu->sort,
                'muid' => $menu->muid,
                'mtime' => $menu->mtime
            );
        }
        return $siteMenuArr;
    }
  
   /**
   * @desc 组装数据
   * @author hws
   * @version 2015-06-23
   */
  public static function format($menus, $parent_id = 0)
    {
        $_menus = array();
        foreach ($menus as $k => $menu) {
            if ($parent_id == $menu['pid']) {
                $_menus[$menu['id']] = $menu;
                $_menus[$menu['id']]['child'] = self::format($menus, $menu['id']);
            }
        }
        return $_menus;
    }
}
#9Joy\Common\Daos\SiteMenuD::getSiteMenu(Array([conditions] => state&2, [order] => id ASC))
<?php
namespace Joy\Website\Controllers;
use Joy\Common\Models\Basic\SiteMenu;
use Joy\Common\Daos\SiteMenuD;
use Joy\Common\Api\Area;
 
/**
 * User: hws
 * Date: 15-08-17
 * Time: 上午11:00
 * Desc: 网站地图
 */
class SitemapController extends Controller
{
    public $cityMapKey = 'memcache_city_map';
    public $sectionsMapKey = 'memcache_sections_map';
    public $hospitalMapKey = 'memcache_hospital_map';
    
    public function onConstruct()
    {
        $this->view->title = "网站地图";
        $this->view->plate = "网站地图";
    }
    
    /**
     * 网站地图列表
     * url http://www.91160.com/v5/sitemap.html
     */
    public function indexAction(){
        $siteMenuArr = SiteMenuD::getSiteMenu(array('conditions'=>'state&2','order'=>'id ASC'));
 
        if($siteMenuArr){
            $siteMap = SiteMenuD::format($siteMenuArr);
        }
        $this->view->site_map = $siteMap;
    }
    
    /**
     * 城市地图列表
     * url http://www.91160.com/v5/citymap.html
     */
    public function cityMapAction(){
        $this->view->title = "城市_网站地图";
        $cityInfo = $this->cache->get($this->cityMapKey);
        $cityInfo = json_decode($cityInfo, true);
        
        if(!$cityInfo){
            $area = new Area();
            $cityInfo = $area->getProCityList();
            if(isset($cityInfo[0])){
                unset($cityInfo[0]);
            }
            $this->cache->save($this->cityMapKey, json_encode($cityInfo), 3600*24);
        }
        
        $this->view->cityinfo = $cityInfo;
    }
    
    /**
     * 科室地图列表
     * url http://www.91160.com/v5/sectionsmap.html
     */
    public function sectionsMapAction(){
        $this->view->title = "科室_网站地图";
        $sectionsInfo = $this->cache->get($this->sectionsMapKey);
        $sectionsInfo = json_decode($sectionsInfo, true);
        
        if(!$sectionsInfo){
            $area = new Area();
            $area->cityid = 5;
            $sectionsInfo = $area->findPubList();
            $this->cache->save($this->sectionsMapKey, json_encode($sectionsInfo), 3600*24);
        }
        
        $this->view->sectionsInfo = $sectionsInfo;
    }
 
    /**
     * 医院地图列表
     * url http://www.91160.com/v5/hospitalmap.html
     */
    public function hospitalMapAction(){
        $this->view->title = "医院_网站地图";
        $new_hospital = $this->cache->get($this->hospitalMapKey);
        $new_hospital = json_decode($new_hospital, true);
        
        if(!$new_hospital){
            $area = new Area();
            $hospitalInfo = $area->getProHospitalList();
            
            $new_hospital = array();
            foreach($hospitalInfo as $key=>$infos){
                foreach($infos as $pkey=>$info){
                    $tmp = explode('|',$pkey);
                    $new_hospital[$key][$tmp[0]] = array(
                        'unit_name' => $tmp[1],
                        'num' => $info
                    );
                }
            }
            $this->cache->save($this->hospitalMapKey, json_encode($new_hospital), 3600*24);
        }
        
        $this->view->hospitalinfo = $new_hospital;
    }
}
#10Joy\Website\Controllers\SitemapController->indexAction()
#11Phalcon\Dispatcher->dispatch()
#12Phalcon\Mvc\Application->handle()
<?php
namespace Joy;
 
use Phalcon\Logger, Phalcon\Logger\Formatter\Line as LineFormatter, Phalcon\Exception as Exception, Phalcon\Logger\Multiple as MultipleStream;
use Phalcon\Net\Http;
use Phalcon\Security\Encrypter;
 
/**
 *
 * @author dancebear
 *
 * @property \Phalcon\Cache\BackendInterface $cache
 * @property \Phalcon\Security $security
 *
 */
class Application extends \Phalcon\Mvc\Application
{
 
    /**
     *
     * @var string
     */
    protected $_basePath;
 
    /**
     *
     * @var string
     */
    protected $_runtimePath;
 
    /**
     * 日志组件
     *
     * @var \Phalcon\Logger\AdapterInterface
     */
    protected $_logger;
 
    /**
     * 应用程序的命名空间
     *
     * @var string
     */
    public $defaultNamespace;
 
    /**
     * 配置文件目录
     *
     * @var string
     */
    protected $_configPath;
 
    /**
     *
     * @var string 终端用户所使用的字符集,推荐使用
     *      [IETF language tags](http://en.wikipedia.org/wiki/IETF_language_tag),例如:`en`代表英语。
     *      `en-US`代表英语(美国).
     * @see sourceLanguage
     */
    public $language = 'zh-CN';
 
    /**
     *
     * @var string 应用程序中所使用的语言
     *      这主要指的是信息和视图文件所使用的语言。
     * @see language
     */
    public $sourceLanguage = 'en-US';
 
    /**
     *
     * @param \Phalcon\DiInterface $di
     */
    public function __construct($di = null)
    {
        parent::setDI(\Joy::$di);
        \Joy::$app = $this;
    }
 
    /**
     * (non-PHPdoc)
     *
     * @see \Phalcon\DI\Injectable::__get()
     */
    public function __get($property)
    {
        if (method_exists($this, 'get' . $property))
            return $this->{'get' . ucfirst($property)}();
        if ($this->_dependencyInjector->has($property)) {
            return $this->_dependencyInjector->getShared($property);
        }
    }
 
    /**
     * 加载通过Composer安装的库
     */
    protected function loadComposerLib()
    {
        /**
         * load composer lib
         */
        if (file_exists($this->_basePath . '/../vendor/autoload.php'))
            require ($this->_basePath . '/../vendor/autoload.php');
    }
 
    /**
     * 加载配置文件
     *
     * @param string|\Phalcon\Config|array $file
     *            配置文件路径,此文件必须返回一个配置信息数组
     * @return \Joy\Application
     */
    public function configure($file)
    {
        if (! is_string($file)) {
            $configData = $file;
            $this->_configPath = null;
        } else {
            if (! file_exists($file)) {
                throw new Exception('The configuration file unfound:'.$file);
            }
            /**
             * Read the configuration
             */
            $configData = include ($file);
            $this->_configPath = basename($file);
        }
        $config = new \Phalcon\Config($configData);
        \Joy::$config = $config;
        $this->defaultNamespace = $config->defaultNamespace;
        $this->setBasePath($config->basePath);
        $this->setRuntimePath($config->runtimePath);
        return $this;
    }
 
    /**
     * 设置应用程序根目录
     *
     * @param string $path
     */
    public function setBasePath($path)
    {
        $this->_basePath = $path;
    }
 
    /**
     * 返回应用程序根目录
     *
     * @return string
     */
    public function getBasePath()
    {
        return $this->_basePath;
    }
 
    /**
     * 获取应用程序的模块目录
     *
     * @return string 应用程序模块目录。默认为{@link basePath}的子目录`Modules`。
     */
    public function getModulePath()
    {
        if ($this->_modulePath !== null)
            return $this->_modulePath;
        else
            return $this->_modulePath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'Modules';
    }
 
    /**
     * 设置应用程序的模块目录
     *
     * @param string $value
     *            应用程序的模块目录
     * @throws Exception 无效的模块目录
     */
    public function setModulePath($value)
    {
        if (($this->_modulePath = realpath($value)) === false || ! is_dir($this->_modulePath))
            throw new Exception(\Joy::t('Joy', 'The module path "{path}" is not a valid directory.', array(
                '{path}' => $value
            )));
    }
 
    /**
     * 设置运行时应用程序目录
     *
     * @param string $path
     */
    public function setRuntimePath($path)
    {
        $this->_runtimePath = $path;
    }
 
    /**
     * 取得运行时文件目录
     *
     * @return string
     */
    public function getRuntimePath()
    {
        return $this->_runtimePath;
    }
 
    /**
     * 设置应用程序日志组件;日志组件允许同时使用多种日志输出方式
     *
     * @link http://docs.phalconphp.com/en/latest/reference/logging.html#adapters
     * @see \Phalcon\Logger\Multiple
     * @see \Phalcon\Logger\Adapter\File
     *
     * @throws Exception
     */
    protected function setLogger()
    {
        \Joy::$di->set('logger', function ()
        {
            $logger = new MultipleStream();
            // get environment variables
            $hostName = getenv('host');
            if ($hostName === false) {
                $logger->log('environment variables "host" for logger is not exists', Logger::ERROR);
                // throw new Exception('environment variables "host" for logger is not exists');
                $hostName = 'localhost';
            }
            foreach (\Joy::$config->components->logger as $adapter => $loggerConfig) {
                $adapter = '\Phalcon\Logger\Adapter\\' . $adapter;
                $_config = isset($loggerConfig[1]) ? $loggerConfig[1] : null;
                $_logger = new $adapter($loggerConfig[0], $_config);
                $logger->push($_logger);
            }
            $formatter = new LineFormatter($hostName . '-%date% - %message%');
            $logger->setFormatter($formatter);
            return $logger;
        });
    }
 
    /**
     * 设置应用程序的控制器模块信息
     * 系统会依据配置文件中的modules区块来启用相应的模块。
     * 以下是一个相应配置的实例:
     * ```
     * return [
     * 'components' => [
     * ],
     * 'modules' => [
     * 'User'
     * ]
     * ]
     * ```
     * 上述例子启用了一个名为User的模块,相应的程序目录为:
     * ``app/Modules/User``
     * 在此目录下包含相应的``Controllers`` ``Models`` ``Views``目录。
     * User目录的下还可以通过一个名为``Module.php``的文件来继承``Joy\Web\Module``类来实现模块的一些自定义设置。
     */
    protected function setModules()
    {
        $namespaces = [];
        foreach ((array) \Joy::$config->modules as $module) {
            $moduleClassFile = $this->getModulePath() . DIRECTORY_SEPARATOR . $module . '/Module.php';
            $moduleClass = $this->defaultNamespace . '\\' . $module . '\Module';
            if (! file_exists($moduleClassFile)) {
                $moduleClassFile = JOY_PATH . '/Web/Module.php';
                $moduleClass = 'Joy\Web\Module';
            }
            $modules[$module] = array(
                'className' => $moduleClass,
                'path' => realpath($moduleClassFile),
                'moduleName' => $module
            );
            $namespaces[$this->defaultNamespace . '\\' . $module] = $this->getModulePath() . DIRECTORY_SEPARATOR . $module;
        }
        $namespaces[$this->defaultNamespace . '\\Controllers'] = $this->getBasePath() . '/Controllers';
        $namespaces[$this->defaultNamespace . '\\Models'] = $this->getBasePath() . '/Models';
        \Joy::$di->get('loader')->registerNamespaces($namespaces, true);
    }
 
    /**
     * 设置系统的事件管理器
     * 这里将实例化一个``\Phalcon\Events\Manager``类,并将它注册微系统的默认事件管理器。
     */
    protected function setEvents()
    {
        $this->setEventsManager(new \Phalcon\Events\Manager());
    }
 
    /**
     * 设置找不到页面的事件
     */
    protected function setNotFound()
    {
        $self = &$this;
        \Joy::$di->set('dispatcher', function () use($self) {
            $eventsManager = new \Phalcon\Events\Manager();
            // Attach a listener
            $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) use($self) {
 
                // Handle 404 exceptions
                if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
                    $self->sendErrorPage();
                }
                // Alternative way, controller or action doesn't exist
                if ($event->getType() == 'beforeException') {
                    switch ($exception->getCode()) {
                        case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                        case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                        $self->sendErrorPage();
                    }
                }
            });
            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            // Bind the EventsManager to the dispatcher
            $dispatcher->setEventsManager($eventsManager);
            return $dispatcher;
        }, true);
    }
 
    /**
     * 输出错误信息或错误页面
     */
    private function sendErrorPage()
    {
        $config = \Joy::$config;
        $config = $config->toArray();
        http_response_code(404);
        if ($config['render'] == 'json') {
            $this->response->setJsonContent([
                'status' => 404,
                'message' => 'page not found'
            ])->send();
        } else {
            $errorPage = JOY_PATH . '/Web/error/404.php';
            if (isset($config['error']['404'])) {
                $custom = ROOT_PATH . $config['error']['404'];
                if (file_exists($custom))
                    $errorPage = $custom;
            }
            include_once $errorPage;
        }
        exit();
    }
    /**
     * 初始化系统的视图组件,支持直接使用PHP模板和volt模板引擎
     *
     * @return \Phalcon\Mvc\View
     */
    protected function setView()
    {
        /**
         * Setting up the view component
         */
        \Joy::$di->set('view', function ()
        {
            $view = new \Phalcon\Mvc\View();
            $volt = new \Phalcon\Mvc\View\Engine\Volt($view, \Joy::$di);
            $php = new \Phalcon\Mvc\View\Engine\Php($view, \Joy::$di);
            $config = \Joy::$config->application->views;
            $volt->setOptions([
                "compiledPath" => function ($templatePath) use($config)
                {
                    $templatePath = str_replace(DIRECTORY_SEPARATOR, $config->compiledSeparator, str_replace(realpath(\Joy::$config->basePath), '', realpath($templatePath)));
                    return $config->compiledPath . '/' . $templatePath . $config->compiledExtension;
                },
 
                // 'compiledExtension' => $config->compiledExtension,
                // 'compiledSeparator' => $config->compiledSeparator,
                'compileAlways' => JOY_DEBUG && $config->compileAlways
            ]);
 
            $view->setViewsDir($config->dir);
            $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT);
 
            $view->registerEngines([
                ".volt" => $volt,
                '.php' => $php
            ]);
 
            if (JOY_DEBUG) {
                $em = $this->getEventsManager();
                $em->attach("view", function ($event, $view)
                {
                    if ($event->getType() == 'beforeRender') {
                        if (! $view->renderStart)
                            $view->renderStart = microtime(true);
                    }
                    if ($event->getType() == 'afterRender') {
                        $output = $view->getContent();
                        $timestamp = microtime(true);
                        $viewTime = round($timestamp - $view->renderStart, 3);
                        $totleTime = round($timestamp - JOY_BEGIN_TIME, 3);
                        if (\Joy::$di->has('profiler')) {
                            /**
                             *
                             * @var \Phalcon\Db\Profiler $profiler
                             */
                            $dbProfiler = \Joy::$di->get('profiler');
                            $querys = 'Total ' . $dbProfiler->getNumberTotalStatements() . ' query';
                            $costs = 'Cost ' . round($dbProfiler->getTotalElapsedSeconds(), 3) . 's';
                        } else {
                            $querys = '';
                            $costs = '';
                        }
                        $output = strtr($output, [
                            '<!-- {total} -->' => 'Page:' . $totleTime . 's',
                            '<!-- {view} -->' => 'View:' . $viewTime . 's',
                            '<!-- {querys} -->' => $querys,
                            '<!-- {costs} -->' => $costs
                        ]);
                        $view->setContent($output);
                        // $di->get('logger')->log('渲染客户端js、css结束'.json_encode($view));
                        return $view;
                    }
                    if ($event->getType() == 'notFoundView') {
                        throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"');
                    }
                });
                $view->setEventsManager($em);
            }
 
            return $view;
        });
    }
 
    /**
     * 设置数据库事件探查器
     *
     * @return \Phalcon\Db\Profiler
     */
    protected function setProfile()
    {
        \Joy::$di->set('profiler', function ()
        {
            return new \Phalcon\Db\Profiler();
        }, true);
    }
 
    /**
     * 根据配置文件信息添加mongo数据库实例
     */
    protected function setMongo()
    {
        if (\Joy::$config->components->offsetExists('mongo')) {
            $config = \Joy::$config->components->mongo->toArray();
        } else {
            $config = [];
        }
        if (count($config) > 0) {
            \Joy::$di->set(
                'mongo',
                function () use ($config) {
                    $collection = $config['username'] . ':' . $config['password'] . '@' . $config['host'];
                    $options = [
                        'connectTimeoutMS' => 1000
                    ];
                    if (strpos($config['host'], ",") > 0) { //集群情况,需增加参数
                        $options['replicaSet'] = 'rep';
                        $options['readPreference'] = 'nearest';
                    }
                    //$mongo = new \Phalcon\Db\Adapter\Mongo\Client("mongodb://".$collection,$options);
                    $mongo = new \Phalcon\Db\Adapter\Mongo\Client("mongodb://" . $collection . ($config['dbname'] ? '/' . $config['dbname'] : ''), $options);
                    return $mongo->selectDB($config['dbname']);
                },
                true
            );
        }
    }
 
    /**
     * 根据配置文件信息添加数据库实例
     */
    protected function setDatabase()
    {
        $config = \Joy::$config->components->database;
        if ($config->count() > 0) {
            foreach ($config as $name => $database) {
                $this->addDatabase($name, $database);
            }
        }
    }
 
    /**
     * 设置数据库连接
     * 请在配置文件的components的database小节下添加
     * ```
     * 'db' => [
     * 'adapter' => 'Mysql',
     * 'host' => 'localhost',
     * 'username' => 'root',
     * 'password' => 'root',
     * 'dbname' => 'tool',
     * 'options' => [
     * PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
     * ],
     * ]
     * ```
     * 其内容依据使用的数据库类型不同而有所区别,具体请参考(@link http://docs.phalconphp.com/en/latest/reference/db.html)
     * 他会自动添加一个名为db的数据库实例;使用方法示例:
     * ```
     * $db=\Joy::$di->get('db');
     * ```
     *
     * @param string $name
     * @param \Phalcon\Config $config
     * @return \Phalcon\Db\AdapterInterface
     */
    public function addDatabase($name, \Phalcon\Config $config)
    {
        /**
         * Database connection is created based in the parameters defined in the configuration file
         */
        \Joy::$di->set($name, function () use($config,$name)
        {
 
            $eventsManager = $this->getEventsManager();
            $adapter = '\Phalcon\Db\Adapter\Pdo\\' . $config->adapter;
            /**
             * @var $connection \Phalcon\Db\Adapter
             */
            try{
                $connection = new $adapter($config->toArray());
            }catch (\Exception $exception){
                $this->addMonitor($exception, $config->adapter, $config->host, $config->dbname);
                throw new Exception('数据库连接失败');
                $connection = null;
            }
 
            // 设置SQL优化日志
            if (JOY_DEBUG || (defined('JOY_DB_DEBUG') && JOY_DB_DEBUG)) {
                $logFile = $this->getRuntimePath() . '/logs/';
                if(!is_dir($logFile)){
                    mkdir($logFile, 0777, true);
                }
                // Attach logger & profiler
                $logger = new \Phalcon\Logger\Adapter\File($logFile.$name.'.log');
                /**
                 * @var $profiler \Phalcon\Db\Profiler
                 */
                $profiler = \Joy::$di->getProfiler();
 
                $eventsManager->attach($name, function ($event, $connection) use($logger, $profiler)
                {
                    if ($event->getType() == 'beforeQuery') {
                        $statement = $connection->getSQLStatement();
                        $profiler->startProfile($statement);
                    }
                    if ($event->getType() == 'afterQuery') {
                        // Stop the active profile
                        $profiler->stopProfile();
                        $last = $profiler->getLastProfile();
                        $logger->log($last->getSQLStatement()."\t\t" .json_encode($last->getSQLVariables())."\t\t" .$last->getTotalElapsedSeconds(), \Phalcon\Logger::DEBUG);
                    }
                });
                $connection->setEventsManager($eventsManager);
            }
            return $connection;
        });
    }
 
    /**
     * 设置数据库元数据的缓存
     * 支持以下的元数据存储方式:
     * Memory、Memcache、Redis
     *
     * @return \Phalcon\Mvc\Model\MetaData\Memory
     */
    protected function setMetaData()
    {
        \Joy::$di['modelsMetadata'] = function ()
        {
            /* @var $config \Phalcon\Config  */
            $config = \Joy::$config->components->metadata;
            $adapter = '\Phalcon\Mvc\Model\MetaData\\' . $config->get('adapter', 'Memory');
            $configDefault = new \Phalcon\Config([
                "lifetime" => 86400,
                "prefix" => "my-prefix"
            ]);
            // Instantiate a meta-data adapter
            $metaData = new $adapter($configDefault->merge($config));
 
            // Set a custom meta-data database introspection
            // 此处是设置支持字段别名
            if(\Phalcon\Version::get()>'2.0'){
              $Annotations = '\Phalcon\Mvc\Model\MetaData\Strategy\Annotations';
            }else{
              $Annotations = '\Phalcon\Mvc\Model\MetaData\Strategy\AnnotationsMetaData';
            }
 
            $metaData->setStrategy(new $Annotations());
 
            return $metaData;
        };
    }
 
    /**
     * 设置全局的模型管理器
     *
     * @return \Phalcon\Mvc\Model\Manager
     */
    protected function setModelsManager()
    {
        \Joy::$di->setShared('modelsManager', function ()
        {
            $eventsManager = new \Phalcon\Events\Manager();
            $modelsManager = new \Phalcon\Mvc\Model\Manager();
            $modelsManager->setEventsManager($eventsManager);
            return $modelsManager;
        });
    }
 
    /**
     * 设置缓存组件
     * 缓存的配置信息如下:
     * ```
     * 'cache' =>[
     * 'adapter' => 'Memcache',
     * 'host' => '127.0.0.1',
     * 'port' => 11211,
     * 'persistent' => 1
     * ]
     * ```
     * 如果您需要使用多台memcache服务器,请使用``Libmemcached``组件
     * 我们的系统支持下列组件:
     * ``File``、``Memcached``、``APC``、``Mongo``、``XCache``、``Redis``
     * 同时,我们还可以很方便的扩展更多的组件。关于各组件的配置方式请参考:
     * [@link http://docs.phalconphp.com/en/latest/reference/cache.html#backend-adapters cache]
     * 其中libMemcached与memcache的配置有所区别,请务必注意。
     * 如果需要配置多个缓存组件,请使用如下格式的配置文件:
     * ```
     * 'cache'=>[
     * [
     * [
     * ],
     * 'redis'=>[
     * ],
     * 'Mongo' => [
     * ]
     * ]
     * ]
     * ```
     * 数组的键将会被作为缓存的组件名的一部分,其中这个数组的KEY为0的初始化后的组件将被
     * 作为默认的缓存使用。缓存组件名的格式为:
     * ``cache_[[key]]``。
     */
    protected function setCache()
    {
        if (\Joy::$config->components->offsetExists('cache')) {
            $config = \Joy::$config->components->cache->toArray();
        } else {
            $config = [];
        }
        if (count($config['backend']) > 0) {
            $adapter = '\Phalcon\Cache\Frontend\\' . $config['frontend'];
            $frontCache = new $adapter(array(
                "lifetime" => 172800
            ));
            foreach ($config['backend'] as $key => $_config) {
                $adapter = '\Phalcon\Cache\Backend\\' . $_config['adapter'];
                unset($_config['adapter']);
                $cache = new $adapter($frontCache, $_config);
                $cacheKey = 'cache';
                if ($key !== 0) {
                    $cacheKey = 'cache_' . $key;
                }
                \Joy::$di->set($cacheKey, $cache);
            }
        }
    }
 
    /**
     * 设置密码安全及CSRF组件
     *
     * @return \Phalcon\Security
     */
    public function setSecurity()
    {
        \Joy::$di->set('security', function ()
        {
 
            $security = new \Phalcon\Security();
            $workFactor = 8;
            $config = \Joy::$config->components->toArray();
            if (isset($config['security']) && isset($config['security']['workFactor']))
                $workFactor = $config['security']['workFactor'];
                // Set the password hashing factor to 12 rounds
            $security->setWorkFactor($workFactor);
 
            return $security;
        }, true);
    }
 
    /**
     * 注册数据加密/解密服务
     */
    public function setEncrypter()
    {
        \Joy::$di->set('encrypter', function() {
            return new Encrypter();
        }, true);
    }
 
    /**
     * 注册数据加密/解密服务
     */
    public function setPhpcat()
    {
        if (\Joy::$config->components->offsetExists('phpcat')) {
            \Joy::$di->set('phpcat', function() {
                $config = \Joy::$config->components->phpcat->toArray();
                return new \PhpCat\PhpCat($config['domain'],$config['servers']);
            }, true);
        }
    }
 
 
    /**
     * 初始化系统组件
     * debug模式时会使用Phalcon\Debug进行调试
     * 否则会输出错误页面
     *
     * @throws \Phalcon\Exception
     * @return \Joy\Application
     */
    public function init()
    {
        if (JOY_DEBUG) {
            $debug = new \Phalcon\Debug();
        } else {
            $debug = new Debug();
            $debug->setView(\Joy::$config->errorPage);
        }
        $debug->setUri(JOY_DEBUG_URI);
        $debug->listen();
        $this->setPhpcat(); //设置PHP的cat监控
        $this->setEvents();
        $this->setLogger();
        $this->loadCustomNamespaces();
        $this->setModules();
        $this->setView();
        $this->setMongo();
        $this->setProfile();
        $this->setNotFound();
        $this->setDatabase();
        $this->setCache();
        $this->setModelsManager();
        $this->setMetaData();
        $this->setSecurity();
        $this->setEncrypter();  //设置数据的加密和解密方法
        return $this;
    }
 
    /**
     * 执行应用程序并处理异常
     */
    public function run()
    {
      $stage = getenv('APPLICATION_STAGE') ? getenv('APPLICATION_STAGE') : '';
      if(is_file(ROOT_PATH . "/config/web.{$stage}.php")){
        $stage_config = ROOT_PATH . "/config/web.{$stage}.php";
      }else{
        $stage_config = ROOT_PATH . "/config/web.php";
      }
        echo $this->configure($stage_config)
            ->init()
            ->handle()
            ->getContent();
    }
 
    /**
     * 输出debug信息
     *
     * @param string $message
     * @param string $context
     */
    public function debug($message, $context = null)
    {
        if ($this->_logger == null) {
            $this->_logger = \Joy::$di->get('logger');
        }
        $this->_logger->debug($message, $context);
    }
 
    /**
     * 加载自定义命名空间
     */
    protected function loadCustomNamespaces()
    {
        $custom_namespaces = \Joy::$config->offsetExists('customNamespaces') ? \Joy::$config->customNamespaces->toArray() : '';
        if($custom_namespaces){
            \Joy::$di->get('loader')->registerNamespaces($custom_namespaces, true);
        }
    }
 
    /**
     * 添加日志
     * @param $resourceType 资源类型: 数据库,Memcache, Redis , 文件 ,日志服务 其它
     * @param $host  主机IP
     * @param $dbName  哪个数据库
     */
    protected function addMonitor($exception, $resourceType, $host, $dbName){
        if (\Joy::$config->components->offsetExists('phpcat')) {
            \Joy::$di->get("phpcat")->logException("Error", "database.".$resourceType.".connect.error", $exception);
        }
        $components = \Joy::$config->components->toArray();
        if(empty($components['monitor'])){
            return true;
        }
        $http = new  Http();
        $http->timeOut = 1;
        $data = [
            'channel_id'=>$components['monitor']['channelId'],
            'business_type'=>$components['monitor']['businessType'],
            'log_level' => 'ERROR',
            'position' => $this->router->getControllerName().'_'.$this->router->getActionName(),
            'msg' => $resourceType.'-'.$dbName.'连接服务器'. $host.'失败'
        ];
        $result = $http->post($components['monitor']['apiUrl'], $data);
        if($result['status']==2){
            return false;
        }
        return true;
    }
}
#13Joy\Application->run()
<?php
/**
 * @desc phalcon框架入口 
 */
header("Content-type: text/html; charset=utf-8");
/**
* 网页入口文件
 */
$stage = getenv('APPLICATION_STAGE') ? getenv('APPLICATION_STAGE') : 'development';
if ($stage == 'production') {
    defined('JOY_DEBUG') or define('JOY_DEBUG', false);
} else {
    defined('JOY_DEBUG') or define('JOY_DEBUG', true);
}
defined('JIUYI_ROOT_PATH') or define('JIUYI_ROOT_PATH', dirname(__FILE__));//91160网站根目录
defined('ROOT_PATH') or define('ROOT_PATH', JIUYI_ROOT_PATH . '/../../91160CMS');//网站根目录
defined('JYBASE_PATH') OR define('JYBASE_PATH', JIUYI_ROOT_PATH . '/../JYBase');//JYBase目录
defined('VENDOR_PATH') OR define('VENDOR_PATH', JIUYI_ROOT_PATH . '/../../api/framework');//第三方库目录
defined('APP_PATH') or define('APP_PATH', JIUYI_ROOT_PATH . '/../../91160CMS/apps/Website');//程序文件
defined('COMMON_PATH') or define('COMMON_PATH', JIUYI_ROOT_PATH . '/../../91160CMS/apps/Common');//程序公共模块
defined('RUNTIME_PATH') or define('RUNTIME_PATH', APP_PATH . '/Runtime');//运行时文件
defined('DEFAULT_NAMESPACE') or define('DEFAULT_NAMESPACE', 'Joy\Website');//默认的命名空间
 
include VENDOR_PATH. '/Joy.php';//引入phalcon框架
include COMMON_PATH. '/Config/define.php'; //引入公共常量
include APP_PATH. '/Config/define.php'; //引入应用常量
 
(new \Joy\Web\Application())->run();
KeyValue
_urlsitemap.html
KeyValue
NLS_LANGAMERICAN_AMERICA.AL32UTF8
ORACLE_HOME/usr/lib/oracle/11.2/client64/lib
USERnobody
HOME/
FCGI_ROLERESPONDER
SCRIPT_FILENAME/app/wwwroot/rrjk160-com/91web/index_new.php
QUERY_STRING_url=sitemap.html
REQUEST_METHODGET
CONTENT_TYPE
CONTENT_LENGTH
SCRIPT_NAME/index_new.php
REQUEST_URI/v5/sitemap.html
DOCUMENT_URI/index_new.php
DOCUMENT_ROOT/app/wwwroot/rrjk160-com/91web
SERVER_PROTOCOLHTTP/1.1
GATEWAY_INTERFACECGI/1.1
SERVER_SOFTWAREnginx/1.16.0
REMOTE_ADDR192.168.1.105
REMOTE_PORT41102
SERVER_ADDR10.8.26.36
SERVER_PORT80
SERVER_NAMEwww.rrjk160.com
REDIRECT_STATUS200
PHALCON_ENVproduction
HTTP_HOSTwww.rrjk160.com
HTTP_X_REAL_IP216.73.216.176
HTTP_X_FORWARDED_FOR216.73.216.176
HTTP_X_FORWARDED_PROTOhttps
HTTP_X_FORWARDED_PORT443
HTTP_ACCEPT*/*
HTTP_USER_AGENTMozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
HTTP_ACCEPT_ENCODINGgzip, br, zstd, deflate
PHP_SELF/index_new.php
REQUEST_TIME_FLOAT1757877086.8449
REQUEST_TIME1757877086
#Path
0/app/wwwroot/rrjk160-com/91web/index_new.php
1/app/wwwroot/api/framework/Joy.php
2/app/wwwroot/api/framework/vendor/autoload.php
3/app/wwwroot/api/framework/vendor/composer/autoload_real.php
4/app/wwwroot/api/framework/vendor/composer/ClassLoader.php
5/app/wwwroot/api/framework/vendor/composer/autoload_namespaces.php
6/app/wwwroot/api/framework/vendor/composer/autoload_psr4.php
7/app/wwwroot/api/framework/vendor/composer/autoload_classmap.php
8/app/wwwroot/91160CMS/apps/Common/Config/define.php
9/app/wwwroot/91160CMS/apps/Website/Config/define.php
10/app/wwwroot/api/framework/Web/Application.php
11/app/wwwroot/api/framework/Application.php
12/app/wwwroot/91160CMS/config/web.php
13/app/wwwroot/91160CMS/apps/Website/Config/params_local.php
14/app/wwwroot/91160CMS/apps/Website/Controllers/Routes.php
15/app/wwwroot/91160CMS/apps/Website/Controllers/SitemapController.php
16/app/wwwroot/91160CMS/apps/Website/Controllers/Controller.php
17/app/wwwroot/api/framework/Web/Controller.php
18/app/wwwroot/91160CMS/apps/Common/Daos/SiteMenuD.php
19/app/wwwroot/91160CMS/apps/Common/Models/Basic/SiteMenu.php
20/app/wwwroot/91160CMS/apps/Common/Models/CmsBaseModel.php
21/app/wwwroot/91160CMS/apps/Common/Models/BaseModel.php
22/app/wwwroot/api/framework/Library/Phalcon/Mvc/Model/MetaData/Strategy/AnnotationsMetaData.php
Memory
Usage1310720