From 33118b1ded12ccb80798ea9c5680e57bcf32188b Mon Sep 17 00:00:00 2001 From: Mehmet Korkmaz Date: Thu, 25 May 2017 02:12:20 +0300 Subject: [PATCH] invoke method changed to magic method __invoke Zend Config introduced --- composer.json | 17 +++--- src/Core/{Application.php => App.php} | 78 +++++++++++++-------------- src/Core/Result.php | 45 ++++++++-------- src/Http/ServerRequestFactory.php | 10 ++-- src/Interfaces/Application.php | 10 ++++ 5 files changed, 88 insertions(+), 72 deletions(-) rename src/Core/{Application.php => App.php} (68%) create mode 100644 src/Interfaces/Application.php diff --git a/composer.json b/composer.json index 5983fe5..0df9533 100644 --- a/composer.json +++ b/composer.json @@ -14,24 +14,29 @@ ], "require": { "php": "^7.1", - "zendframework/zend-diactoros": "~1.3.9", - "psr/container": "^1.0", - "symfony/http-foundation": "~3.2.2", "selami/router": "^0.2", "selami/views": "^0.1", - "mattketmo/camel": "~1.1", - "vlucas/phpdotenv": "^2.4" + "psr/container": "^1.0", + "zendframework/zend-diactoros": "~1.3.9", + "zendframework/zend-config": "^3.1", + "symfony/http-foundation": "~3.2.2" }, "require-dev": { "phpunit/phpunit": "^6.0", "satooshi/php-coveralls": "~1.0", "phpunit/phpcov": "^4.0", "squizlabs/php_codesniffer": "^2.0", - "zendframework/zend-servicemanager": "^3.0" + "zendframework/zend-servicemanager": "^3.0", + "zendframework/zend-stdlib": "^3.1" }, "autoload": { "psr-4": { "Selami\\": "src/" } + }, + "autoload-dev": { + "psr-4": { + "MyApp\\": "app/" + } } } diff --git a/src/Core/Application.php b/src/Core/App.php similarity index 68% rename from src/Core/Application.php rename to src/Core/App.php index b6cb640..ff7d436 100644 --- a/src/Core/Application.php +++ b/src/Core/App.php @@ -4,8 +4,8 @@ /** * Selami Application * - * @link https://github.com/selamiphp/core - * @license https://github.com/selamiphp/core/blob/master/LICENSE (MIT License) + * @link https://github.com/selamiphp/core + * @license https://github.com/selamiphp/core/blob/master/LICENSE (MIT License) */ @@ -17,15 +17,13 @@ use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Session\Session as SymfonySession; use Selami\Http\Psr7Response; +use Zend\Config\Config as ZendConfig; -class Application + +class App { - /** - * ServerRequest - * - * @var ServerRequestInterface - */ + /* private $config = [ 'base_dir' => null, 'app_dir' => '/var/lib/www/app', @@ -38,15 +36,22 @@ class Application 'bypass_error_handlers' => true, 'aliases' => [] ]; + */ + + private $container; + private $config; + /** + * ServerRequest + * + * @var ServerRequestInterface + */ private $request; private $route; - private $controller; private $session; private $response; - private $container; public function __construct( - array $config, + ZendConfig $config, ServerRequestInterface $request, Router $router, SymfonySession $session, @@ -54,19 +59,19 @@ public function __construct( ) { $this->request = $request; - $this->config = array_merge($this->config, $config); + $this->config = $config; $this->route = $router->getRoute(); $this->session = $session; $this->container = $container; } - public static function selamiApplicationFactory(ContainerInterface $container) : Application + public static function selamiApplicationFactory(ContainerInterface $container) : App { - return new Application( - $container->get('config'), - $container->get('request'), - $container->get('router'), - $container->get('session'), + return new App( + $container->get(ZendConfig::class), + $container->get(ServerRequestInterface::class), + $container->get(Router::class), + $container->get(SymfonySession::class), $container ); } @@ -106,45 +111,38 @@ private function startSession() :void private function runDispatcher(array $route) : void { $this->response = new Result($this->container, $this->session); - + $defaultReturnType = $this->config->app->get('default_return_type', 'html'); switch ($route['status']) { - case 405: - $this->response->notFound(405, $this->config['default_return_type'], 'Method Not Allowed'); - break; - case 200: - $this->runRoute($route['controller'], $route['returnType'], $route['args']); - break; - case 404: - default: - $this->response->notFound(404, $this->config['default_return_type'], 'Not Found'); - break; + case 405: + $this->response->notFound(405, $defaultReturnType, 'Method Not Allowed'); + break; + case 200: + $this->runRoute($route['controller'], $route['returnType'], $route['args']); + break; + case 404: + default: + $this->response->notFound(404, $defaultReturnType, 'Not Found'); + break; } } - private function runRoute(string $controller, string $returnType = 'html', array $args = []) : void + private function runRoute(string $controller, string $returnType = 'html', ?array $args) : void { - $this->controller = $controller; if (!class_exists($controller)) { $message = "Controller has not class name as {$controller}"; throw new \BadMethodCallException($message); } - $controllerInstance = new $controller($this->container); + $controllerInstance = new $controller($this->container, $args); if (method_exists($controllerInstance, 'applicationLoad')) { $controllerInstance->applicationLoad(); } if (method_exists($controllerInstance, 'controllerLoad')) { $controllerInstance->controllerLoad(); } - $functionOutput = $controllerInstance->invoke($this->request, $args); + $functionOutput = $controllerInstance(); - if (method_exists($controllerInstance, 'controllerClose')) { - $controllerInstance->controllerClose(); - } - if (method_exists($controllerInstance, 'applicationClose')) { - $controllerInstance->applicationClose(); - } $returnFunction = 'return' . ucfirst($returnType); - $this->response->$returnFunction($functionOutput, $this->controller); + $this->response->$returnFunction($functionOutput, $controller); } public function getResponse() : array diff --git a/src/Core/Result.php b/src/Core/Result.php index 096f7cb..cb4acbf 100644 --- a/src/Core/Result.php +++ b/src/Core/Result.php @@ -4,13 +4,14 @@ namespace Selami\Core; use Selami as s; +use Zend\Config\Config as ZendConfig; use Psr\Container\ContainerInterface; use Selami\View\ViewInterface; use Symfony\Component\HttpFoundation\Session\Session; class Result { - private $config = []; + private $config; private $container; private $view; private $session; @@ -27,13 +28,13 @@ class Result public function __construct(ContainerInterface $container, Session $session) { $this->container = $container; - $this->config = $container->get('config'); + $this->config = $container->get(ZendConfig::class); $this->session =$session; } private function checkTemplateFile($template, $type, $controller) : void { - if (!file_exists($this->config['templates_dir'] .'/'. $template)) { + if (!file_exists($this->config->app->get('templates_dir', './templates') .'/'. $template)) { $message = sprintf( '%s template file not found! %s needs a main template file at: %s', $type, @@ -74,7 +75,7 @@ public function returnJson(array $functionOutput, string $controller) : void public function returnHtml(array $functionOutput, string $controller) : void { - $this->useView($this->container->get('view')); + $this->useView($this->container->get(ViewInterface::class)); $paths = explode("\\", $controller); $templateFile = array_pop($paths); $templateFolder = array_pop($paths); @@ -99,7 +100,7 @@ public function returnHtml(array $functionOutput, string $controller) : void public function returnText(array $functionOutput, string $controller) : void { - $this->useView($this->container->get('view')); + $this->useView($this->container->get(ViewInterface::class)); $paths = explode("\\", $controller); $templateFile = array_pop($paths); $templateFolder = array_pop($paths); @@ -168,23 +169,23 @@ public function sendResponse() : void $response->setHeaders($this->result['headers']); $response->setStatusCode($this->result['statusCode']); switch ($this->result['contentType']) { - case 'redirect': - $response->setOutputType('redirect'); - $response->setRedirect($this->result['redirect']); - break; - case 'json': - $response->setOutputType('json'); - $response->setData($this->result['data']); - break; - case 'text': - $response->setOutputType('text'); - $response->setBody($this->result['body']); - break; - case 'html': - default: - $response->setOutputType('html'); - $response->setBody($this->result['body']); - break; + case 'redirect': + $response->setOutputType('redirect'); + $response->setRedirect($this->result['redirect']); + break; + case 'json': + $response->setOutputType('json'); + $response->setData($this->result['data']); + break; + case 'text': + $response->setOutputType('text'); + $response->setBody($this->result['body']); + break; + case 'html': + default: + $response->setOutputType('html'); + $response->setBody($this->result['body']); + break; } $response->send(); } diff --git a/src/Http/ServerRequestFactory.php b/src/Http/ServerRequestFactory.php index 10131a0..3b6a5cd 100644 --- a/src/Http/ServerRequestFactory.php +++ b/src/Http/ServerRequestFactory.php @@ -13,10 +13,12 @@ private static function marshalProtocolVersion(array $server) : string return '1.1'; } if (! preg_match('#^(HTTP/)?(?P[1-9]\d*(?:\.\d)?)$#', $server['SERVER_PROTOCOL'], $matches)) { - throw new \UnexpectedValueException(sprintf( - 'Unrecognized protocol version (%s)', - $server['SERVER_PROTOCOL'] - )); + throw new \UnexpectedValueException( + sprintf( + 'Unrecognized protocol version (%s)', + $server['SERVER_PROTOCOL'] + ) + ); } return $matches['version']; } diff --git a/src/Interfaces/Application.php b/src/Interfaces/Application.php new file mode 100644 index 0000000..dfbb851 --- /dev/null +++ b/src/Interfaces/Application.php @@ -0,0 +1,10 @@ +