Passed
Branch master (93b6ae)
by refat
12:48
created

Application::share()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 8
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
   * Application Object
20
   *
21
   * @var \System\Application
22
   */
23
  private static $instance;
24
25
  /**
26
   * Constructor
27
   *
28
   * @param \System\File $file
29
   */
30
  private function __construct(File $file)
31
  {
32
    $this->share('file', $file);
33
34
    Dotenv::createImmutable($this->file->root())->load();
35
36
    $this->file->call('Core/helpers.php');
37
38
    $this->error->toggleError();
39
40
    register_shutdown_function([$this->error, 'handleErrors']);
41
  }
42
43
  /**
44
   * Get Application instance
45
   *
46
   * @param \System\File $file
47
   * @return \System\Application
48
   */
49
  public static function getInstance($file)
50
  {
51
    self::$instance = is_null(self::$instance) ? new static($file) : self::$instance;
52
53
    return self::$instance;
54
  }
55
56
  /**
57
   * Run the Application
58
   *
59
   * @return void
60
   */
61
  public function run()
62
  {
63
    $this->session->start();
64
65
    $this->request->prepareUrl();
66
67
    foreach (glob("routes/**/*.php") as $route) {
68
69
      $this->file->call($route);
70
    }
71
72
    $output = $this->route->getProperRoute();
73
74
    $this->response->setOutput($output);
75
76
    $this->response->send();
77
  }
78
79
  /**
80
   * Get all core classes
81
   *
82
   * @return array
83
   */
84
  public function coreClasses()
85
  {
86
    return $this->file->call('config/alias.php')['classes'];
87
  }
88
89
  /**
90
   * Share the given key|value through Application
91
   *
92
   * @param string $key
93
   * @param mixed $value
94
   * @return void
95
   */
96
  public function share($key, $value)
97
  {
98
    if ($value instanceof Closure) {
99
100
      $value = call_user_func($value, $this);
101
    }
102
103
    $this->container[$key] = $value;
104
  }
105
106
  /**
107
   * Get shared value
108
   *
109
   * @param string $key
110
   * @return mixed
111
   */
112
  public function get($key)
113
  {
114
    if (!$this->isSharing($key)) {
115
116
      if ($this->isCoreAlias($key)) {
117
118
        $this->share($key, $this->createObject($key));
119
120
      } else {
121
122
        throw new Exception("$key is not found");
123
      }
124
    }
125
126
    return $this->container[$key];
127
  }
128
129
  /**
130
   * Determine if the given key is shared through Application
131
   *
132
   * @param string $key
133
   * @return bool
134
   */
135
  public function isSharing($key)
136
  {
137
    return isset($this->container[$key]);
138
  }
139
140
  /**
141
   * Determine if the given key is an alias to core class
142
   *
143
   * @param string $key
144
   * @return bool
145
   */
146
  public function isCoreAlias($key)
147
  {
148
    return isset($this->coreClasses()[$key]);
149
  }
150
151
  /**
152
   * Create new object for the core class based on the given key
153
   *
154
   * @param string $key
155
   * @return object
156
   */
157
  public function createObject($key)
158
  {
159
    $object = $this->coreClasses()[$key];
160
161
    return new $object($this);
162
  }
163
164
  /**
165
   * Get shared value dynamically
166
   *
167
   * @param string $key
168
   * @return mixed
169
   */
170
  public function __get($key)
171
  {
172
    return $this->get($key);
173
  }
174
}
175