Skip to content

Commit

Permalink
添加:代码注释
Browse files Browse the repository at this point in the history
  • Loading branch information
dxkite committed Aug 3, 2019
1 parent 785e91d commit c8721b0
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 46 deletions.
3 changes: 1 addition & 2 deletions suda/src/application/Application.php
Expand Up @@ -83,7 +83,6 @@ public function load()
*/
protected function prepare(Request $request, Response $response)
{

$response->setHeader('x-powered-by', 'suda/' . SUDA_VERSION, true);
$response->getWrapper()->register(ExceptionContentWrapper::class, [Throwable::class]);
$response->getWrapper()->register(TemplateWrapper::class, [RawTemplate::class]);
Expand Down Expand Up @@ -127,7 +126,7 @@ public function run(RequestInterface $request, ResponseInterface $response)
$init = $this->debug->timeEnd('init');
$this->debug->recordTiming('init', $init, 'init total');
$this->debug->time('match route');
$result = $this->route->match($appRequest->getMethod(), $appRequest->getUri());
$result = $this->route->match($appRequest->getMethod(), $appRequest->getUri());
$match = $this->debug->timeEnd('match route');
$this->debug->recordTiming('dispatch', $match);
if ($result !== null) {
Expand Down
9 changes: 3 additions & 6 deletions suda/src/application/ApplicationBase.php
Expand Up @@ -177,17 +177,14 @@ public function getRunning()
return $this->running;
}


/**
* Set 运行的模块
*
* @param Module $running 运行的模块
*
* @return self
* @param Module $running
* @return $this
*/
public function setRunning(Module $running)
{
$this->running = $running;

return $this;
}

Expand Down
56 changes: 37 additions & 19 deletions suda/src/application/ApplicationContext.php
Expand Up @@ -81,15 +81,40 @@ class ApplicationContext extends Context
public function __construct(string $path, array $manifest, Loader $loader, ?string $dataPath = null)
{
parent::__construct(new Config(['app' => $manifest]), $loader);
$this->setPath($path);
$this->setManifest($manifest);
$this->setRouteGroup($manifest['route-group'] ?? ['default']);
$resource = new Resource();
$resource->registerResourcePath($path, $manifest['resource'] ?? './resource');
$this->setResource($resource);
$this->setLocate($manifest['locale'] ?? 'zh-cn');
$this->setStyle($manifest['style'] ?? 'default');
$this->setDataPath($dataPath ?? Resource::getPathByRelativePath($manifest['resource'] ?? './data', $path));
$this->config->set('data_path', $this->dataPath);
}

/**
* @param string $path
*/
public function setPath(string $path): void
{
$this->path = $path;
$this->routeGroup = $manifest['route-group'] ?? ['default'];
$this->resource = new Resource();
$this->resource->registerResourcePath($path, $manifest['resource'] ?? './resource');
$this->locate = $manifest['locale'] ?? 'zh-cn';
$this->style = $manifest['style'] ?? 'default';
}

/**
* @param array $manifest
*/
public function setManifest(array $manifest): void
{
$this->manifest = $manifest;
$this->dataPath = $dataPath ?? Resource::getPathByRelativePath($manifest['resource'] ?? './data', $path);
$this->config->set('data_path', $this->dataPath);
}

/**
* @param array $routeGroup
*/
public function setRouteGroup(array $routeGroup): void
{
$this->routeGroup = $routeGroup;
}

/**
Expand Down Expand Up @@ -117,29 +142,22 @@ public function setStyle(string $style)
}

/**
* Get 语言
*
* @return string
* @return string
*/
public function getLocate()
public function getLocate(): string
{
return $this->locate;
}

/**
* Set 语言
*
* @param string $locate 语言
*
* @return self
* @param string $locate
*/
public function setLocate(string $locate)
public function setLocate(string $locate): void
{
$this->locate = $locate;

return $this;
}


/**
* Get 配置数组
*
Expand Down
36 changes: 33 additions & 3 deletions suda/src/application/loader/ApplicationLoader.php
Expand Up @@ -50,6 +50,9 @@ public function __construct(Application $application)
$this->reachableModules = [];
}

/**
* 加载APP
*/
public function load()
{
$this->loadVendorIfExist();
Expand All @@ -61,6 +64,9 @@ public function load()
}


/**
* 加载路由
*/
public function loadRoute()
{
foreach ($this->application->getModules() as $name => $module) {
Expand All @@ -71,6 +77,9 @@ public function loadRoute()
}


/**
* 加载全局配置
*/
public function loadGlobalConfig()
{
$resource = $this->application->getResource();
Expand All @@ -82,6 +91,9 @@ public function loadGlobalConfig()
}
}

/**
* 加载额外vendor
*/
public function loadVendorIfExist()
{
$vendorAutoload = $this->application->getPath() . '/vendor/autoload.php';
Expand All @@ -91,6 +103,8 @@ public function loadVendorIfExist()
}

/**
* 准备数据源
*
* @throws SQLException
*/
public function loadDataSource()
Expand All @@ -100,16 +114,21 @@ public function loadDataSource()
$this->application->setDataSource($dataSource);
}


