o2system /
framework
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Framework\Http; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Framework\Containers\Modules\DataStructures\Module\Theme; |
||||
| 19 | use O2System\Spl\Patterns\Structural\Repository\AbstractRepository; |
||||
| 20 | use O2System\Spl\DataStructures\SplArrayObject; |
||||
| 21 | use O2System\Spl\Traits\Collectors\ConfigCollectorTrait; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * Class Presenter |
||||
| 25 | * |
||||
| 26 | * @package O2System\Framework\Http |
||||
| 27 | */ |
||||
| 28 | class Presenter extends AbstractRepository |
||||
| 29 | { |
||||
| 30 | use ConfigCollectorTrait; |
||||
| 31 | |||||
| 32 | /** |
||||
| 33 | * Presenter::$meta |
||||
| 34 | * |
||||
| 35 | * @var Presenter\Meta |
||||
| 36 | */ |
||||
| 37 | public $meta; |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * Presenter::$page |
||||
| 41 | * |
||||
| 42 | * @var Presenter\Page |
||||
| 43 | */ |
||||
| 44 | public $page; |
||||
| 45 | |||||
| 46 | /** |
||||
| 47 | * Presenter::$assets |
||||
| 48 | * |
||||
| 49 | * @var Presenter\Assets |
||||
| 50 | */ |
||||
| 51 | public $assets; |
||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * Presenter::$partials |
||||
| 55 | * |
||||
| 56 | * @var Presenter\Repositories\Partials |
||||
| 57 | */ |
||||
| 58 | public $partials; |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * Presenter::$widgets |
||||
| 62 | * |
||||
| 63 | * @var Presenter\Repositories\Widgets |
||||
| 64 | */ |
||||
| 65 | public $widgets; |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * Presenter::$theme |
||||
| 69 | * |
||||
| 70 | * @var bool|Theme |
||||
| 71 | */ |
||||
| 72 | public $theme = false; |
||||
| 73 | |||||
| 74 | // ------------------------------------------------------------------------ |
||||
| 75 | |||||
| 76 | /** |
||||
| 77 | * Presenter::__construct |
||||
| 78 | */ |
||||
| 79 | public function __construct() |
||||
| 80 | { |
||||
| 81 | loader()->helper('Url'); |
||||
| 82 | |||||
| 83 | $this->meta = new Presenter\Meta; |
||||
| 84 | $this->page = new Presenter\Page; |
||||
| 85 | $this->assets = new Presenter\Assets; |
||||
| 86 | $this->partials = new Presenter\Repositories\Partials(); |
||||
| 87 | $this->widgets = new Presenter\Repositories\Widgets(); |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | // ------------------------------------------------------------------------ |
||||
| 91 | |||||
| 92 | /** |
||||
| 93 | * Presenter::initialize |
||||
| 94 | * |
||||
| 95 | * @param array $config |
||||
| 96 | * |
||||
| 97 | * @return static |
||||
| 98 | */ |
||||
| 99 | public function initialize(array $config = []) |
||||
| 100 | { |
||||
| 101 | if (count($config)) { |
||||
| 102 | $this->setConfig($config); |
||||
| 103 | } elseif (false !== ($config = config('view')->presenter)) { |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 104 | $this->setConfig($config); |
||||
| 105 | } |
||||
| 106 | |||||
| 107 | // autoload presenter assets |
||||
| 108 | if (isset($config[ 'assets' ])) { |
||||
| 109 | $this->assets->autoload($config[ 'assets' ]); |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | // autoload presenter theme |
||||
| 113 | if(isset($config['theme'])) { |
||||
| 114 | $this->setTheme($config['theme']); |
||||
| 115 | } |
||||
| 116 | |||||
| 117 | return $this; |
||||
| 118 | } |
||||
| 119 | |||||
| 120 | // ------------------------------------------------------------------------ |
||||
| 121 | |||||
| 122 | /** |
||||
| 123 | * Presenter::setTheme |
||||
| 124 | * |
||||
| 125 | * @param string $theme |
||||
| 126 | * |
||||
| 127 | * @return static |
||||
| 128 | */ |
||||
| 129 | public function setTheme($theme) |
||||
| 130 | { |
||||
| 131 | if($this->theme instanceof Theme) { |
||||
| 132 | $this->assets->removeFilePath($this->theme->getRealPath()); |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | if (is_bool($theme)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 136 | $this->theme = false; |
||||
| 137 | } elseif(($moduleTheme = modules()->top()->getTheme($theme, true)) instanceof Theme) { |
||||
|
0 ignored issues
–
show
The method
top() does not exist on O2System\Framework\Conta...s\DataStructures\Module.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 138 | $this->theme = $moduleTheme; |
||||
| 139 | } elseif(($appTheme = modules()->first()->getTheme($theme, true)) instanceof Theme) { |
||||
|
0 ignored issues
–
show
The method
first() does not exist on O2System\Framework\Conta...s\DataStructures\Module.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 140 | $this->theme = $appTheme; |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | if($this->theme) { |
||||
| 144 | if ( ! defined('PATH_THEME')) { |
||||
| 145 | define('PATH_THEME', $this->theme->getRealPath()); |
||||
| 146 | } |
||||
| 147 | |||||
| 148 | // add theme assets directory |
||||
| 149 | $this->assets->pushFilePath($this->theme->getRealPath()); |
||||
| 150 | |||||
| 151 | if(is_dir($themeViewPath = $this->theme->getRealPath() . 'views' . DIRECTORY_SEPARATOR)) { |
||||
| 152 | |||||
| 153 | // add theme view directory |
||||
| 154 | view()->addFilePath($this->theme->getRealPath()); |
||||
| 155 | |||||
| 156 | // add theme output directory |
||||
| 157 | output()->pushFilePath($themeViewPath); |
||||
| 158 | |||||
| 159 | $modules = modules()->getArrayCopy(); |
||||
|
0 ignored issues
–
show
The method
getArrayCopy() does not exist on O2System\Framework\Conta...s\DataStructures\Module.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 160 | |||||
| 161 | foreach($modules as $module) { |
||||
| 162 | if ( ! in_array($module->getType(), ['KERNEL', 'FRAMEWORK', 'APP'])) { |
||||
| 163 | $moduleResourcesPath = str_replace(PATH_RESOURCES, '', $module->getResourcesDir()); |
||||
| 164 | |||||
| 165 | if(is_dir($themeViewPath . $moduleResourcesPath)) { |
||||
| 166 | view()->pushFilePath($themeViewPath . $moduleResourcesPath); |
||||
| 167 | } |
||||
| 168 | } |
||||
| 169 | } |
||||
| 170 | } |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | return $this; |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | // ------------------------------------------------------------------------ |
||||
| 177 | |||||
| 178 | /** |
||||
| 179 | * Presenter::store |
||||
| 180 | * |
||||
| 181 | * @param string $offset |
||||
| 182 | * @param mixed $value |
||||
| 183 | * @param bool $replace |
||||
| 184 | */ |
||||
| 185 | public function store($offset, $value, $replace = false) |
||||
| 186 | { |
||||
| 187 | if ($value instanceof \Closure) { |
||||
| 188 | parent::store($offset, call_user_func($value, $this)); |
||||
| 189 | } else { |
||||
| 190 | parent::store($offset, $value); |
||||
| 191 | } |
||||
| 192 | } |
||||
| 193 | |||||
| 194 | // ------------------------------------------------------------------------ |
||||
| 195 | |||||
| 196 | /** |
||||
| 197 | * Presenter::include |
||||
| 198 | * |
||||
| 199 | * @param string $filename |
||||
| 200 | * @param array $vars |
||||
| 201 | * |
||||
| 202 | * @return string |
||||
| 203 | */ |
||||
| 204 | public function include($filename, array $vars = []) |
||||
| 205 | { |
||||
| 206 | return view()->load($filename, $vars, true); |
||||
| 207 | } |
||||
| 208 | |||||
| 209 | // ------------------------------------------------------------------------ |
||||
| 210 | |||||
| 211 | /** |
||||
| 212 | * Presenter::getArrayCopy |
||||
| 213 | * |
||||
| 214 | * @return array |
||||
| 215 | */ |
||||
| 216 | public function getArrayCopy() |
||||
| 217 | { |
||||
| 218 | $storage = $this->storage; |
||||
| 219 | |||||
| 220 | // Add Properties |
||||
| 221 | $storage[ 'meta' ] = $this->meta; |
||||
| 222 | $storage[ 'page' ] = $this->page; |
||||
| 223 | $storage[ 'assets' ] = new SplArrayObject([ |
||||
| 224 | 'head' => $this->assets->getHead(), |
||||
| 225 | 'body' => $this->assets->getBody(), |
||||
| 226 | ]); |
||||
| 227 | $storage[ 'partials' ] = $this->partials; |
||||
| 228 | $storage[ 'widgets' ] = $this->widgets; |
||||
| 229 | $storage[ 'theme' ] = $this->theme; |
||||
| 230 | |||||
| 231 | // Add Services |
||||
| 232 | $storage[ 'config' ] = config(); |
||||
| 233 | $storage[ 'language' ] = language(); |
||||
| 234 | $storage[ 'session' ] = session(); |
||||
| 235 | $storage[ 'presenter' ] = presenter(); |
||||
| 236 | $storage[ 'input' ] = input(); |
||||
| 237 | |||||
| 238 | if (services()->has('csrfProtection')) { |
||||
| 239 | $storage[ 'csrfToken' ] = services()->get('csrfProtection')->getToken(); |
||||
| 240 | } |
||||
| 241 | |||||
| 242 | return $storage; |
||||
| 243 | } |
||||
| 244 | |||||
| 245 | // ------------------------------------------------------------------------ |
||||
| 246 | |||||
| 247 | /** |
||||
| 248 | * Presenter::get |
||||
| 249 | * |
||||
| 250 | * @param string $property |
||||
| 251 | * |
||||
| 252 | * @return mixed |
||||
| 253 | */ |
||||
| 254 | public function get($property) |
||||
| 255 | { |
||||
| 256 | // CodeIgniter property aliasing |
||||
| 257 | if ($property === 'load') { |
||||
| 258 | $property = 'loader'; |
||||
| 259 | } |
||||
| 260 | |||||
| 261 | if (services()->has($property)) { |
||||
| 262 | return services()->get($property); |
||||
| 263 | } elseif ($property === 'model') { |
||||
| 264 | return models('controller'); |
||||
| 265 | } elseif ($property === 'services' || $property === 'libraries') { |
||||
| 266 | return services(); |
||||
| 267 | } elseif (method_exists($this, $property)) { |
||||
| 268 | return call_user_func([&$this, $property]); |
||||
| 269 | } |
||||
| 270 | |||||
| 271 | return parent::get($property); |
||||
| 272 | } |
||||
| 273 | |||||
| 274 | // ------------------------------------------------------------------------ |
||||
| 275 | |||||
| 276 | /** |
||||
| 277 | * Presenter::__call |
||||
| 278 | * |
||||
| 279 | * @param string $method |
||||
| 280 | * @param array $args |
||||
| 281 | * |
||||
| 282 | * @return mixed |
||||
| 283 | */ |
||||
| 284 | public function __call($method, array $args = []) |
||||
| 285 | { |
||||
| 286 | if (method_exists($this, $method)) { |
||||
| 287 | return call_user_func_array([$this, $method], $args); |
||||
| 288 | } |
||||
| 289 | } |
||||
| 290 | } |
||||
| 291 |