44
55namespace TinyBlocks \DockerContainer ;
66
7- use Symfony \Component \Process \Process ;
87use TinyBlocks \DockerContainer \Contracts \ContainerStarted ;
98use TinyBlocks \DockerContainer \Internal \Client \DockerClient ;
109use TinyBlocks \DockerContainer \Internal \CommandHandler \CommandHandler ;
1110use TinyBlocks \DockerContainer \Internal \CommandHandler \ContainerCommandHandler ;
1211use TinyBlocks \DockerContainer \Internal \Commands \DockerPull ;
1312use TinyBlocks \DockerContainer \Internal \Commands \DockerRun ;
13+ use TinyBlocks \DockerContainer \Internal \Containers \ContainerReaper ;
1414use TinyBlocks \DockerContainer \Internal \Containers \Definitions \ContainerDefinition ;
15+ use TinyBlocks \DockerContainer \Internal \Containers \Reused ;
16+ use TinyBlocks \DockerContainer \Internal \Containers \ShutdownHook ;
1517use TinyBlocks \DockerContainer \Waits \ContainerWaitAfterStarted ;
1618use TinyBlocks \DockerContainer \Waits \ContainerWaitBeforeStarted ;
1719
1820class GenericDockerContainer implements DockerContainer
1921{
2022 protected ContainerDefinition $ definition ;
2123
22- private ?Process $ imagePullProcess = null ;
23-
2424 private ?ContainerWaitBeforeStarted $ waitBeforeStarted = null ;
2525
26- protected function __construct (ContainerDefinition $ definition , private CommandHandler $ commandHandler )
27- {
26+ protected function __construct (
27+ private readonly ContainerReaper $ reaper ,
28+ ContainerDefinition $ definition ,
29+ private readonly CommandHandler $ commandHandler
30+ ) {
2831 $ this ->definition = $ definition ;
2932 }
3033
3134 public static function from (string $ image , ?string $ name = null ): static
3235 {
36+ $ client = new DockerClient ();
3337 $ definition = ContainerDefinition::create (image: $ image , name: $ name );
34- $ commandHandler = new ContainerCommandHandler (client: new DockerClient ());
38+ $ reaper = new ContainerReaper (client: $ client );
39+ $ commandHandler = new ContainerCommandHandler (client: $ client , shutdownHook: new ShutdownHook ());
3540
36- return new static (definition: $ definition , commandHandler: $ commandHandler );
41+ return new static (reaper: $ reaper , definition: $ definition , commandHandler: $ commandHandler );
3742 }
3843
3944 public function withNetwork (string $ name ): static
@@ -66,9 +71,7 @@ public function withEnvironmentVariable(string $key, string $value): static
6671
6772 public function pullImage (): static
6873 {
69- $ command = DockerPull::from (image: $ this ->definition ->image ->name );
70- $ this ->imagePullProcess = Process::fromShellCommandline (command: $ command ->toCommandLine ());
71- $ this ->imagePullProcess ->start ();
74+ $ this ->commandHandler ->execute (command: DockerPull::from (image: $ this ->definition ->image ->name ));
7275
7376 return $ this ;
7477 }
@@ -103,22 +106,8 @@ public function withVolumeMapping(string $pathOnHost, string $pathOnContainer):
103106 return $ this ;
104107 }
105108
106- public function runIfNotExists (
107- array $ commands = [],
108- ?ContainerWaitAfterStarted $ waitAfterStarted = null
109- ): ContainerStarted {
110- $ existing = $ this ->commandHandler ->findBy (definition: $ this ->definition );
111-
112- if (!is_null ($ existing )) {
113- return $ existing ;
114- }
115-
116- return $ this ->run (commands: $ commands , waitAfterStarted: $ waitAfterStarted );
117- }
118-
119109 public function run (array $ commands = [], ?ContainerWaitAfterStarted $ waitAfterStarted = null ): ContainerStarted
120110 {
121- $ this ->imagePullProcess ?->wait();
122111 $ this ->waitBeforeStarted ?->waitBefore();
123112
124113 $ dockerRun = DockerRun::from (definition: $ this ->definition , commands: $ commands );
@@ -128,4 +117,20 @@ public function run(array $commands = [], ?ContainerWaitAfterStarted $waitAfterS
128117
129118 return $ containerStarted ;
130119 }
120+
121+ public function runIfNotExists (
122+ array $ commands = [],
123+ ?ContainerWaitAfterStarted $ waitAfterStarted = null
124+ ): ContainerStarted {
125+ $ existing = $ this ->commandHandler ->findBy (definition: $ this ->definition );
126+
127+ if (!is_null ($ existing )) {
128+ return new Reused (reaper: $ this ->reaper , containerStarted: $ existing );
129+ }
130+
131+ return new Reused (
132+ reaper: $ this ->reaper ,
133+ containerStarted: $ this ->run (commands: $ commands , waitAfterStarted: $ waitAfterStarted )
134+ );
135+ }
131136}
0 commit comments