Skip to content

Commit

Permalink
底层更加调用合理
Browse files Browse the repository at this point in the history
  • Loading branch information
baichou committed Dec 15, 2017
1 parent e44b8c5 commit b94aaba
Show file tree
Hide file tree
Showing 23 changed files with 211 additions and 23 deletions.
15 changes: 15 additions & 0 deletions apps/Events/LoginToIndexEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: 明月有色
* Date: 2017/12/15
* Time: 19:43
*/

namespace Apps\Events;


class LoginToIndexEvent
{

}
78 changes: 77 additions & 1 deletion apps/Facades/Containers/ConfigContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,97 @@
namespace Apps\Facades\Containers;


use Framework\App;
use Framework\Support\Containers\Container;
use Framework\Support\Containers\FacadeInterface;
use Phalcon\Config;
use Phalcon\Di;

class ConfigContainer implements FacadeInterface
{
use Container;

/**
* @var \Phalcon\Config
*/
private static $_instance;

/**
* 映射实体类
*
* @return string|object
*/
public static function getFacadesAccessor()
{
return Di::getDefault()->setShared('config');
return Di::getDefault()->getShared('config');
}

/**
* 读取配置-自动加载配置文件
*
* @param string $key
* @param string $default
* @return mixed|null|Config
*/
public function get($key=null,$default=null)
{
if( $key===null ){
return self::$_instance;
}elseif ( !strpos($key,'.') ){
$value = self::$_instance->get($key);
if( $value===null ){
$value = self::getFileConfig($key);
if( $value===false ){
return $default;
}
return $value;
}else{
return $value;
}
}
$arrConfigKey = explode('.',$key);
$config = self::$_instance;
$value = $config->get($arrConfigKey[0]);
foreach ($arrConfigKey as $num=>$keyNext){
if( $value===null ){
if( $num==0 ){
$value = self::getFileConfig($keyNext);
}else{
return $default;
}
}elseif( $value instanceof Config){
$config = $value;
$value = $config->get($keyNext);
}elseif( $num==0 ){
$value = self::getFileConfig($keyNext);
if( $value===null ) {
return $default;
}
}
}
return $value;
}

private function getFileConfig($fileName)
{
$value = false;
$configFile = Di::getDefault()->getShared("module")->modulePath . "/Config/{$fileName}.php";
if( file_exists($configFile) ){
$addConfig = @include_once $configFile;
if( is_array($addConfig) ){
self::$_instance->merge(new Config([$fileName=>$addConfig]));
$value = self::$_instance->get($fileName);
}
}else{
$configFile = App::getRootPath() . "/config/{$fileName}.php";
if( file_exists($configFile) ){
$addConfig = @include_once $configFile;
if( is_array($addConfig) ){
self::$_instance->merge(new Config([$fileName=>$addConfig]));
$value = self::$_instance->get($fileName);
}
}
}
return $value;
}
}
3 changes: 2 additions & 1 deletion apps/Facades/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Apps\Facades;

use Apps\Facades\Containers\ConfigContainer;
use Framework\Support\Containers\LogContainer;
use Framework\Support\FacadeKernel;

