Skip to content

Commit 1beadcf

Browse files
committed
made config class ide friendly
1 parent 9ed888c commit 1beadcf

File tree

7 files changed

+200
-112
lines changed

7 files changed

+200
-112
lines changed

App/Config.php

+66-71
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,89 @@
22

33
namespace App;
44

5-
class Config
5+
use Core\Config as BaseConfig;
6+
7+
class Config extends BaseConfig
68
{
79
/**
8-
* Get Applcation Config
9-
*
10-
* @return array configs
10+
* Base Url of application
11+
* ex:
12+
* example.com/ => '' // Leave empty
13+
* example.com/myapp => 'myapp'
14+
* example.com/myapp/anotherapp => 'myapp/anotherapp'
1115
*/
12-
public static function getConfig()
13-
{
14-
return [
15-
/**
16-
* Base Url of application
17-
* ex:
18-
* example.com/ => '' // Leave empty
19-
* example.com/myapp => 'myapp'
20-
* example.com/myapp/anotherapp => 'myapp/anotherapp'
21-
*/
22-
'base_url' => 'oneklick/public',
23-
16+
const BASE_URL = 'oneklick/public';
17+
2418

25-
/**
26-
* Assets directory path relative to base_url
27-
*/
28-
'assets_path' => 'assets',
19+
/**
20+
* Assets directory path relative to base_url
21+
*/
22+
const ASSETS_PATH = 'assets';
2923

30-
/**
31-
* Which namespace controllers of application resides
32-
* Deafault is App\Controllers
33-
*/
34-
'namespace' => 'App\\Controllers',
24+
/**
25+
* Which namespace controllers of application resides
26+
* Deafault is App\Controllers
27+
*/
28+
const CONTROLLER_NAMESPACE = 'App\\Controllers';
3529

3630

37-
/**
38-
* Which controller will be used for simple /action urls
39-
* Deafult is Site
40-
* ex:
41-
* example.com/about :
42-
* will call about method on Site Controller.
43-
* example.com/about/1/edit :
44-
* will be same as above but 1 and edit are params to function.
45-
*/
46-
'default_controller' => 'Site',
31+
/**
32+
* Which controller will be used for simple /action urls
33+
* Deafult is Site
34+
* ex:
35+
* example.com/about :
36+
* will call about method on Site Controller.
37+
* example.com/about/1/edit :
38+
* will be same as above but 1 and edit are params to function.
39+
*/
40+
const DEFAULT_CONTROLLER = 'Site';
4741

4842

49-
/**
50-
* What action should / route trigger i.e. your landing site
51-
* By deafult it's index method in Deafult Controller(SiteController)
52-
*/
53-
'action_index' => 'index',
43+
/**
44+
* What action should / route trigger i.e. your landing site
45+
* By deafult it's index method in Deafult Controller(SiteController)
46+
*/
47+
const ACTION_INDEX = 'index';
5448

5549

56-
/**
57-
* If unknown page is requested (i.e. 404)
58-
* By deafult it's page404 method in Deafult Controller(SiteController)
59-
*/
60-
'action_404' => 'page404',
50+
/**
51+
* If unknown page is requested (i.e. 404)
52+
* By deafult it's page404 method in Deafult Controller(SiteController)
53+
*/
54+
const ACTION_404 = 'page404';
6155

6256

63-
/**
64-
* Path where twig view templates are stored
65-
*/
66-
'view_path' => __DIR__ . '/Views/',
57+
/**
58+
* Path where twig view templates are stored
59+
*/
60+
const VIEW_PATH = __DIR__ . '/Views/';
6761

6862

69-
/**
70-
* Path where twig to store compiled templates
71-
*/
72-
'view_cache_path' => dirname(__DIR__) . '/storage/views/',
63+
/**
64+
* Path where twig to store compiled templates
65+
*/
66+
const VIEW_CACHE_PATH = __DIR__ . '/../storage/views/';
7367

7468

75-
/**
76-
* Which superglobals to while making app instance
77-
*/
78-
'superglobals' => [
79-
'get' => $_GET,
80-
'post' => $_POST,
81-
'files' => $_FILES,
82-
'server' => $_SERVER,
83-
'cookie' => $_COOKIE,
84-
'request' => $_REQUEST
85-
],
69+
/**
70+
* Which superglobals to while making app instance
71+
*/
72+
public static function getGlobals()
73+
{
74+
return [
75+
'get' => $_GET,
76+
'post' => $_POST,
77+
'files' => $_FILES,
78+
'server' => $_SERVER,
79+
'cookie' => $_COOKIE,
80+
'request' => $_REQUEST
81+
];
82+
}
8683

8784

88-
/**
89-
* Default user model to use
90-
* default is App\Models\User
91-
*/
92-
'user_model' => 'User',
93-
];
94-
}
85+
/**
86+
* Default user model to use
87+
* default is App\Models\User
88+
*/
89+
const USER_MODEL = 'User';
9590
}

