2
2
3
3
namespace Somnambulist \ProjectManager \Commands \Projects ;
4
4
5
+ use Exception ;
5
6
use Somnambulist \ProjectManager \Commands \AbstractCommand ;
7
+ use Somnambulist \ProjectManager \Commands \Behaviours \DockerAwareCommand ;
8
+ use Somnambulist \ProjectManager \Commands \Behaviours \GetCurrentActiveProject ;
6
9
use Somnambulist \ProjectManager \Commands \Behaviours \GetProjectFromInput ;
7
10
use Somnambulist \ProjectManager \Commands \Behaviours \ProjectConfigAwareCommand ;
8
11
use Somnambulist \ProjectManager \Commands \Behaviours \UseEnvironmentTemplate ;
12
+ use Somnambulist \ProjectManager \Contracts \DockerAwareInterface ;
9
13
use Somnambulist \ProjectManager \Contracts \ProjectConfigAwareInterface ;
10
14
use Somnambulist \ProjectManager \Models \Project ;
15
+ use Somnambulist \ProjectManager \Models \Service ;
11
16
use Symfony \Component \Console \Input \InputArgument ;
12
17
use Symfony \Component \Console \Input \InputInterface ;
13
18
use Symfony \Component \Console \Output \OutputInterface ;
14
19
use function array_filter ;
15
20
use function file_put_contents ;
16
21
use function implode ;
22
+ use function strtolower ;
17
23
use const DIRECTORY_SEPARATOR ;
18
24
19
25
/**
22
28
* @package Somnambulist\ProjectManager\Commands\Projects
23
29
* @subpackage Somnambulist\ProjectManager\Commands\Projects\SwitchProjectCommand
24
30
*/
25
- class SwitchProjectCommand extends AbstractCommand implements ProjectConfigAwareInterface
31
+ class SwitchProjectCommand extends AbstractCommand implements DockerAwareInterface, ProjectConfigAwareInterface
26
32
{
27
33
28
34
use UseEnvironmentTemplate;
35
+ use DockerAwareCommand;
36
+ use GetCurrentActiveProject;
29
37
use GetProjectFromInput;
30
38
use ProjectConfigAwareCommand;
31
39
@@ -43,6 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
43
51
{
44
52
$ this ->setupConsoleHelper ($ input , $ output );
45
53
54
+ $ this ->checkCurrentProjectIsRunning ();
55
+
46
56
$ project = $ this ->getProjectFrom ($ input );
47
57
48
58
$ this ->tools ()->warning ('switching active project to <info>%s</info> ' , $ project ->name ());
@@ -73,4 +83,29 @@ private function makePath(?string ...$args): string
73
83
{
74
84
return implode (DIRECTORY_SEPARATOR , array_filter ($ args ));
75
85
}
86
+
87
+ private function checkCurrentProjectIsRunning (): void
88
+ {
89
+ try {
90
+ $ project = $ this ->getActiveProject ();
91
+ $ cnt = $ project ->services ()->list ()
92
+ ->each (function (Service $ s ) {
93
+ $ this ->docker ->resolve ($ s );
94
+ })
95
+ ->filter (function (Service $ s ) {
96
+ return $ s ->isRunning ();
97
+ })
98
+ ->count ()
99
+ ;
100
+
101
+ if ($ cnt > 0 ) {
102
+ if ('y ' == strtolower ($ this ->tools ()->ask ('Project <info>%s</info> has running services. Should these be stopped? [y/n] ' , false , $ project ->name ()))) {
103
+ $ this ->tools ()->info ('stopping running services in <info>%s</info> ' , $ project ->name ());
104
+ $ this ->tools ()->run ('spm stop all ' );
105
+ }
106
+ }
107
+ } catch (Exception $ e ) {
108
+
109
+ }
110
+ }
76
111
}
0 commit comments