Passed
Branch master (08bb80)
by refat
04:48
created

Application::coreClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace System;
4
5
use Closure;
6
use Exception;
7
use Dotenv\Dotenv;
8
9
class Application
10
{
11
  /**
12
   * Container
13
   *
14
   * @var array
15
   */
16
  private $container = [];
17
18
  /**
19
   * Set and rename core classes
20
   *
21
   * @var array
22
   */
23
  private $coreClasses = [
24
    'request'   =>  'System\\Http\\Request',
25
    'response'  =>  'System\\Http\\Response',
26
    'route'     =>  'System\\Route',
27
    'session'   =>  'System\\Session',
28
    'cookie'    =>  'System\\Cookie',
29
    'load'      =>  'System\\Loader',
30
    'html'      =>  'System\\Html',
31
    'db'        =>  'System\\Database',
32
    'url'       =>  'System\\Url',
33
    'view'      =>  'System\\View',
34
    'hash'      =>  'System\\Hash',
35
    'error'     =>  'System\\Error',
36
    'email'     =>  'System\\Email'
37
  ];
38
39
  /**
40
   * Application Object
41
   *
42
   * @var \System\Application
43
   */
44
  private static $instance;
45
46
  /**
47
   * Constructor
48
   *
49
   * @param \System\File $file
50
   */
51
  private function __construct(File $file)
52
  {
53
    $this->share('file', $file);
54
55
    Dotenv::createImmutable($this->file->root())->load();
56
57
    $this->file->call('Core/helpers.php');
58
59
    $this->error->toggleError();
60
61
    register_shutdown_function([$this->error, 'handleErrors']);
62
  }
63
64
  /**
65
   * Get Application instance
66
   *
67
   * @param \System\File $file
68
   * @return \System\Application
69
   */
70
  public static function getInstance($file)
71
  {
72
    self::$instance = is_null(self::$instance) ? new static($file) : self::$instance;
73
74
    return self::$instance;
75
  }
76
77
  /**
78
   * Run the Application
79
   *
80
   * @return void
81
   */
82
  public function run()
83
  {
84
    $this->session->start();
85
86
    $this->request->prepareUrl();
87
88
    foreach (glob("routes/**/*.php") as $route) {
89
90
      $this->file->call($route);
91
    }
92
93
    $output = $this->route->getProperRoute();
94
95
    $this->response->setOutput($output);
96
97
    $this->response->send();
98
  }
99
100
  /**
101
   * Share the given key|value through Application
102
   *
103
   * @param string $key
104
   * @param mixed $value
105
   * @return void
106
   */
107
  public function share($key, $value)
108
  {
109
    if ($value instanceof Closure) {
110
111
      $value = call_user_func($value, $this);
112
    }
113
114
    $this->container[$key] = $value;
115
  }
116
117
  /**
118
   * Get shared value
119
   *
120
   * @param string $key
121
   * @return mixed
122
   */
123
  public function get($key)
124
  {
125
    if (!$this->isSharing($key)) {
126
127
      if ($this->isCoreAlias($key)) {
128
129
        $this->share($key, $this->createObject($key));
130
131
      } else {
132
        $found = false;
133
        $dirs = getAllSubDires('core/System/');
134
135
        foreach ($dirs as $dir) {
136
          $path = $this->file->fullPath($dir . ucwords($key)) . '.php';
137
138
          if (file_exists($path)) {
139
            $found = true;
140
141
            $dir = $this->file->fullPath($dir . ucwords($key));
142
            $dir = ltrim($dir, $this->file->root() . 'core');
143
144
            $this->coreClasses[$key] = $dir;
145
146
            $this->share($key, $this->createObject($key));
147
          }
148
        }
149
150
        if (!$found) {
151
          throw new Exception("$key is not found");
152
        }
153
      }
154
    }
155
156
    return $this->container[$key];
157
  }
158
159
  /**
160
   * Determine if the given key is shared through Application
161
   *
162
   * @param string $key
163
   * @return bool
164
   */
165
  public function isSharing($key)
166
  {
167
    return isset($this->container[$key]);
168
  }
169
170
  /**
171
   * Determine if the given key is an alias to core class
172
   *
173
   * @param string $key
174
   * @return bool
175
   */
176
  public function isCoreAlias($key)
177
  {
178
    return isset($this->coreClasses[$key]);
179
  }
180
181
  /**
182
   * Create new object for the core class based on the given key
183
   *
184
   * @param string $key
185
   * @return object
186
   */
187
  public function createObject($key)
188
  {
189
    $object = $this->coreClasses[$key];
190
191
    return new $object($this);
192
  }
193
194
  /**
195
   * Get shared value dynamically
196
   *
197
   * @param string $key
198
   * @return mixed
199
   */
200
  public function __get($key)
201
  {
202
    return $this->get($key);
203
  }
204
}
205