Skip to content

Commit 44e8fe0

Browse files
committed
Move helper functions to Traits
1 parent 0e57cf8 commit 44e8fe0

File tree

5 files changed

+175
-133
lines changed

5 files changed

+175
-133
lines changed

commands/NewCommand.php

+10-132
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22

33
namespace Apiato\Installer\Commands;
44

5+
use Apiato\Installer\Traits\CommandTrait;
6+
use Apiato\Installer\Traits\FileTrait;
7+
use Apiato\Installer\Traits\GitTrait;
58
use RuntimeException;
69
use Symfony\Component\Console\Command\Command;
710
use Symfony\Component\Console\Input\InputArgument;
811
use Symfony\Component\Console\Input\InputInterface;
912
use Symfony\Component\Console\Input\InputOption;
1013
use Symfony\Component\Console\Output\OutputInterface;
11-
use Symfony\Component\Process\Process;
14+
use Symfony\Component\Console\Question\ChoiceQuestion;
15+
use Symfony\Component\Console\Question\ConfirmationQuestion;
1216

1317
class NewCommand extends Command
1418
{
1519

20+
use GitTrait;
21+
use CommandTrait;
22+
use FileTrait;
23+
1624
protected static $defaultName = 'new';
1725

1826
protected function configure()
@@ -88,134 +96,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
8896
return $process->getExitCode();
8997
}
9098

91-
/**
92-
* Get the version that should be downloaded.
93-
*
94-
* @param \Symfony\Component\Console\Input\InputInterface $input
95-
* @return string
96-
*/
97-
protected function getVersion(InputInterface $input)
98-
{
99-
if ($input->getOption('dev')) {
100-
return 'dev-master';
101-
}
102-
103-
return '';
104-
}
105-
106-
/**
107-
* Create a Git repository and commit the base Apiato skeleton.
108-
*
109-
* @param string $directory
110-
* @param \Symfony\Component\Console\Input\InputInterface $input
111-
* @param \Symfony\Component\Console\Output\OutputInterface $output
112-
* @return void
113-
*/
114-
protected function createRepository(string $directory, InputInterface $input, OutputInterface $output)
115-
{
116-
chdir($directory);
117-
118-
$branch = $input->getOption('branch') ?: 'main';
119-
120-
$commands = [
121-
"git init -q -b {$branch} .",
122-
'git add .',
123-
'git commit -q -m "Set up a fresh Apiato app"',
124-
];
125-
126-
$this->runCommands($commands, $input, $output);
127-
}
128-
129-
/**
130-
* Verify that the application does not already exist.
131-
*
132-
* @param string $directory
133-
* @return void
134-
*/
135-
protected function verifyApplicationDoesntExist($directory)
136-
{
137-
if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) {
138-
throw new RuntimeException('Application already exists!');
139-
}
140-
}
141-
142-
/**
143-
* Get the composer command for the environment.
144-
*
145-
* @return string
146-
*/
147-
protected function findComposer()
148-
{
149-
$composerPath = getcwd() . '/composer.phar';
150-
151-
if (file_exists($composerPath)) {
152-
return '"' . PHP_BINARY . '" ' . $composerPath;
153-
}
154-
155-
return 'composer';
156-
}
157-
158-
/**
159-
* Run the given commands.
160-
*
161-
* @param array $commands
162-
* @param \Symfony\Component\Console\Input\InputInterface $input
163-
* @param \Symfony\Component\Console\Output\OutputInterface $output
164-
* @param array $env
165-
* @return \Symfony\Component\Process\Process
166-
*/
167-
protected function runCommands($commands, InputInterface $input, OutputInterface $output, array $env = [])
168-
{
169-
if ($input->getOption('no-ansi')) {
170-
$commands = array_map(function ($value) {
171-
if (substr($value, 0, 5) === 'chmod') {
172-
return $value;
173-
}
174-
175-
return $value . ' --no-ansi';
176-
}, $commands);
177-
}
178-
179-
if ($input->getOption('quiet')) {
180-
$commands = array_map(function ($value) {
181-
if (substr($value, 0, 5) === 'chmod') {
182-
return $value;
183-
}
184-
185-
return $value . ' --quiet';
186-
}, $commands);
187-
}
188-
189-
$process = Process::fromShellCommandline(implode(' && ', $commands), null, $env, null, null);
190-
191-
if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
192-
try {
193-
$process->setTty(true);
194-
} catch (RuntimeException $e) {
195-
$output->writeln('Warning: ' . $e->getMessage());
196-
}
197-
}
198-
199-
$process->run(function ($type, $line) use ($output) {
200-
$output->write(' ' . $line);
201-
});
202-
203-
return $process;
204-
}
205-
206-
/**
207-
* Replace the given string in the given file.
208-
*
209-
* @param string $search
210-
* @param string $replace
211-
* @param string $file
212-
* @return void
213-
*/
214-
protected function replaceInFile(string $search, string $replace, string $file)
215-
{
216-
file_put_contents(
217-
$file,
218-
str_replace($search, $replace, file_get_contents($file))
219-
);
220-
}
221-
}
99+
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"autoload": {
1212
"psr-4": {
13-
"Apiato\\Installer\\Commands\\": "commands/"
13+
"Apiato\\Installer\\Commands\\": "commands/",
14+
"Apiato\\Installer\\Traits\\": "traits/"
1415
}
1516
}
1617
}