Expand All @@ -22,7 +23,7 @@ protected function register()
{
return [
'Log'=>LogContainer::class,
// 'Demo'=>"\\Apps\Facades\\Containers\\DemoContainer",
'Config'=>ConfigContainer::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Time: 14:24
*/

namespace Apps\Modules\Common\Controllers;
namespace Apps\Http\Common\Controllers;


class Controller extends \Phalcon\Mvc\Controller
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Time: 18:23
*/

namespace Apps\Modules\Index\Controllers;
namespace Apps\Http\Index\Controllers;


use Apps\Modules\Common\Controllers\Controller;
use Apps\Http\Common\Controllers\Controller;
use Unirest\Request;

class BookController extends Controller
Expand Down
18 changes: 18 additions & 0 deletions apps/Http/Index/Controllers/ConfigController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Created by PhpStorm.
* User: 明月有色
* Date: 2017/12/15
* Time: 17:27
*/

namespace Apps\Http\Index\Controllers;


class ConfigController
{
public function index()
{
dump(\Config::get('database','默认'));
}
}
22 changes: 22 additions & 0 deletions apps/Http/Index/Controllers/DbController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: 明月有色
* Date: 2017/12/15
* Time: 17:26
*/

namespace Apps\Http\Index\Controllers;


use Apps\Http\Common\Controllers\Controller;
use Phalcon\Di;

class DbController extends Controller
{
public function index()
{
dump($this->db->listTables());
dump($this->di);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Time: 17:42
*/

namespace Apps\Modules\Index\Controllers;
namespace Apps\Http\Index\Controllers;

use Phalcon\Mvc\Controller;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
* Time: 15:06
*/

namespace Apps\Modules\Index\Controllers;
namespace Apps\Http\Index\Controllers;

use Apps\Modules\Common\Controllers\Controller;
use Apps\Http\Common\Controllers\Controller;

class IndexController extends Controller
{
public function index()
{
throw new \Exception('发送报错');
throw new \Exception('测试错误');
$this->view->pick('home');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Time: 17:28
*/

namespace Apps\Modules\Index\Controllers;
namespace Apps\Http\Index\Controllers;


use Apps\Modules\Common\Controllers\Controller;
use Apps\Http\Common\Controllers\Controller;
use Unirest\Request;

class ProxiesController extends Controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Time: 18:21
*/

namespace Apps\Modules\Index\Controllers;
namespace Apps\Http\Index\Controllers;


use Apps\Modules\Common\Controllers\Controller;
use Apps\Http\Common\Controllers\Controller;

class ToolController extends Controller
{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
49 changes: 49 additions & 0 deletions apps/Listeners/DbListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Created by PhpStorm.
* User: 明月有色
* Date: 2017/12/15
* Time: 19:58
*/

namespace Apps\Listeners;


class DbListener
{
protected $_profiler;

protected $_logger;

/**
*创建分析器并开始纪录
*/
public function __construct()
{
$this->_profiler = new Profiler();
$this->_logger = new Logger("../apps/logs/db.log");
}

/**
* 如果事件触发器是'beforeQuery',此函数将会被执行
*/
public function beforeQuery($event, $connection)
{
$this->_profiler->startProfile($connection->getSQLStatement());
}

/**
* 如果事件触发器是'afterQuery',此函数将会被执行
*/
public function afterQuery($event, $connection)
{
$this->_logger->log($connection->getSQLStatement(), Logger::INFO);
$this->_profiler->stopProfile();
}

public function getProfiler()
{
return $this->_profiler;
}

}
2 changes: 2 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
\Framework\Providers\LoggerServiceProvider::class,
\Framework\Providers\ExceptionHandlerServiceProvider::class,
\Framework\Providers\LoadFacadeServiceProvider::class,

\Framework\Providers\DatabaseServiceProvider::class,
]);

$app->init();
Expand Down
9 changes: 7 additions & 2 deletions config/database.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

return [
'default'=>[

'default' => [
'host' => env('DB_HOST', 'mysql'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '123456'),
'dbname' => env('DB_DATABASE', 'full_crm'),
'port' => '3306',
'charset' => 'utf8'
],
];
10 changes: 5 additions & 5 deletions ppe/Providers/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
namespace Framework\Providers;


use Apps\Listeners\DbListener;
use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Mvc\Model\Manager;
use Phalcon\Events\Manager;

class DatabaseServiceProvider extends ServiceProvider
{
Expand All @@ -25,10 +26,9 @@ public function register()
{
$this->di->setShared($this->serviceName, function () {
$eventsManager = new Manager();
$dataBaseCfg = $this->getShared('config')->database->toArray();
$connection = new Mysql($dataBaseCfg['database']);
unset($dataBaseCfg);
$eventsManager->attach('db', new DatabaseEvent());
$defaultDbConfig = \Config::get('database.default')->toArray();
$connection = new Mysql($defaultDbConfig);
$eventsManager->attach('db', new DbListener());
$connection->setEventsManager($eventsManager);
return $connection;
});
Expand Down
4 changes: 2 additions & 2 deletions ppe/Providers/ModulesRouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ private function registerWeb()
$nameSpace = $modules[$moduleName]['nameSpace'];

$module = new Config([
'modulePath'=>$applicationPath.'/apps/Modules/'.$nameSpace,
'defaultNamespace'=>"\\Apps\\Modules\\{$nameSpace}\\Controllers",
'modulePath'=>$applicationPath.'/apps/Http/'.$nameSpace,
'defaultNamespace'=>"\\Apps\\Http\\{$nameSpace}\\Controllers",
]);

$this->di->set('module',function ()use ($module){
Expand Down
2 changes: 1 addition & 1 deletion ppe/Support/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function getView()
$di = Di::getDefault();
$applicationPath = App::getRootPath();
$view = $di->getShared('view');
$viewDir = $applicationPath . '/apps/Modules/Common/Views/';
$viewDir = $applicationPath . '/apps/Http/Common/Views/';
$view->setViewsDir($viewDir);
$view->registerEngines([
".html" => function ($view, Di $di) {
Expand Down

0 comments on commit b94aaba

Please sign in to comment.