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

Commit 75549f3

Browse files
committed
reorganized configs and dependencies
1 parent f3c78d8 commit 75549f3

14 files changed

+97
-52
lines changed

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
"psr/container": "^1.0",
1818
"psr/http-message": "^1.0",
1919
"selami/stdlib": "^1.8",
20-
"selami/router": "^1.0",
21-
"selami/views": "~0.8",
20+
"selami/router": "^1.1",
21+
"selami/views": "^0.9",
2222
"zendframework/zend-config": "^3.2",
23+
"zendframework/zend-servicemanager": "^3.3",
2324
"zendframework/zend-diactoros": "^1.8",
24-
"zendframework/zend-httphandlerrunner": "^1.0",
25-
"zendframework/zend-servicemanager": "^3.3"
25+
"zendframework/zend-httphandlerrunner": "^1.0"
2626
},
2727
"require-dev": {
2828
"roave/security-advisories": "dev-master",
@@ -31,7 +31,9 @@
3131
"symfony/http-foundation": "^4.2",
3232
"squizlabs/php_codesniffer": "^3.4",
3333
"phpstan/phpstan": "^0.10.6",
34-
"zendframework/zend-stratigility": "^3.0"
34+
"zendframework/zend-stratigility": "^3.0",
35+
"twig/twig": "^2.6",
36+
"twig/extensions": "^1.5"
3537
},
3638
"autoload": {
3739
"psr-4": {

src/Application.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
class Application implements RequestHandlerInterface
1717
{
1818

19+
private $id;
1920
/**
2021
* @var ContainerInterface
2122
*/
@@ -40,18 +41,21 @@ class Application implements RequestHandlerInterface
4041
private $request;
4142

4243
public function __construct(
44+
string $id,
4345
ContainerInterface $container,
4446
Router $router,
4547
Config $config
4648
) {
49+
$this->id = $id;
4750
$this->config = $config;
4851
$this->router = $router;
4952
$this->container = $container;
5053
}
5154

52-
public static function createWithContainer(ContainerInterface $container) : self
55+
public static function createWithContainer(ContainerInterface $container, ?string $id = 'selami-app') : self
5356
{
5457
return new self(
58+
$id,
5559
$container,
5660
$container->get(Router::class),
5761
$container->get(Config::class)
@@ -112,8 +116,8 @@ private function getRoute() : Route
112116
$this->router = $this->router
113117
->withDefaultReturnType($this->config->app->get('default_return_type', Router::HTML))
114118
->withSubFolder($this->config->app->get('app_sub_folder', ''));
115-
$cacheFile = $this->config->app->get('route_cache_file', null);
116-
if ($cacheFile !== null) {
119+
$cacheFile = $this->config->app->get('router_cache_file-' . $this->id, null);
120+
if ((bool) $cacheFile) {
117121
$this->router = $this->router
118122
->withCacheFile($cacheFile);
119123
}

src/ApplicationResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ private function renderResponse() : string
111111
$template = CaseConverter::toSnakeCase($templateFolder)
112112
. '/' . CaseConverter::toSnakeCase($templateFile);
113113
$layout = $this->controllerResponse->getMetaData()['layout'] ?? $template;
114-
$templatePath = $layout. '.twig';
114+
$templatePath = $layout. '.' . $this->config->view->get('template_file_extension');
115115

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

120120
private function checkTemplateFile($template, $type, $controller) : void
121121
{
122-
if (!file_exists($this->config->app->get('templates_path') .'/'. $template)) {
122+
if (!file_exists($this->config->view->get('templates_path') .'/'. $template)) {
123123
$message = sprintf(
124124
'%s template file not found! %s needs a main template file at: %s',
125125
$type,

test/resources/app/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
$stack = new EmitterStack();
2626
$stack->push(new SapiEmitter());
2727

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

3030
$runner = new RequestHandlerRunner(
3131
$myApp,

test/resources/app/config/autoload/app.global.php

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
declare(strict_types=1);
33

4-
5-
64
return [
75
'dependencies' => []
86
];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
return [
5+
'app' => [
6+
'app_namespace' => 'MyApp',
7+
],
8+
];
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Selami\Router\Router;
5-
64
return [
7-
'config_cache_enabled' => false,
8-
'routes' => [
9-
[Router::GET, '/', MyApp\Contents\Main::class, Router::HTML, 'home'],
10-
[Router::GET, '/category/{category}', MyApp\Contents\Category::class, Router::HTML, 'category'],
11-
[Router::GET, '/{year}/{month}/{slug}', MyApp\Contents\Post::class, Router::JSON, 'post'],
12-
[Router::GET, '/download', MyApp\Contents\Download::class, Router::DOWNLOAD],
13-
[Router::GET, '/text', MyApp\Contents\Text::class, Router::TEXT],
14-
[Router::GET, '/custom', MyApp\Contents\Custom::class, Router::CUSTOM],
15-
[Router::GET, '/redirect', MyApp\Contents\Redirect::class, Router::REDIRECT],
16-
[Router::GET, '/redirected', MyApp\Contents\Redirected::class, Router::HTML],
17-
[Router::POST, '/405', MyApp\Contents\NotFound::class, Router::JSON]
18-
]
5+
'app' => [
6+
'cache_dir' => './cache',
7+
'config_cache_enabled' => false, // enable on production
8+
'router_cache_file-selami-example-app' => false, // './cache/fastroute.selami-example-app.cache'
9+
],
1910
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
return [
5+
'app' => [
6+
'cache_dir' => './cache',
7+
'config_cache_enabled' => true, // enable on production
8+
'router_cache_file-selami-example-app' => './cache/fastroute.selami-example-app.cache'
9+
],
10+
];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Selami\Router\Router;
5+
6+
return [
7+
'routes' => [
8+
[Router::GET, '/', MyApp\Contents\Main::class, Router::HTML, 'home'],
9+
[Router::GET, '/category/{category}', MyApp\Contents\Category::class, Router::HTML, 'category'],
10+
[Router::GET, '/{year}/{month}/{slug}', MyApp\Contents\Post::class, Router::JSON, 'post'],
11+
[Router::GET, '/download', MyApp\Contents\Download::class, Router::DOWNLOAD],
12+
[Router::GET, '/text', MyApp\Contents\Text::class, Router::TEXT],
13+
[Router::GET, '/custom', MyApp\Contents\Custom::class, Router::CUSTOM],
14+
[Router::GET, '/redirect', MyApp\Contents\Redirect::class, Router::REDIRECT],
15+
[Router::GET, '/redirected', MyApp\Contents\Redirected::class, Router::HTML],
16+
[Router::POST, '/405', MyApp\Contents\NotFound::class, Router::JSON]
17+
]
18+
];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
return [
5+
'view' => [
6+
'type' => 'twig',
7+
'templates_path' => './templates',
8+
'template_file_extension' => 'twig',
9+
'twig' => [
10+
'debug' => true, // disable on production
11+
'strict_variables' => true, // disable on production
12+
'autoescape' => 'html',
13+
'cache' => false, // change to a valid file path to enable caching on production
14+
'auto_reload' => true, // disable on production
15+
'optimizations' => 0 // change to -1 on production
16+
],
17+
]
18+
];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
return [
5+
'view' => [
6+
'type' => 'twig',
7+
'templates_path' => './templates',
8+
'template_file_extension' => 'twig',
9+
'twig' => [
10+
'debug' => false, // disable on production
11+
'strict_variables' => false, // disable on production
12+
'autoescape' => 'html',
13+
'cache' => './cache/twig', // change to a valid file path to enable caching on production
14+
'auto_reload' => false, // disable on production
15+
'optimizations' => -1 // change to -1 on production
16+
],
17+
]
18+
];

test/resources/app/config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
$config = ArrayUtils::merge($config, include $file);
1818
}
1919
// Cache config if enabled
20-
if (isset($config['config_cache_enabled']) && $config['config_cache_enabled'] === true) {
20+
if (isset($config['app']['config_cache_enabled']) && $config['app']['config_cache_enabled'] === true) {
2121
file_put_contents($cachedConfigFile, json_encode($config));
2222
}
2323
}

test/resources/app/config/container.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
$container->setFactory(
3535
TwigEnvironment::class,
3636
function () use ($config) {
37-
$loader = new Twig\Loader\FilesystemLoader($config['app']['templates_path']);
38-
return new TwigEnvironment($loader, $config['app']);
37+
$loader = new Twig\Loader\FilesystemLoader($config['view']['templates_path']);
38+
return new TwigEnvironment($loader, $config['view']['twig']);
3939
}
4040
);
4141

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

4747
$viewConfig = $config['view'];
48-
$viewConfig['templates_path'] = $config['app']['templates_path'];
4948
$viewConfig['runtime']['query_parameters'] = $request->getQueryParams();
5049
$viewConfig['runtime']['base_url'] = $config['app']['base_url'];
5150
$viewConfig['runtime']['config'] = $config;

0 commit comments

Comments
 (0)