traits/CommandTrait.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Apiato\Installer\Traits;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use Symfony\Component\Process\Process;
8+
9+
trait CommandTrait
10+
{
11+
12+
/**
13+
* Get the composer command for the environment.
14+
*
15+
* @return string
16+
*/
17+
protected function findComposer()
18+
{
19+
$composerPath = getcwd() . '/composer.phar';
20+
21+
if (file_exists($composerPath)) {
22+
return '"' . PHP_BINARY . '" ' . $composerPath;
23+
}
24+
25+
return 'composer';
26+
}
27+
28+
/**
29+
* Run the given commands.
30+
*
31+
* @param array $commands
32+
* @param \Symfony\Component\Console\Input\InputInterface $input
33+
* @param \Symfony\Component\Console\Output\OutputInterface $output
34+
* @param array $env
35+
* @return \Symfony\Component\Process\Process
36+
*/
37+
protected function runCommands($commands, InputInterface $input, OutputInterface $output, array $env = [])
38+
{
39+
if ($input->getOption('no-ansi')) {
40+
$commands = array_map(function ($value) {
41+
if (substr($value, 0, 5) === 'chmod') {
42+
return $value;
43+
}
44+
45+
return $value . ' --no-ansi';
46+
}, $commands);
47+
}
48+
49+
if ($input->getOption('quiet')) {
50+
$commands = array_map(function ($value) {
51+
if (substr($value, 0, 5) === 'chmod') {
52+
return $value;
53+
}
54+
55+
return $value . ' --quiet';
56+
}, $commands);
57+
}
58+
59+
$process = Process::fromShellCommandline(implode(' && ', $commands), null, $env, null, null);
60+
61+
if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
62+
try {
63+
$process->setTty(true);
64+
} catch (RuntimeException $e) {
65+
$output->writeln('Warning: ' . $e->getMessage());
66+
}
67+
}
68+
69+
$process->run(function ($type, $line) use ($output) {
70+
$output->write(' ' . $line);
71+
});
72+
73+
return $process;
74+
}
75+
76+
/**
77+
* Create a Git repository and commit the base Apiato skeleton.
78+
*
79+
* @param string $directory
80+
* @param \Symfony\Component\Console\Input\InputInterface $input
81+
* @param \Symfony\Component\Console\Output\OutputInterface $output
82+
* @return void
83+
*/
84+
protected function createRepository(string $directory, InputInterface $input, OutputInterface $output)
85+
{
86+
chdir($directory);
87+
88+
$branch = $input->getOption('branch') ?: 'main';
89+
90+
$commands = [
91+
"git init -q -b {$branch} .",
92+
'git add .',
93+
'git commit -q -m "Set up a fresh Apiato app"',
94+
];
95+
96+
$this->runCommands($commands, $input, $output);
97+
}
98+
99+
}

traits/FileTrait.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Apiato\Installer\Traits;
4+
5+
use RuntimeException;
6+
7+
trait FileTrait
8+
{
9+
10+
/**
11+
* Verify that the application does not already exist.
12+
*
13+
* @param string $directory
14+
* @return void
15+
*/
16+
protected function verifyApplicationDoesntExist($directory)
17+
{
18+
if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) {
19+
throw new RuntimeException('Application already exists!');
20+
}
21+
}
22+
23+
/**
24+
* Replace the given string in the given file.
25+
*
26+
* @param string $search
27+
* @param string $replace
28+
* @param string $file
29+
* @return void
30+
*/
31+
protected function replaceInFile(string $search, string $replace, string $file)
32+
{
33+
file_put_contents(
34+
$file,
35+
str_replace($search, $replace, file_get_contents($file))
36+
);
37+
}
38+
39+
}

traits/GitTrait.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Apiato\Installer\Traits;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
7+
trait GitTrait
8+
{
9+
10+
/**
11+
* Get the version that should be downloaded.
12+
*
13+
* @param \Symfony\Component\Console\Input\InputInterface $input
14+
* @return string
15+
*/
16+
protected function getVersion(InputInterface $input)
17+
{
18+
if ($input->getOption('dev')) {
19+
return 'dev-master';
20+
}
21+
22+
return '';
23+
}
24+
25+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy