@@ -33,6 +33,8 @@ class CLI
3333 */
3434 protected Container $ container ;
3535
36+ protected ?Container $ parentContainer = null ;
37+
3638 /**
3739 * Args
3840 *
@@ -83,10 +85,11 @@ class CLI
8385 *
8486 * @param Adapter|null $adapter
8587 * @param array $args
88+ * @param Container|null $container
8689 *
8790 * @throws Exception
8891 */
89- public function __construct (?Adapter $ adapter = null , array $ args = [])
92+ public function __construct (?Adapter $ adapter = null , array $ args = [], ? Container $ container = null )
9093 {
9194 if (\php_sapi_name () !== 'cli ' ) {
9295 throw new Exception ('CLI tasks can only work from the command line ' );
@@ -97,7 +100,8 @@ public function __construct(?Adapter $adapter = null, array $args = [])
97100 @\cli_set_process_title ($ this ->command );
98101
99102 $ this ->adapter = $ adapter ?? new Generic ();
100- $ this ->container = new Container ();
103+ $ this ->parentContainer = $ container ;
104+ $ this ->container = new Container ($ container );
101105 }
102106
103107 /**
@@ -213,6 +217,11 @@ public function setResource(string $name, callable $callback, array $dependencie
213217 $ this ->container ->set ($ name , $ callback , $ dependencies );
214218 }
215219
220+ public function getContainer (): Container
221+ {
222+ return $ this ->container ;
223+ }
224+
216225 /**
217226 * task-name --foo=test
218227 *
@@ -401,16 +410,17 @@ protected function validate(string $key, array $param, $value): void
401410 }
402411 }
403412
404- public function setContainer ($ container ): self
413+ public function setContainer (Container $ container ): self
405414 {
406- $ this ->container = $ container ;
415+ $ this ->parentContainer = $ container ;
416+ $ this ->container = new Container ($ container );
407417
408418 return $ this ;
409419 }
410420
411421 public function reset (): void
412422 {
413- $ this ->container = new Container ();
423+ $ this ->container = new Container ($ this -> parentContainer );
414424 }
415425
416426 private function camelCaseIt ($ key ): string
0 commit comments