Core/Config.php

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Core;
4+
5+
class Config
6+
{
7+
/**
8+
* Base Url of application
9+
* ex:
10+
* example.com/ => '' // Leave empty
11+
* example.com/myapp => 'myapp'
12+
* example.com/myapp/anotherapp => 'myapp/anotherapp'
13+
*/
14+
const BASE_URL = 'oneklick/public';
15+
16+
17+
/**
18+
* Assets directory path relative to base_url
19+
*/
20+
const ASSETS_PATH = 'assets';
21+
22+
/**
23+
* Which namespace controllers of application resides
24+
* Deafault is App\Controllers
25+
*/
26+
const CONTROLLER_NAMESPACE = 'App\\Controllers';
27+
28+
29+
/**
30+
* Which controller will be used for simple /action urls
31+
* Deafult is Site
32+
* ex:
33+
* example.com/about :
34+
* will call about method on Site Controller.
35+
* example.com/about/1/edit :
36+
* will be same as above but 1 and edit are params to function.
37+
*/
38+
const DEFAULT_CONTROLLER = 'Site';
39+
40+
41+
/**
42+
* What action should / route trigger i.e. your landing site
43+
* By deafult it's index method in Deafult Controller(SiteController)
44+
*/
45+
const ACTION_INDEX = 'index';
46+
47+
48+
/**
49+
* If unknown page is requested (i.e. 404)
50+
* By deafult it's page404 method in Deafult Controller(SiteController)
51+
*/
52+
const ACTION_404 = 'page404';
53+
54+
55+
/**
56+
* Path where twig view templates are stored
57+
*/
58+
const VIEW_PATH = __DIR__ . '/Views/';
59+
60+
61+
/**
62+
* Path where twig to store compiled templates
63+
*/
64+
const VIEW_CACHE_PATH = __DIR__ . '/../storage/views/';
65+
66+
67+
/**
68+
* Which superglobals to while making app instance
69+
*/
70+
public static function getGlobals()
71+
{
72+
return [
73+
'get' => $_GET,
74+
'post' => $_POST,
75+
'files' => $_FILES,
76+
'server' => $_SERVER,
77+
'cookie' => $_COOKIE,
78+
'request' => $_REQUEST
79+
];
80+
}
81+
82+
83+
/**
84+
* Default user model to use
85+
* default is App\Models\User
86+
*/
87+
const USER_MODEL = 'User';
88+
}

Core/ResponseFactory.php

