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

Commit

Permalink
Application forces to use factory to get action class
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jun 15, 2017
1 parent 33118b1 commit 5ca5e8b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 107 deletions.
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
],
"require": {
"php": "^7.1",
"psr/container": "^1.0",
"selami/router": "^0.2",
"selami/views": "^0.1",
"psr/container": "^1.0",
"zendframework/zend-diactoros": "~1.3.9",
"zendframework/zend-config": "^3.1",
"symfony/http-foundation": "~3.2.2"
"zendframework/zend-diactoros": "^1.3",
"zendframework/zend-servicemanager": "^3.0",
"zendframework/zend-config": "^3.1"
},
"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-stdlib": "^3.1"
"symfony/http-foundation": "^3.2"
},
"autoload": {
"psr-4": {
Expand Down
62 changes: 21 additions & 41 deletions src/Core/App.php → src/Foundation/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
*/


namespace Selami\Core;
namespace Selami\Foundation;

use Selami\Router;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
use Selami\Http\Psr7Response;
use Zend\Config\Config as ZendConfig;

Expand All @@ -37,41 +36,40 @@ class App
'aliases' => []
];
*/

/**
* @var ContainerInterface
*/
private $container;
/**
* @var ZendConfig
*/
private $config;
/**
* ServerRequest
*
* @var ServerRequestInterface
* @var array
*/
private $request;
private $route;
private $session;

/**
* @var array
*/
private $response;

public function __construct(
ZendConfig $config,
ServerRequestInterface $request,
Router $router,
SymfonySession $session,
ContainerInterface $container
) {

$this->request = $request;

$this->config = $config;
$this->route = $router->getRoute();
$this->session = $session;
$this->container = $container;
}

public static function selamiApplicationFactory(ContainerInterface $container) : App
{
return new App(
$container->get(ZendConfig::class),
$container->get(ServerRequestInterface::class),
$container->get(Router::class),
$container->get(SymfonySession::class),
$container
);
}
Expand All @@ -81,7 +79,6 @@ public function __invoke(
ResponseInterface $response,
callable $next = null
) : ResponseInterface {
$this->request = $request;
$this->run();
$psr7Response = new Psr7Response;
$response = $psr7Response($response, $this->response);
Expand All @@ -93,24 +90,14 @@ public function __invoke(

private function run() : void
{
$this->startSession();
$this->runDispatcher($this->route['route']);
}

private function startSession() :void
{
ini_set('session.use_cookies', '1');
ini_set('session.use_only_cookies', '1');
ini_set('session.cookie_httponly', '1');
ini_set('session.name', 'SELAMISESSID');
if (!$this->session->isStarted()) {
$this->session->start();
}
}


private function runDispatcher(array $route) : void
{
$this->response = new Result($this->container, $this->session);
$this->response = new Response($this->container);
$defaultReturnType = $this->config->app->get('default_return_type', 'html');
switch ($route['status']) {
case 405:
Expand All @@ -126,23 +113,16 @@ private function runDispatcher(array $route) : void
}
}

private function runRoute(string $controller, string $returnType = 'html', ?array $args) : void
private function runRoute(string $controllerClass, string $returnType = 'html', ?array $args) : void
{
if (!class_exists($controller)) {
$message = "Controller has not class name as {$controller}";
if (!class_exists($controllerClass)) {
$message = "Controller has not class name as {$controllerClass}";
throw new \BadMethodCallException($message);
}
$controllerInstance = new $controller($this->container, $args);
if (method_exists($controllerInstance, 'applicationLoad')) {
$controllerInstance->applicationLoad();
}
if (method_exists($controllerInstance, 'controllerLoad')) {
$controllerInstance->controllerLoad();
}
$functionOutput = $controllerInstance();

$controller = $controllerClass::factory($this->container, $args);
$functionOutput = $controller->respond();
$returnFunction = 'return' . ucfirst($returnType);
$this->response->$returnFunction($functionOutput, $controller);
$this->response->$returnFunction($functionOutput, $controllerClass);
}

public function getResponse() : array
Expand Down
Loading

0 comments on commit 5ca5e8b

Please sign in to comment.