44
55namespace MaplePHP \Container ;
66
7+ use Closure ;
78use MaplePHP \Container \Interfaces \ContainerInterface ;
89use MaplePHP \Container \Interfaces \FactoryInterface ;
910use MaplePHP \DTO \Format \Arr ;
10- use MaplePHP \Container \Reflection ;
11+ // use MaplePHP\Container\Reflection;
1112use MaplePHP \Container \Exceptions \NotFoundException ;
1213use MaplePHP \Container \Exceptions \ContainerException ;
14+ use ReflectionException ;
1315
1416class Container implements ContainerInterface, FactoryInterface
1517{
16- private $ services = array ();
17- private $ args ;
18- //private $overwrite;
19- private $ getter = array ();
20-
18+ private array $ services = [];
19+ private array $ args = [];
20+ private array $ getter = [];
2121
22+ /**
23+ * @throws ReflectionException
24+ */
2225 public function __call ($ method , $ args )
2326 {
2427 return $ this ->get ($ method , $ args );
@@ -32,14 +35,14 @@ public function __call($method, $args)
3235 * TestClasses\Test::class."::__construct",
3336 * TestClasses\Test::class."::getStaticMethod",
3437 * @param array|null $args Pass argumnets to constructor staticMethod if you choose.
35- * @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
38+ * @param bool $overwrite Will throw exception if already been defined if not arg is set to TRUE.
3639 */
3740 public function set (string $ identifier , $ value , ?array $ args = null , bool $ overwrite = false ): ContainerInterface
3841 {
3942 if (!$ overwrite && $ this ->has ($ identifier )) {
4043 $ type = ($ this ->isFactory ($ identifier )) ? "factory " : "container " ;
41- throw new ContainerException ("The { $ type} ( { $ identifier} ) has already been defined. If you want to overwrite " .
42- "the { $ type} then set overwrite argument to true. " , 1 );
44+ throw new ContainerException ("The $ type ( $ identifier) has already been defined. If you want to overwrite " .
45+ "the $ type then set overwrite argument to true. " , 1 );
4346 }
4447
4548 if (isset ($ this ->getter [$ identifier ])) {
@@ -54,18 +57,18 @@ public function set(string $identifier, $value, ?array $args = null, bool $overw
5457 * Same as @set, BUT will only accept a factory
5558 * @param string $identifier Uniq identifier
5659 * @param callable $factory
57- * @param bool|boolean $overwrite Will throw exception if already been defined if not arg is set to TRUE.
60+ * @param bool $overwrite Will throw exception if already been defined if not arg is set to TRUE.
5861 * @return self
5962 */
6063 public function factory (string $ identifier , callable $ factory , bool $ overwrite = false ): self
6164 {
6265 if (!$ overwrite && $ this ->has ($ identifier )) {
6366 if (!$ this ->isFactory ($ identifier )) {
64- throw new ContainerException ("( { $ identifier} ) Has already been defined, but has been defined as a " .
67+ throw new ContainerException ("( $ identifier) Has already been defined, but has been defined as a " .
6568 "container and not factory. If you want to overwrite the container as factory then set " .
6669 "overwrite argument to true. " , 1 );
6770 } else {
68- throw new ContainerException ("The factory ( { $ identifier} ) has already been defined. If you want to " .
71+ throw new ContainerException ("The factory ( $ identifier) has already been defined. If you want to " .
6972 "overwrite the factory then set overwrite argument to true. " , 1 );
7073 }
7174 }
@@ -92,26 +95,27 @@ public function has(string $identifier): bool
9295 * @param string $identifier Uniq identifier
9396 * @return boolean
9497 */
95- public function isFactory (string $ identifier )
98+ public function isFactory (string $ identifier ): bool
9699 {
97- return ($ this ->getService ($ identifier ) instanceof \ Closure);
100+ return ($ this ->getService ($ identifier ) instanceof Closure);
98101 }
99102
100103 /**
101104 * Check if is a container
102- * @param string $identifier Uniq identifier
105+ * @param string $identifier Uniq identifier
103106 * @return boolean
104107 */
105- public function isContainer ($ identifier )
108+ public function isContainer (string $ identifier ): bool
106109 {
107110 return (!$ this ->isFactory ($ identifier ));
108111 }
109112
110113 /**
111114 * Get a container or factory
112- * @param string $identifier [description]
113- * @param array $args Is possible to overwrite/add __construct or method argumnets
115+ * @param string $identifier [description]
116+ * @param array $args Is possible to overwrite/add __construct or method argumnets
114117 * @return mixed
118+ * @throws ReflectionException
115119 */
116120 public function get (string $ identifier , array $ args = []): mixed
117121 {
@@ -136,22 +140,23 @@ public function get(string $identifier, array $args = []): mixed
136140 }
137141 return $ this ->getter [$ identifier ];
138142 } else {
139- throw new NotFoundException ("Trying to get a container ( { $ identifier} ) that does not exists " , 1 );
143+ throw new NotFoundException ("Trying to get a container ( $ identifier) that does not exists " , 1 );
140144 }
141145 }
142146
143147
144148 /**
145149 * Fetch is used to load multiple container and factories at once with the help of a wildcard search
150+ * @param string $identifier
151+ * @return array
152+ * @throws ReflectionException
146153 * @example @set("event.session", \name\space\session::class)
147154 * ->set("event.traverse", \name\space\traverse::class)
148155 * ->fetch("event.*");
149- * @param string $identifier
150- * @return mixed
151156 */
152- public function fetch (string $ identifier )
157+ public function fetch (string $ identifier ): array
153158 {
154- if (strpos ($ identifier , "* " ) !== false ) {
159+ if (str_contains ($ identifier , "* " )) {
155160 $ arr = Arr::value ($ this ->services )->wildcardSearch ($ identifier )->get ();
156161 if (count ($ arr ) > 0 ) {
157162 $ new = array ();
0 commit comments