+25-9
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,43 @@
77
*/
88
class ResponseFactory {
99

10-
public static function view($config, $template, $data = [])
10+
11+
/**
12+
* Render view with twig
13+
*
14+
* @param \Core\Config $config
15+
* @param string $template
16+
* @param array $data
17+
* @return Response
18+
*/
19+
public static function view($config = \Core\Config::class, $template, $data = []): Response
1120
{
12-
$loader = new \Twig\Loader\FilesystemLoader($config['view_path']);
21+
$loader = new \Twig\Loader\FilesystemLoader($config::VIEW_PATH);
1322
$twig = new \Twig\Environment($loader, [
14-
'cache' => false
15-
// 'cache' => $config['view_cache_path']
23+
'cache' => $config::VIEW_CACHE_PATH
1624
]);
1725

18-
$asset = new \Twig\TwigFunction('asset', function ($path = '') use($config) {
19-
return $config['base_url'].'/'.$config['assets_path'].'/'.$path;
20-
});
26+
// $asset = new \Twig\TwigFunction('asset', function ($path = '') use($config) {
27+
// return $config::BASE_URL.'/'. $config::VIEW_PATH.'/'.$path;
28+
// });
2129

22-
$twig->addFunction($asset);
30+
// $twig->addFunction($asset);
2331

2432
$response = new Response($twig->render($template, $data), [
2533
'Content-Type'=> 'text/html'
2634
], 200);
2735
return $response;
2836
}
2937

30-
public static function json($data, $options = 0,$depth = 512)
38+
/**
39+
* Generate json response
40+
*
41+
* @param array $data
42+
* @param integer $options
43+
* @param integer $depth
44+
* @return Response
45+
*/
46+
public static function json($data, $options = 0,$depth = 512): Response
3147
{
3248
$response = new Response(json_encode($data, $options, $depth), [
3349
'Content-Type'=> 'text/json'

Core/Router.php

+10-16
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@
44

55
class Router
66
{
7-
/** @var array Configuration of application */
8-
protected $config = [
9-
'base_url' => '',
10-
'namespace' => 'App\\Controllers',
11-
'default_controller' => 'Site',
12-
'action_index' => 'index',
13-
'action_404' => 'page404',
14-
];
7+
/** @var \Core\Config */
8+
protected $config;
9+
1510

16-
1711
public function __construct($config)
1812
{
19-
$this->config = array_merge($this->config, $config);
13+
$this->config = $config;
2014
}
2115

2216

@@ -29,13 +23,13 @@ public function __construct($config)
2923
public function handle(Request $request): Response
3024
{
3125
$uri = $request->server('REQUEST_URI');
32-
$uri = str_replace($this->config['base_url'], "", $uri);
26+
$uri = str_replace($this->config::BASE_URL, "", $uri);
3327
$args = explode('/', trim($uri, '/'));
3428

3529
// Check if simple routes being used like /, /about, etc.
3630
if (1 === count($args)) {
37-
$controller = $this->config['default_controller'];
38-
$method = array_shift($args) ?: $this->config['action_index'];
31+
$controller = $this->config::DEFAULT_CONTROLLER;
32+
$method = array_shift($args) ?: $this->config::ACTION_INDEX;
3933
} else {
4034
$controller = array_shift($args);
4135
$method = array_shift($args);
@@ -86,12 +80,12 @@ protected function run($controller, $method, $args, Request $request): Response
8680
{
8781
// Add request object as a parameter
8882
array_unshift($args, $request);
89-
$class = "{$this->config['namespace']}\\{$controller}Controller";
83+
$class = $this->config::CONTROLLER_NAMESPACE."\\{$controller}Controller";
9084
if (class_exists($class) && is_callable($toCall = [new $class, $method])) {
9185
$result = call_user_func_array($toCall, $args);
9286
} else {
93-
$class = "{$this->config['namespace']}\\{$this->config['default_controller']}Controller";
94-
$result = call_user_func([new $class, $this->config['action_404']], $request);
87+
$class = $this->config::CONTROLLER_NAMESPACE.'\\'.$this->config::DEFAULT_CONTROLLER.'Controller';
88+
$result = call_user_func([new $class, $this->config::ACTION_404], $request);
9589
}
9690
return $result;
9791
}

0 commit comments

Comments
 (0)