diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3914939 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/app/cache/* \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/app/backend/BackendModule.php b/app/backend/BackendModule.php new file mode 100644 index 0000000..9c46918 --- /dev/null +++ b/app/backend/BackendModule.php @@ -0,0 +1,101 @@ + registerDispatcherService($di); + /** DI注册url服务 */ + $this -> registerUrlService($di); + /** DI注册view服务 */ + $this -> registerViewService($di); + } + + /** + * DI注册dispatcher服务 + * @param DiInterface $di + */ + protected function registerDispatcherService(DiInterface $di){ + $config = $di -> get('config'); + $di->setShared('dispatcher', function() use ($config) { + $eventsManager = new \Phalcon\Events\Manager(); + $eventsManager -> attach("dispatch:beforeException", function($event, $dispatcher, $exception) { + if ($event -> getType() == 'beforeException') { + switch ($exception->getCode()) { + case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: + $dispatcher->forward(array( + 'controller' => 'Index', + 'action' => 'notfound' + )); + return false; + case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: + $dispatcher->forward(array( + 'controller' => 'Index', + 'action' => 'notfound' + )); + return false; + } + } + }); + $dispatcher = new \Phalcon\Mvc\Dispatcher(); + $dispatcher -> setEventsManager($eventsManager); + //默认设置为后台的调度器 + $dispatcher -> setDefaultNamespace('\\Marser\\App\\Backend\\Controllers'); + return $dispatcher; + }); + } + + /** + * DI注册url服务 + * @param DiInterface $di + */ + protected function registerUrlService(DiInterface $di){ + $config = $di -> get('config'); + $di -> setShared('url', function() use($config){ + $url = new \Phalcon\Mvc\Url(); + $url -> setBaseUri('/admin/'); + $url -> setStaticBaseUri('/admin-assets/'); + return $url; + }); + } + + /** + * DI注册view服务 + * @param DiInterface $di + */ + protected function registerViewService(DiInterface $di){ + $config = $di -> get('config'); + $di -> setShared('view', function() use($config) { + $view = new \Phalcon\Mvc\View(); + $view -> setViewsDir(ROOT_PATH . '/app/backend/views'); + $view -> registerEngines(array( + '.phtml' => function($view, $di) use($config) { + $volt = new \Marser\App\Core\PhalBaseVolt($view, $di); + $volt -> setOptions(array( + 'compileAlways' => false, + 'compiledPath' => ROOT_PATH . '/app/cache/compiled/backend' + )); + $volt -> initFunction(); + return $volt; + }, + )); + return $view; + }); + } +} \ No newline at end of file diff --git a/app/backend/controllers/.gitignore b/app/backend/controllers/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/app/backend/controllers/IndexController.php b/app/backend/controllers/IndexController.php new file mode 100644 index 0000000..7157268 --- /dev/null +++ b/app/backend/controllers/IndexController.php @@ -0,0 +1,15 @@ + array( + 'module' => 'backend', + 'controller'=>1, + 'action'=>2 + ), + +); \ No newline at end of file diff --git a/app/config/system.php b/app/config/system.php new file mode 100644 index 0000000..c4fc7cb --- /dev/null +++ b/app/config/system.php @@ -0,0 +1,19 @@ + array( + //数据库连接信息 + 'db' => array( + 'host' => '127.0.0.1', + 'port' => 3306, + 'username' => 'admin', + 'password' => 'admin', + 'dbname' => 'test', + 'charset' => 'utf8', + ), + + //表前缀 + 'prefix' => 'test_', + ), +); \ No newline at end of file diff --git a/app/core/.gitignore b/app/core/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/app/core/PhalBaseVolt.php b/app/core/PhalBaseVolt.php new file mode 100644 index 0000000..b6932d4 --- /dev/null +++ b/app/core/PhalBaseVolt.php @@ -0,0 +1,32 @@ +getCompiler(); + + /** 添加str_repeat函数 */ + $compiler -> addFunction('str_repeat', 'str_repeat'); + + /** 添加substr_count函数 */ + $compiler -> addFunction('substr_count', 'substr_count'); + + /** 添加explode函数 */ + $compiler -> addFunction('explode', 'explode'); + + /** 添加array_rand函数 */ + $compiler -> addFunction('array_rand', 'array_rand'); + } + + +} + + + diff --git a/app/core/loader.php b/app/core/loader.php new file mode 100644 index 0000000..93058d4 --- /dev/null +++ b/app/core/loader.php @@ -0,0 +1,20 @@ + registerNamespaces(array( + 'Marser' => ROOT_PATH, + + 'Marser\App\Core' => ROOT_PATH . '/app/core', + 'Marser\App\Libs' => ROOT_PATH . '/app/libs', + 'Marser\App\Tasks' => ROOT_PATH . '/app/tasks', + + 'Marser\App\Frontend\Controllers' => ROOT_PATH . '/app/frontend/controllers', + 'Marser\App\Frontend\Models' => ROOT_PATH . '/app/frontend/models', + + 'Marser\App\Backend\Controllers' => ROOT_PATH . '/app/backend/controllers', + 'Marser\App\Backend\Models' => ROOT_PATH . '/app/backend/models', +)) -> register(); \ No newline at end of file diff --git a/app/core/services.php b/app/core/services.php new file mode 100644 index 0000000..fbe669e --- /dev/null +++ b/app/core/services.php @@ -0,0 +1,123 @@ + setShared('router', function(){ + $router = new \Phalcon\Mvc\Router(); + $router -> setDefaultModule('frontend'); + + $routerRules = new \Phalcon\Config\Adapter\Php(ROOT_PATH . "/app/config/routers.php"); + foreach ($routerRules->toArray() as $key => $value){ + $router->add($key,$value); + } + + return $router; +}); + +/** + * DI注册session服务 + */ +$di -> setShared('session', function(){ + $session = new Session(); + $session -> start(); + return $session; +}); + +/** + * DI注册cookies服务 + */ +$di -> setShared('cookies', function() { + $cookies = new \Phalcon\Http\Response\Cookies(); + $cookies -> useEncryption(false); + return $cookies; +}); + +/** + * DI注册DB配置 + */ +$di -> setShared('db', function () use($config) { + $dbconfig = $config -> database -> db; + $dbconfig = $dbconfig -> toArray(); + if (!is_array($dbconfig) || count($dbconfig)==0) { + throw new \Exception("the database config is error"); + } + + $eventsManager = new \Phalcon\Events\Manager(); + // 分析底层sql性能,并记录日志 + $profiler = new DbProfiler(); + $eventsManager -> attach('db', function ($event, $connection) use ($profiler) { + if($event -> getType() == 'beforeQuery'){ + //在sql发送到数据库前启动分析 + $profiler -> startProfile($connection -> getSQLStatement()); + } + if($event -> getType() == 'afterQuery'){ + //在sql执行完毕后停止分析 + $profiler -> stopProfile(); + //获取分析结果 + $profile = $profiler -> getLastProfile(); + $sql = $profile->getSQLStatement(); + $executeTime = $profile->getTotalElapsedSeconds(); + //日志记录 + $currentDay = date('Ymd'); + $logger = new \Phalcon\Logger\Adapter\File(ROOT_PATH . "/app/cache/logs/{$currentDay}.log"); + $logger -> debug("{$sql} {$executeTime}"); + } + }); + + $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array( + "host" => $dbconfig['host'], "port" => $dbconfig['port'], + "username" => $dbconfig['username'], + "password" => $dbconfig['password'], + "dbname" => $dbconfig['dbname'], + "charset" => $dbconfig['charset']) + ); + + /* 注册监听事件 */ + $connection->setEventsManager($eventsManager); + + return $connection; +}); + +/** + * DI注册modelsManager服务 + */ +$di -> setShared('modelsManager', function() use($di){ + return new ModelsManager(); +}); + +/** + * DI注册日志服务 + */ +$di -> setShared('logger', function() use($di){ + $currentDay = date('Ymd'); + $logger = \Phalcon\Logger\Adapter\File(ROOT_PATH . "/app/cache/logs/{$currentDay}.log"); + return $logger; +}); + +/** + * DI注册配置服务 + */ +$di -> setShared('config', function() use($config){ + return $config; +}); + + diff --git a/app/frontend/FrontendModule.php b/app/frontend/FrontendModule.php new file mode 100644 index 0000000..be92926 --- /dev/null +++ b/app/frontend/FrontendModule.php @@ -0,0 +1,101 @@ + registerDispatcherService($di); + /**DI注册url服务*/ + $this -> registerUrlService($di); + /**DI注册前台view*/ + $this -> registerViewService($di); + } + + /** + * DI注册dispatcher服务 + * @param DiInterface $di + */ + protected function registerDispatcherService(DiInterface $di){ + $config = $di -> get('config'); + $di->set('dispatcher', function() use ($config) { + $eventsManager = new \Phalcon\Events\Manager(); + $eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) { + if ($event->getType() == 'beforeException') { + switch ($exception->getCode()) { + case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: + $dispatcher->forward(array( + 'controller' => 'Index', + 'action' => 'notfound' + )); + return false; + case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: + $dispatcher->forward(array( + 'controller' => 'Index', + 'action' => 'notfound' + )); + return false; + } + } + }); + $dispatcher = new \Phalcon\Mvc\Dispatcher(); + $dispatcher->setEventsManager($eventsManager); + //默认设置为前台的调度器 + $dispatcher->setDefaultNamespace('\\Marser\\App\\Frontend\\Controllers'); + return $dispatcher; + }, true); + } + + /** + * DI注册url服务 + * @param DiInterface $di + */ + protected function registerUrlService(DiInterface $di){ + $config = $di -> get('config'); + $di -> setShared('url', function() use($config) { + $url = new \Phalcon\Mvc\Url(); + $url -> setBaseUri('/'); + $url -> setStaticBaseUri('/assets/'); + return $url; + }); + } + + /** + * DI注册view服务 + * @param DiInterface $di + */ + protected function registerViewService(DiInterface $di){ + $config = $di -> get('config'); + $di -> setShared('view', function() use($config) { + $view = new \Phalcon\Mvc\View(); + $view -> setViewsDir(ROOT_PATH . '/app/frontend/views/'); + $view -> registerEngines(array( + '.phtml' => function($view, $di) use($config) { + $volt = new \Marser\App\Core\PhalBaseVolt($view, $di); + $volt -> setOptions(array( + 'compileAlways' => false, + 'compiledPath' => ROOT_PATH . '/app/cache/compiled/frontend' + )); + $volt -> initFunction(); + return $volt; + }, + )); + return $view; + }); + } +} \ No newline at end of file diff --git a/app/frontend/controllers/.gitignore b/app/frontend/controllers/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/app/frontend/controllers/IndexController.php b/app/frontend/controllers/IndexController.php new file mode 100644 index 0000000..7856ab1 --- /dev/null +++ b/app/frontend/controllers/IndexController.php @@ -0,0 +1,15 @@ + registerModules(array( + 'frontend' => array( + 'className' => 'Marser\App\Frontend\FrontendModule', + 'path' => ROOT_PATH . '/app/frontend/FrontendModule.php', + ), + 'backend' => array( + 'className' => 'Marser\App\Backend\BackendModule', + 'path' => ROOT_PATH . '/app/backend/BackendModule.php', + ), + )); + + echo $application->handle()->getContent(); + +}catch (\Exception $e) { + echo '
'; + print_r($e); +} + + + + +