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

Commit

Permalink
Calling controller strategy changed to autowiring
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jul 14, 2017
1 parent ac60a14 commit 80dbdea
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"require": {
"php": "^7.1",
"psr/container": "^1.0",
"selami/router": "^0.5",
"selami/stdlib": "^1.2",
"selami/router": "^0.6",
"selami/views": "^0.2",
"zendframework/zend-diactoros": "^1.3",
"zendframework/zend-servicemanager": "^3.0",
Expand Down
18 changes: 7 additions & 11 deletions src/Foundation/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,14 @@ private function runDispatcher(array $route) : void
}
}

private function runRoute(string $controllerClass, int $returnType = Response::HTML, ?array $args = null) : void
private function runRoute($controller, int $returnType = Response::HTML, ?array $args = null) : void
{
if (!class_exists($controllerClass)) {
$message = "Controller has not class name as {$controllerClass}";
throw new \BadMethodCallException($message);
}
$controller = $controllerClass::factory($this->container, $args);
$actionOutput = $controller->respond();
if (isset($actionOutput['meta']['type']) && $actionOutput['meta']['type'] === Dispatcher::REDIRECT) {
$returnType = Router::REDIRECT;
}
$this->response->setResponse($returnType, $actionOutput, $controllerClass);
$controller = new Controller($this->container, $controller, $returnType, $args);
$this->response->setResponse(
$controller->getReturnType(),
$controller->getActionOutput(),
$controller->getControllerClass()
);
}

public function getResponse() : array
Expand Down
85 changes: 85 additions & 0 deletions src/Foundation/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);

namespace Selami\Foundation;

use Psr\Container\ContainerInterface;
use Selami\Stdlib\Resolver;
use ReflectionClass;
use BadMethodCallException;

class Controller
{
const SIMPLE_RESPONSE = 'simpleFactoryResponse';
const AUTOWIRED_RESPONSE = 'autowiredResponse';

private $container;
private $controller;
private $controllerClass;
private $returnType;
private $args;
private $actionOutput;

/**
* Controller constructor.
* @param ContainerInterface $container
* @param $controller
* @param int $returnType
* @param array|null $args
*/
public function __construct(
ContainerInterface $container,
string $controller, int $returnType = Response::HTML, ?array $args = null)
{
$this->container = $container;
$this->controller = $controller;
$this->returnType = $returnType;
$this->args = $args;

$this->autowiredResponse();
}

public function getControllerClass() : string
{
return $this->controllerClass;
}

public function getActionOutput() : array
{
return $this->actionOutput;
}

public function getReturnType() : int
{
return $this->returnType;
}

private function autowiredResponse() : void
{
$this->controllerClass = $this->controller;
if (!class_exists($this->controllerClass)) {
$message = "Controller has not class name as {$this->controllerClass}";
throw new BadMethodCallException($message);
}
$controllerConstructorArguments = Resolver::getParameterHints($this->controllerClass, '__construct');
$arguments = [];
foreach ($controllerConstructorArguments as $argumentName =>$argumentType) {
$arguments[] = $this->getArgument($argumentName, $argumentType);
}
$reflectionClass = new ReflectionClass($this->controllerClass);
$controller = $reflectionClass->newInstanceArgs($arguments);
$this->actionOutput = $controller->__invoke();
if (isset($this->actionOutput['meta']['type']) && $this->actionOutput['meta']['type'] === Dispatcher::REDIRECT) {
$this->returnType = Router::REDIRECT;
}
}

private function getArgument(string $argumentName, string $argumentType)
{
if ($argumentType === Resolver::ARRAY) {
return $this->{$argumentName};
}
return $this->container->get($argumentType);
}

}
7 changes: 4 additions & 3 deletions src/Foundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Zend\Config\Config as ZendConfig;
use Psr\Container\ContainerInterface;
use Selami\View\ViewInterface;
use Selami\Stdlib\CaseConverter;
use Symfony\Component\HttpFoundation\Session\Session;

class Response
Expand Down Expand Up @@ -115,6 +116,7 @@ public function setDownloadResponse(array $actionOutput) : void
$this->downloadFileName = $actionOutput['meta']['download_file_name'] ?? date('Ymdhis');
}
}

public function setJsonResponse(array $actionOutput) : void
{
$this->contentType = Selami\Router::JSON;
Expand Down Expand Up @@ -145,7 +147,7 @@ private function renderResponse(int $returnType, array $actionOutput, string $co
$paths = explode("\\", $controllerClass);
$templateFile = array_pop($paths);
$templateFolder = array_pop($paths);
$template = strtolower($templateFolder) . '/' . strtolower($templateFile) . '.twig';
$template = CaseConverter::toSnakeCase($templateFolder) . '/' . CaseConverter::toSnakeCase($templateFile) . '.twig';
$this->checkTemplateFile($template, 'Method\'s', $controllerClass);
$actionOutput['data'] = $actionOutput['data'] ?? [];
$output = [
Expand All @@ -155,7 +157,7 @@ private function renderResponse(int $returnType, array $actionOutput, string $co
];
$output['app']['_content'] = $this->view->render($template, $actionOutput['data']);
$mainTemplateName = $actionOutput['meta']['layout'] ?? 'default';
$mainTemplate = '_' . strtolower($mainTemplateName) . '.twig';
$mainTemplate = '_' . CaseConverter::toSnakeCase($mainTemplateName) . '.twig';
$this->checkTemplateFile($mainTemplate, 'Layout', $controllerClass);
$this->contentType = $returnType;
if ($returnType === Selami\Router::CUSTOM) {
Expand All @@ -164,7 +166,6 @@ private function renderResponse(int $returnType, array $actionOutput, string $co
$this->body = $this->view->render($mainTemplate, $output);
}


public function notFound($status = 404, $returnType = Selami\Router::HTML, $message = 'Not Found') : void
{
if ($returnType === Selami\Router::JSON) {
Expand Down
3 changes: 1 addition & 2 deletions src/Interfaces/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

interface Application
{
public function respond(): array;
public static function factory(ContainerInterface $container, ?array $args) : Application;
public function __invoke(): array;
}

0 comments on commit 80dbdea

Please sign in to comment.