bytic /
container
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Nip\Container\Tests\Fixtures; |
||
| 4 | |||
| 5 | use ArrayAccess; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class Modules. |
||
| 9 | */ |
||
| 10 | class ModulesService implements ArrayAccess, \Countable |
||
| 11 | { |
||
| 12 | protected $modules = []; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Modules constructor. |
||
| 16 | */ |
||
| 17 | public function __construct($newModules = []) |
||
| 18 | { |
||
| 19 | $this->init(); |
||
| 20 | foreach ($newModules as $module) { |
||
| 21 | $this->addModule($module); |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | public function init() |
||
| 26 | { |
||
| 27 | $this->addModule('admin'); |
||
| 28 | $this->addModule('default'); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param $name |
||
| 33 | */ |
||
| 34 | public function addModule($name) |
||
| 35 | { |
||
| 36 | if (!$this->offsetExists($name)) { |
||
| 37 | $this->modules[$name] = $name; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Determine if a given offset exists. |
||
| 43 | * |
||
| 44 | * @param string $key |
||
| 45 | * |
||
| 46 | * @return bool |
||
| 47 | */ |
||
| 48 | public function offsetExists($key) |
||
| 49 | { |
||
| 50 | return array_key_exists($key, $this->modules); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param $name |
||
| 55 | * |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public function hasModule($name) |
||
| 59 | { |
||
| 60 | return $this->offsetExists($name); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getNames() |
||
| 67 | { |
||
| 68 | return $this->modules; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param $name |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function getViewPath($name) |
||
| 77 | { |
||
| 78 | return $this->getModuleDirectory($name).DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param $name |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getModuleDirectory($name) |
||
| 87 | { |
||
| 88 | return $this->getModulesBaseDirectory().$name; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function getModulesBaseDirectory() |
||
| 95 | { |
||
| 96 | return defined('MODULES_PATH') ? MODULES_PATH : ''; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the value at a given offset. |
||
| 101 | * |
||
| 102 | * @param string $key |
||
| 103 | * |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | public function offsetGet($key) |
||
| 107 | { |
||
| 108 | return $this->modules[$key]; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set the value at a given offset. |
||
| 113 | * |
||
| 114 | * @param string $key |
||
| 115 | * @param mixed $value |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | public function offsetSet($key, $value) |
||
| 120 | { |
||
| 121 | $this->modules[$key] = $value; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Unset the value at a given offset. |
||
| 126 | * |
||
| 127 | * @param string $key |
||
| 128 | * |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | public function offsetUnset($key) |
||
| 132 | { |
||
| 133 | unset($this->modules[$key]); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function count(): int |
||
| 137 | { |
||
| 138 | return count($this->modules); |
||
| 139 | } |
||
| 140 | } |
||
| 141 |