/**
* 准备模块
*/
protected function prepareModule()
{
foreach ($this->application->getModules()->all() as $name => $module) {
$this->moduleLoader[$name] = new ModuleLoader($this->application, $module);
$this->moduleLoader[$name]->toLoad();
$this->moduleLoader[$name]->loadExtraModuleResourceLibrary();
$this->moduleLoader[$name]->toLoad(); // 切换到加载状态
$this->moduleLoader[$name]->loadExtraModuleResourceLibrary(); // 加载二外的模块源
}
}

/**
* 激活模块
*/
protected function activeModule()
{
foreach ($this->application->getModules()->all() as $name => $module) {
Expand All @@ -119,6 +138,9 @@ protected function activeModule()
}
}

/**
* 注册模块
*/
protected function registerModule()
{
$extractPath = $this->application->getDataPath() . '/extract-module';
Expand All @@ -136,6 +158,10 @@ protected function setModuleStatus()
$this->setModuleReachable($this->application->getModules(), $reachable);
}

/**
* @param string $path
* @param string $extractPath
*/
protected function registerModuleFrom(string $path, string $extractPath)
{
$modules = new ModuleBag;
Expand All @@ -145,6 +171,10 @@ protected function registerModuleFrom(string $path, string $extractPath)
$this->prepareModuleConfig($path, $modules);
}

/**
* @param string $path
* @param ModuleBag $modules
*/
protected function prepareModuleConfig(string $path, ModuleBag $modules)
{
$config = $this->getModuleDirectoryConfig($path);
Expand Down
8 changes: 8 additions & 0 deletions suda/src/database/connection/Connection.php
Expand Up @@ -120,6 +120,14 @@ public function connect()
return $this->isConnected();
}

/**
* @return array
*/
public function __sleep()
{
return ['type', 'config', 'observer'];
}

/**
* 获取PDO
* @ignore-dump
Expand Down
33 changes: 17 additions & 16 deletions suda/src/framework/Context.php
@@ -1,4 +1,5 @@
<?php

namespace suda\framework;

use function is_array;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function __construct(Config $config, Loader $loader, ?Event $event = null
*
* @return Route
*/
public function route():Route
public function route(): Route
{
return $this->route;
}
Expand All @@ -82,7 +83,7 @@ public function cache(): Cache
*
* @return Event
*/
public function event():Event
public function event(): Event
{
return $this->event;
}
Expand All @@ -94,7 +95,7 @@ public function event():Event
* @param array $cacheConfig
* @return Cache
*/
protected function createCacheFrom(string $cacheName, array $cacheConfig):Cache
protected function createCacheFrom(string $cacheName, array $cacheConfig): Cache
{
return new $cacheName($cacheConfig);
}
Expand All @@ -104,9 +105,12 @@ protected function createCacheFrom(string $cacheName, array $cacheConfig):Cache
*
* @return Cache
*/
protected function getDefaultCache():Cache
protected function getDefaultCache(): Cache
{
return $this->createCacheFrom($this->conf('cache.class', FileCache::class), $this->conf('cache', []));
$config = $this->conf('cache', []);
$cacheClass = $this->conf('cache.class', FileCache::class);
$realClassName = Loader::realName($cacheClass);
return $this->createCacheFrom($realClassName, $config);
}

/**
Expand All @@ -122,14 +126,13 @@ public function getEvent()
/**
* Set 事件监听器
*
* @param Event $event 事件监听器
* @param Event $event 事件监听器
*
* @return self
* @return $this
*/
public function setEvent(Event $event)
{
$this->event = $event;

return $this;
}

Expand All @@ -146,14 +149,13 @@ public function getCache()
/**
* Set 缓存
*
* @param Cache $cache 缓存
* @param Cache $cache 缓存
*
* @return self
* @return $this
*/
public function setCache(Cache $cache)
{
$this->cache = $cache;

return $this;
}

Expand Down Expand Up @@ -184,14 +186,13 @@ public function getRoute()
/**
* Set 路由匹配工具
*
* @param Route $route 路由匹配工具
* @param Route $route 路由匹配工具
*
* @return self
* @return $this
*/
public function setRoute(Route $route)
{
$this->route = $route;

return $this;
}

Expand All @@ -208,9 +209,9 @@ public function getDebug()
/**
* Set PHP错误调试
*
* @param Debugger $debug PHP错误调试
* @param Debugger $debug PHP错误调试
*
* @return self
* @return $this
*/
public function setDebug(Debugger $debug)
{
Expand Down

0 comments on commit c8721b0

Please sign in to comment.