1
+ <?php
2
+
3
+ namespace Apiato \Installer \Console ;
4
+
5
+ use RuntimeException ;
6
+ use Symfony \Component \Console \Command \Command ;
7
+ use Symfony \Component \Console \Input \InputArgument ;
8
+ use Symfony \Component \Console \Input \InputInterface ;
9
+ use Symfony \Component \Console \Input \InputOption ;
10
+ use Symfony \Component \Console \Output \OutputInterface ;
11
+ use Symfony \Component \Process \Process ;
12
+
13
+ class NewCommand extends Command
14
+ {
15
+
16
+ protected static $ defaultName = 'new ' ;
17
+
18
+ protected function configure ()
19
+ {
20
+ $ this
21
+ ->setDescription ('Create a new Apiato application ' )
22
+ ->addArgument ('name ' , InputArgument::REQUIRED )
23
+ ->addOption ('dev ' , null , InputOption::VALUE_NONE , 'Installs the latest "development" release ' )
24
+ ->addOption ('force ' , 'f ' , InputOption::VALUE_NONE , 'Forces install even if the directory already exists ' )
25
+ ->setHelp ('This command only shows a message to welcome user and nothing more. ' );
26
+ }
27
+
28
+ protected function execute (InputInterface $ input , OutputInterface $ output )
29
+ {
30
+
31
+ $ output ->writeln ([
32
+ '<fg=red> ' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL ,
33
+ " ___ .______ __ ___ .___________. ______ " ,
34
+ " / \ | _ \ | | / \ | | / __ \ " ,
35
+ " / ^ \ | |_) | | | / ^ \ `---| |----`| | | | " ,
36
+ " / /_\ \ | ___/ | | / /_\ \ | | | | | | " ,
37
+ " / _____ \ | | | | / _____ \ | | | `--' | " ,
38
+ "/__/ \__\ | _| |__| /__/ \__\ |__| \______/ " ,
39
+ '</> ' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL ,
40
+ ]);
41
+
42
+ sleep (1 );
43
+
44
+ $ name = $ input ->getArgument ('name ' );
45
+
46
+ $ directory = $ name !== '. ' ? getcwd () . '/ ' . $ name : '. ' ;
47
+
48
+ $ version = $ this ->getVersion ($ input );
49
+
50
+ if (!$ input ->getOption ('force ' )) {
51
+ $ this ->verifyApplicationDoesntExist ($ directory );
52
+ }
53
+
54
+ if ($ input ->getOption ('force ' ) && $ directory === '. ' ) {
55
+ throw new RuntimeException ('Cannot use --force option when using current directory for installation! ' );
56
+ }
57
+
58
+ $ composer = $ this ->findComposer ();
59
+
60
+ $ commands = [
61
+ // TODO: remove specific version for stable version release
62
+ $ composer . " create-project apiato/apiato:10.0.0-rc.11 \"$ directory \" $ version --remove-vcs --prefer-dist " ,
63
+ ];
64
+
65
+ if ($ directory != '. ' && $ input ->getOption ('force ' )) {
66
+ if (PHP_OS_FAMILY == 'Windows ' ) {
67
+ array_unshift ($ commands , "rd /s /q \"$ directory \"" );
68
+ } else {
69
+ array_unshift ($ commands , "rm -rf \"$ directory \"" );
70
+ }
71
+ }
72
+
73
+ if (PHP_OS_FAMILY != 'Windows ' ) {
74
+ $ commands [] = "chmod 755 \"$ directory/artisan \"" ;
75
+ }
76
+
77
+ if (($ process = $ this ->runCommands ($ commands , $ input , $ output ))->isSuccessful ()) {
78
+ if ($ name !== '. ' ) {
79
+ $ this ->replaceInFile (
80
+ 'APP_URL=http://localhost ' ,
81
+ 'APP_URL=http:// ' . $ name . '.test ' ,
82
+ $ directory . '/.env '
83
+ );
84
+
85
+ $ this ->replaceInFile (
86
+ 'DB_DATABASE=laravel ' ,
87
+ 'DB_DATABASE= ' . str_replace ('- ' , '_ ' , strtolower ($ name )),
88
+ $ directory . '/.env '
89
+ );
90
+
91
+ $ this ->replaceInFile (
92
+ 'DB_DATABASE=laravel ' ,
93
+ 'DB_DATABASE= ' . str_replace ('- ' , '_ ' , strtolower ($ name )),
94
+ $ directory . '/.env.example '
95
+ );
96
+ }
97
+
98
+ $ output ->writeln (PHP_EOL . '<comment>Apiato ready! Build something amazing.</comment> ' );
99
+ }
100
+
101
+ return $ process ->getExitCode ();
102
+ }
103
+
104
+ /**
105
+ * Get the version that should be downloaded.
106
+ *
107
+ * @param \Symfony\Component\Console\Input\InputInterface $input
108
+ * @return string
109
+ */
110
+ protected function getVersion (InputInterface $ input )
111
+ {
112
+ if ($ input ->getOption ('dev ' )) {
113
+ return 'dev-master ' ;
114
+ }
115
+
116
+ return '' ;
117
+ }
118
+
119
+ /**
120
+ * Verify that the application does not already exist.
121
+ *
122
+ * @param string $directory
123
+ * @return void
124
+ */
125
+ protected function verifyApplicationDoesntExist ($ directory )
126
+ {
127
+ if ((is_dir ($ directory ) || is_file ($ directory )) && $ directory != getcwd ()) {
128
+ throw new RuntimeException ('Application already exists! ' );
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Get the composer command for the environment.
134
+ *
135
+ * @return string
136
+ */
137
+ protected function findComposer ()
138
+ {
139
+ $ composerPath = getcwd () . '/composer.phar ' ;
140
+
141
+ if (file_exists ($ composerPath )) {
142
+ return '" ' . PHP_BINARY . '" ' . $ composerPath ;
143
+ }
144
+
145
+ return 'composer ' ;
146
+ }
147
+
148
+ /**
149
+ * Run the given commands.
150
+ *
151
+ * @param array $commands
152
+ * @param \Symfony\Component\Console\Input\InputInterface $input
153
+ * @param \Symfony\Component\Console\Output\OutputInterface $output
154
+ * @param array $env
155
+ * @return \Symfony\Component\Process\Process
156
+ */
157
+ protected function runCommands ($ commands , InputInterface $ input , OutputInterface $ output , array $ env = [])
158
+ {
159
+ if ($ input ->getOption ('no-ansi ' )) {
160
+ $ commands = array_map (function ($ value ) {
161
+ if (substr ($ value , 0 , 5 ) === 'chmod ' ) {
162
+ return $ value ;
163
+ }
164
+
165
+ return $ value . ' --no-ansi ' ;
166
+ }, $ commands );
167
+ }
168
+
169
+ if ($ input ->getOption ('quiet ' )) {
170
+ $ commands = array_map (function ($ value ) {
171
+ if (substr ($ value , 0 , 5 ) === 'chmod ' ) {
172
+ return $ value ;
173
+ }
174
+
175
+ return $ value . ' --quiet ' ;
176
+ }, $ commands );
177
+ }
178
+
179
+ $ process = Process::fromShellCommandline (implode (' && ' , $ commands ), null , $ env , null , null );
180
+
181
+ if ('\\' !== DIRECTORY_SEPARATOR && file_exists ('/dev/tty ' ) && is_readable ('/dev/tty ' )) {
182
+ try {
183
+ $ process ->setTty (true );
184
+ } catch (RuntimeException $ e ) {
185
+ $ output ->writeln ('Warning: ' . $ e ->getMessage ());
186
+ }
187
+ }
188
+
189
+ $ process ->run (function ($ type , $ line ) use ($ output ) {
190
+ $ output ->write (' ' . $ line );
191
+ });
192
+
193
+ return $ process ;
194
+ }
195
+
196
+ /**
197
+ * Replace the given string in the given file.
198
+ *
199
+ * @param string $search
200
+ * @param string $replace
201
+ * @param string $file
202
+ * @return void
203
+ */
204
+ protected function replaceInFile (string $ search , string $ replace , string $ file )
205
+ {
206
+ file_put_contents (
207
+ $ file ,
208
+ str_replace ($ search , $ replace , file_get_contents ($ file ))
209
+ );
210
+ }
211
+ }
0 commit comments