Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
invoke method changed to magic method __invoke
Browse files Browse the repository at this point in the history
Zend Config introduced
  • Loading branch information
mkorkmaz committed May 24, 2017
1 parent 5b37e36 commit 33118b1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 72 deletions.
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
}
}
78 changes: 38 additions & 40 deletions src/Core/Application.php → src/Core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/


Expand All @@ -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',
Expand All @@ -38,35 +36,42 @@ 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,
ContainerInterface $container
) {

$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
);
}
Expand Down Expand Up @@ -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
Expand Down
45 changes: 23 additions & 22 deletions src/Core/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down
10 changes: 6 additions & 4 deletions src/Http/ServerRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ private static function marshalProtocolVersion(array $server) : string
return '1.1';
}
if (! preg_match('#^(HTTP/)?(?P<version>[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'];
}
Expand Down
10 changes: 10 additions & 0 deletions src/Interfaces/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Selami\Interfaces;


interface Application
{
public function __invoke();
}

0 comments on commit 33118b1

Please sign in to comment.