Skip to content

Commit

Permalink
添加:链式调用
Browse files Browse the repository at this point in the history
  • Loading branch information
dxkite committed Sep 13, 2019
1 parent 72eb07e commit 9b77062
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 23 deletions.
15 changes: 8 additions & 7 deletions suda/src/application/Application.php
Expand Up @@ -20,8 +20,9 @@
use suda\application\wrapper\ExceptionContentWrapper;
use suda\application\exception\ConfigurationException;
use suda\framework\http\Response as ResponseInterface;
use suda\application\processor\TemplateAssetProccesser;
use suda\application\processor\TemplateAssetProcessor;
use suda\application\processor\TemplateRequestProcessor;
use suda\application\processor\RunnableRequestProcessor;

/**
* 应用程序
Expand Down Expand Up @@ -160,17 +161,17 @@ public function run(RequestInterface $request, ResponseInterface $response)
public function request(array $method, string $name, string $url, array $attributes = [])
{
$route = $attributes['config'] ?? [];
$runnable = null;
$runnable = RunnableRequestProcessor::class . '->onRequest';
if (array_key_exists('class', $route)) {
$runnable = $this->className($route['class']) . '->onRequest';
$attributes['class'] = $route['class'];
} elseif (array_key_exists('source', $route)) {
$attributes['class'] = FileRequestProcessor::class;
$attributes['source'] = $route['source'];
$runnable = FileRequestProcessor::class . '->onRequest';
} elseif (array_key_exists('template', $route)) {
$attributes['class'] = TemplateRequestProcessor::class;
$attributes['template'] = $route['template'];
$runnable = TemplateRequestProcessor::class . '->onRequest';
} elseif (array_key_exists('runnable', $route)) {
$runnable = $route['runnable'];
$attributes['runnable'] = $route['runnable'];
} else {
throw new ConfigurationException('request config error', ConfigurationException::ERR_CONFIG_SET);
}
Expand All @@ -187,7 +188,7 @@ public function request(array $method, string $name, string $url, array $attribu
*/
protected function defaultResponse(Application $application, Request $request, Response $response)
{
if ((new TemplateAssetProccesser)->onRequest($application, $request, $response)) {
if ((new TemplateAssetProcessor)->onRequest($application, $request, $response)) {
return;
}
return $this->route->getDefaultRunnable()->run($request, $response);
Expand Down
11 changes: 0 additions & 11 deletions suda/src/application/ApplicationBase.php
Expand Up @@ -120,17 +120,6 @@ public function getModulePaths()
}


/**
* 转换类名
*
* @param string $name
* @return string
*/
protected function className(string $name)
{
return str_replace(['.','/'], '\\', $name);
}

/**
* 语言翻译
*
Expand Down
2 changes: 1 addition & 1 deletion suda/src/application/loader/ApplicationModuleLoader.php
Expand Up @@ -111,7 +111,7 @@ private function getModuleDirectoryConfig(string $path)
*/
private function prepareModule()
{
foreach ($this->application->getModules()->all() as $name => $module) {
foreach ($this->application->getModules() as $name => $module) {
$this->moduleLoader[$name] = new ModuleLoader($this->application, $module);
$this->moduleLoader[$name]->toLoad(); // 切换到加载状态
$this->moduleLoader[$name]->loadExtraModuleResourceLibrary(); // 加载二外的模块源
Expand Down
Expand Up @@ -13,7 +13,7 @@
/**
* 响应
*/
class FileRangeProccessor implements RequestProcessor
class FileRangeProcessor implements RequestProcessor
{
/**
* 文件路径
Expand Down
2 changes: 1 addition & 1 deletion suda/src/application/processor/FileRequestProcessor.php
Expand Up @@ -15,7 +15,7 @@ public function onRequest(Application $application, Request $request, Response $
{
$filename = $request->getAttribute('source');
if (is_string($filename)) {
$processor = new FileRangeProccessor($filename);
$processor = new FileRangeProcessor($filename);
try {
$processor->onRequest($application, $request, $response);
} catch (Exception $e) {
Expand Down
14 changes: 14 additions & 0 deletions suda/src/application/processor/RequestChanProcessor.php
@@ -0,0 +1,14 @@
<?php


namespace suda\application\processor;


use suda\application\Application;
use suda\framework\Request;
use suda\framework\Response;

interface RequestChanProcessor
{
public function onRequest(Application $application, Request $request, Response $response, RequestProcessor $next);
}
119 changes: 119 additions & 0 deletions suda/src/application/processor/RunnableRequestProcessor.php
@@ -0,0 +1,119 @@
<?php


namespace suda\application\processor;


use suda\application\Application;
use suda\framework\Request;
use suda\framework\Response;
use suda\framework\runnable\Runnable;

class RunnableRequestProcessor implements RequestProcessor
{
/**
* @var string|null
*/
protected $runnable = null;

/**
* @param Application $application
* @param Request $request
* @param Response $response
* @return mixed
*/
public function onRequest(Application $application, Request $request, Response $response)
{
$runnable = $this->getNextRunnable($application, $request);
if ($runnable === null) {
return null;
}
return $runnable($application, $request, $response, $this);
}

/**
* @param Application $application
* @param Request $request
* @return Runnable|null
*/
protected function getNextRunnable(Application $application, Request $request)
{
$runnable = $this->getNextRunnableFromChan($application, $request);
if ($runnable === null) {
return null;
}
return new Runnable($runnable);
}

/**
* @param Application $application
* @param Request $request
* @return string|null
*/
protected function getNextRunnableFromChan(Application $application, Request $request)
{
if ($this->runnable === null) {
$this->runnable = $this->createRunnable($application, $request);
}
if (count($this->runnable) === 0) {
return null;
}
return array_shift($this->runnable);
}

/**
* @param Application $application
* @param Request $request
* @return array
*/
protected function createRunnable(Application $application, Request $request)
{
$class = $application->getConfig()->get('processor', []);
$processor = $this->formatClassAsRunnable($class);
$runnable = $this->createChanFromRequest($request);
return array_merge($processor, $runnable);
}

/**
* @param Request $request
* @return array
*/
protected function createChanFromRequest(Request $request)
{
$runnable = $request->getAttribute('runnable');
if (is_string($runnable)) {
return [$runnable];
}
if (is_array($runnable)) {
return $runnable;
}
$class = $request->getAttribute('class', []);
return $this->formatClassAsRunnable($class);
}

/**
* @param $class
* @return array|string
*/
private function formatClassAsRunnable($class)
{
if (is_string($class)) {
$class = [$class];
}
foreach ($class as $index => $className) {
$class[$index] = $this->className($className) . '->onRequest';
}
return $class;
}

/**
* 转换类名
*
* @param string $name
* @return string
*/
private function className(string $name)
{
return trim(str_replace(['.', '/'], '\\', $name), '\\');
}
}
Expand Up @@ -10,7 +10,7 @@
/**
* 模块资源处理响应
*/
class TemplateAssetProccesser implements RequestProcessor
class TemplateAssetProcessor implements RequestProcessor
{
/**
* 处理文件请求
Expand All @@ -32,7 +32,7 @@ public function onRequest(Application $application, Request $request, Response $
$resourcePath = $parent.'/'.$assetPath;
$realPath = $module->getResource()->getResourcePath($resourcePath, $parent);
if ($realPath) {
$file = new FileRangeProccessor($realPath);
$file = new FileRangeProcessor($realPath);
$file->onRequest($application, $request, $response);
return true;
}
Expand Down

0 comments on commit 9b77062

Please sign in to comment.