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

Commit

Permalink
reorganized configs and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Dec 20, 2018
1 parent f3c78d8 commit 75549f3
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 52 deletions.
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
"psr/container": "^1.0",
"psr/http-message": "^1.0",
"selami/stdlib": "^1.8",
"selami/router": "^1.0",
"selami/views": "~0.8",
"selami/router": "^1.1",
"selami/views": "^0.9",
"zendframework/zend-config": "^3.2",
"zendframework/zend-servicemanager": "^3.3",
"zendframework/zend-diactoros": "^1.8",
"zendframework/zend-httphandlerrunner": "^1.0",
"zendframework/zend-servicemanager": "^3.3"
"zendframework/zend-httphandlerrunner": "^1.0"
},
"require-dev": {
"roave/security-advisories": "dev-master",
Expand All @@ -31,7 +31,9 @@
"symfony/http-foundation": "^4.2",
"squizlabs/php_codesniffer": "^3.4",
"phpstan/phpstan": "^0.10.6",
"zendframework/zend-stratigility": "^3.0"
"zendframework/zend-stratigility": "^3.0",
"twig/twig": "^2.6",
"twig/extensions": "^1.5"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 7 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class Application implements RequestHandlerInterface
{

private $id;
/**
* @var ContainerInterface
*/
Expand All @@ -40,18 +41,21 @@ class Application implements RequestHandlerInterface
private $request;

public function __construct(
string $id,
ContainerInterface $container,
Router $router,
Config $config
) {
$this->id = $id;
$this->config = $config;
$this->router = $router;
$this->container = $container;
}

public static function createWithContainer(ContainerInterface $container) : self
public static function createWithContainer(ContainerInterface $container, ?string $id = 'selami-app') : self
{
return new self(
$id,
$container,
$container->get(Router::class),
$container->get(Config::class)
Expand Down Expand Up @@ -112,8 +116,8 @@ private function getRoute() : Route
$this->router = $this->router
->withDefaultReturnType($this->config->app->get('default_return_type', Router::HTML))
->withSubFolder($this->config->app->get('app_sub_folder', ''));
$cacheFile = $this->config->app->get('route_cache_file', null);
if ($cacheFile !== null) {
$cacheFile = $this->config->app->get('router_cache_file-' . $this->id, null);
if ((bool) $cacheFile) {
$this->router = $this->router
->withCacheFile($cacheFile);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ApplicationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ private function renderResponse() : string
$template = CaseConverter::toSnakeCase($templateFolder)
. '/' . CaseConverter::toSnakeCase($templateFile);
$layout = $this->controllerResponse->getMetaData()['layout'] ?? $template;
$templatePath = $layout. '.twig';
$templatePath = $layout. '.' . $this->config->view->get('template_file_extension');

$this->checkTemplateFile($templatePath, 'Method\'s', $this->controllerClass);
return $this->view->render($templatePath, $this->controllerResponse->getData());
}

private function checkTemplateFile($template, $type, $controller) : void
{
if (!file_exists($this->config->app->get('templates_path') .'/'. $template)) {
if (!file_exists($this->config->view->get('templates_path') .'/'. $template)) {
$message = sprintf(
'%s template file not found! %s needs a main template file at: %s',
$type,
Expand Down
2 changes: 1 addition & 1 deletion test/resources/app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
$stack = new EmitterStack();
$stack->push(new SapiEmitter());

$myApp = Selami\Application::createWithContainer($container);
$myApp = Selami\Application::createWithContainer($container, 'selami-example-app');

$runner = new RequestHandlerRunner(
$myApp,
Expand Down
21 changes: 0 additions & 21 deletions test/resources/app/config/autoload/app.global.php

This file was deleted.

2 changes: 0 additions & 2 deletions test/resources/app/config/autoload/dependencies.global.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
declare(strict_types=1);



return [
'dependencies' => []
];
8 changes: 8 additions & 0 deletions test/resources/app/config/autoload/global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

return [
'app' => [
'app_namespace' => 'MyApp',
],
];
19 changes: 5 additions & 14 deletions test/resources/app/config/autoload/local.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<?php
declare(strict_types=1);

use Selami\Router\Router;

return [
'config_cache_enabled' => false,
'routes' => [
[Router::GET, '/', MyApp\Contents\Main::class, Router::HTML, 'home'],
[Router::GET, '/category/{category}', MyApp\Contents\Category::class, Router::HTML, 'category'],
[Router::GET, '/{year}/{month}/{slug}', MyApp\Contents\Post::class, Router::JSON, 'post'],
[Router::GET, '/download', MyApp\Contents\Download::class, Router::DOWNLOAD],
[Router::GET, '/text', MyApp\Contents\Text::class, Router::TEXT],
[Router::GET, '/custom', MyApp\Contents\Custom::class, Router::CUSTOM],
[Router::GET, '/redirect', MyApp\Contents\Redirect::class, Router::REDIRECT],
[Router::GET, '/redirected', MyApp\Contents\Redirected::class, Router::HTML],
[Router::POST, '/405', MyApp\Contents\NotFound::class, Router::JSON]
]
'app' => [
'cache_dir' => './cache',
'config_cache_enabled' => false, // enable on production
'router_cache_file-selami-example-app' => false, // './cache/fastroute.selami-example-app.cache'
],
];
10 changes: 10 additions & 0 deletions test/resources/app/config/autoload/local.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

return [
'app' => [
'cache_dir' => './cache',
'config_cache_enabled' => true, // enable on production
'router_cache_file-selami-example-app' => './cache/fastroute.selami-example-app.cache'
],
];
18 changes: 18 additions & 0 deletions test/resources/app/config/autoload/routes.global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

use Selami\Router\Router;

return [
'routes' => [
[Router::GET, '/', MyApp\Contents\Main::class, Router::HTML, 'home'],
[Router::GET, '/category/{category}', MyApp\Contents\Category::class, Router::HTML, 'category'],
[Router::GET, '/{year}/{month}/{slug}', MyApp\Contents\Post::class, Router::JSON, 'post'],
[Router::GET, '/download', MyApp\Contents\Download::class, Router::DOWNLOAD],
[Router::GET, '/text', MyApp\Contents\Text::class, Router::TEXT],
[Router::GET, '/custom', MyApp\Contents\Custom::class, Router::CUSTOM],
[Router::GET, '/redirect', MyApp\Contents\Redirect::class, Router::REDIRECT],
[Router::GET, '/redirected', MyApp\Contents\Redirected::class, Router::HTML],
[Router::POST, '/405', MyApp\Contents\NotFound::class, Router::JSON]
]
];
18 changes: 18 additions & 0 deletions test/resources/app/config/autoload/view.local.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

return [
'view' => [
'type' => 'twig',
'templates_path' => './templates',
'template_file_extension' => 'twig',
'twig' => [
'debug' => true, // disable on production
'strict_variables' => true, // disable on production
'autoescape' => 'html',
'cache' => false, // change to a valid file path to enable caching on production
'auto_reload' => true, // disable on production
'optimizations' => 0 // change to -1 on production
],
]
];
18 changes: 18 additions & 0 deletions test/resources/app/config/autoload/view.local.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

return [
'view' => [
'type' => 'twig',
'templates_path' => './templates',
'template_file_extension' => 'twig',
'twig' => [
'debug' => false, // disable on production
'strict_variables' => false, // disable on production
'autoescape' => 'html',
'cache' => './cache/twig', // change to a valid file path to enable caching on production
'auto_reload' => false, // disable on production
'optimizations' => -1 // change to -1 on production
],
]
];
2 changes: 1 addition & 1 deletion test/resources/app/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
$config = ArrayUtils::merge($config, include $file);
}
// Cache config if enabled
if (isset($config['config_cache_enabled']) && $config['config_cache_enabled'] === true) {
if (isset($config['app']['config_cache_enabled']) && $config['app']['config_cache_enabled'] === true) {
file_put_contents($cachedConfigFile, json_encode($config));
}
}
Expand Down
5 changes: 2 additions & 3 deletions test/resources/app/config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
$container->setFactory(
TwigEnvironment::class,
function () use ($config) {
$loader = new Twig\Loader\FilesystemLoader($config['app']['templates_path']);
return new TwigEnvironment($loader, $config['app']);
$loader = new Twig\Loader\FilesystemLoader($config['view']['templates_path']);
return new TwigEnvironment($loader, $config['view']['twig']);
}
);

Expand All @@ -45,7 +45,6 @@ function () use ($config) {
function ($container) use ($config, $request) {

$viewConfig = $config['view'];
$viewConfig['templates_path'] = $config['app']['templates_path'];
$viewConfig['runtime']['query_parameters'] = $request->getQueryParams();
$viewConfig['runtime']['base_url'] = $config['app']['base_url'];
$viewConfig['runtime']['config'] = $config;
Expand Down

0 comments on commit 75549f3

Please sign in to comment.