Skip to content

Commit 8ad5578

Browse files
feature #448 Allow configuring the "extra.symfony.root-dir" of the Symfony app (nicolas-grekas)
This PR was merged into the 1.1-dev branch. Discussion ---------- Allow configuring the "extra.symfony.root-dir" of the Symfony app Fixes #243 and makes path management a bit more flexible. Commits ------- 186bee1 Allow configuring the "extra.symfony.root-dir" of the Symfony app
2 parents ef8b60e + 186bee1 commit 8ad5578

19 files changed

+69
-77
lines changed

src/Configurator/AbstractConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
3232
$this->composer = $composer;
3333
$this->io = $io;
3434
$this->options = $options;
35-
$this->path = new Path(getcwd());
35+
$this->path = new Path($options->get('root-dir'));
3636
}
3737

3838
abstract public function configure(Recipe $recipe, $config, array $options = []);

src/Configurator/BundlesConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@ private function dump(string $file, array $bundles)
100100

101101
private function getConfFile(): string
102102
{
103-
return $this->options->expandTargetDir('%CONFIG_DIR%/bundles.php');
103+
return $this->options->get('root-dir').'/'.$this->options->expandTargetDir('%CONFIG_DIR%/bundles.php');
104104
}
105105
}

src/Configurator/ContainerConfigurator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function configure(Recipe $recipe, $parameters, array $options = [])
2727
public function unconfigure(Recipe $recipe, $parameters)
2828
{
2929
$this->write('Unsetting parameters');
30-
$target = $this->options->expandTargetDir('%CONFIG_DIR%/services.yaml');
30+
$target = $this->options->get('root-dir').'/'.$this->options->expandTargetDir('%CONFIG_DIR%/services.yaml');
3131
$lines = [];
3232
foreach (file($target) as $line) {
3333
foreach (array_keys($parameters) as $key) {
@@ -42,7 +42,7 @@ public function unconfigure(Recipe $recipe, $parameters)
4242

4343
private function addParameters(array $parameters)
4444
{
45-
$target = $this->options->expandTargetDir('%CONFIG_DIR%/services.yaml');
45+
$target = $this->options->get('root-dir').'/'.$this->options->expandTargetDir('%CONFIG_DIR%/services.yaml');
4646
$endAt = 0;
4747
$isParameters = false;
4848
$lines = [];

src/Configurator/CopyFromPackageConfigurator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public function configure(Recipe $recipe, $config, array $options = [])
2323
{
2424
$this->write('Setting configuration and copying files');
2525
$packageDir = $this->composer->getInstallationManager()->getInstallPath($recipe->getPackage());
26-
$this->copyFiles($config, $packageDir, getcwd(), $options['force'] ?? false);
26+
$this->copyFiles($config, $packageDir, $this->options->get('root-dir'), $options['force'] ?? false);
2727
}
2828

2929
public function unconfigure(Recipe $recipe, $config)
3030
{
3131
$this->write('Removing configuration and files');
3232
$packageDir = $this->composer->getInstallationManager()->getInstallPath($recipe->getPackage());
33-
$this->removeFiles($config, $packageDir, getcwd());
33+
$this->removeFiles($config, $packageDir, $this->options->get('root-dir'));
3434
}
3535

3636
private function copyFiles(array $manifest, string $from, string $to, bool $overwrite = false)
@@ -97,7 +97,7 @@ public function copyFile(string $source, string $target, bool $overwrite = false
9797
throw new LogicException(sprintf('File "%s" does not exist!', $source));
9898
}
9999

100-
copy($source, $target);
100+
file_put_contents($target, $this->options->expandTargetDir(file_get_contents($source)));
101101
@chmod($target, fileperms($target) | (fileperms($source) & 0111));
102102
$this->write(sprintf('Created <fg=green>"%s"</>', $this->path->relativize($target)));
103103
}

src/Configurator/CopyFromRecipeConfigurator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ class CopyFromRecipeConfigurator extends AbstractConfigurator
2121
public function configure(Recipe $recipe, $config, array $options = [])
2222
{
2323
$this->write('Setting configuration and copying files');
24-
$this->copyFiles($config, $recipe->getFiles(), getcwd(), $options['force'] ?? false);
24+
$this->copyFiles($config, $recipe->getFiles(), $this->options->get('root-dir'), $options['force'] ?? false);
2525
}
2626

2727
public function unconfigure(Recipe $recipe, $config)
2828
{
2929
$this->write('Removing configuration and files');
30-
$this->removeFiles($config, $recipe->getFiles(), getcwd());
30+
$this->removeFiles($config, $recipe->getFiles(), $this->options->get('root-dir'));
3131
}
3232

3333
private function copyFiles(array $manifest, array $files, string $to, bool $overwrite = false)
@@ -62,7 +62,7 @@ private function copyFile(string $to, string $contents, bool $executable, bool $
6262
mkdir(\dirname($to), 0777, true);
6363
}
6464

65-
file_put_contents($to, $contents);
65+
file_put_contents($to, $this->options->expandTargetDir($contents));
6666
if ($executable) {
6767
@chmod($to, fileperms($to) | 0111);
6868
}

src/Configurator/EnvConfigurator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function configure(Recipe $recipe, $vars, array $options = [])
2323
$this->write('Added environment variable defaults');
2424

2525
$this->configureEnvDist($recipe, $vars);
26-
if (!file_exists(getcwd().'/.env.test')) {
26+
if (!file_exists($this->options->get('root-dir').'/.env.test')) {
2727
$this->configurePhpUnit($recipe, $vars);
2828
}
2929
}
@@ -37,7 +37,7 @@ public function unconfigure(Recipe $recipe, $vars)
3737
private function configureEnvDist(Recipe $recipe, $vars)
3838
{
3939
foreach (['.env.dist', '.env'] as $file) {
40-
$env = getcwd().'/'.$file;
40+
$env = $this->options->get('root-dir').'/'.$file;
4141
if (!is_file($env)) {
4242
continue;
4343
}
@@ -69,7 +69,7 @@ private function configureEnvDist(Recipe $recipe, $vars)
6969
private function configurePhpUnit(Recipe $recipe, $vars)
7070
{
7171
foreach (['phpunit.xml.dist', 'phpunit.xml'] as $file) {
72-
$phpunit = getcwd().'/'.$file;
72+
$phpunit = $this->options->get('root-dir').'/'.$file;
7373
if (!is_file($phpunit)) {
7474
continue;
7575
}
@@ -110,7 +110,7 @@ private function configurePhpUnit(Recipe $recipe, $vars)
110110
private function unconfigureEnvFiles(Recipe $recipe, $vars)
111111
{
112112
foreach (['.env', '.env.dist'] as $file) {
113-
$env = getcwd().'/'.$file;
113+
$env = $this->options->get('root-dir').'/'.$file;
114114
if (!file_exists($env)) {
115115
continue;
116116
}
@@ -128,7 +128,7 @@ private function unconfigureEnvFiles(Recipe $recipe, $vars)
128128
private function unconfigurePhpUnit(Recipe $recipe, $vars)
129129
{
130130
foreach (['phpunit.xml.dist', 'phpunit.xml'] as $file) {
131-
$phpunit = getcwd().'/'.$file;
131+
$phpunit = $this->options->get('root-dir').'/'.$file;
132132
if (!is_file($phpunit)) {
133133
continue;
134134
}

src/Configurator/GitignoreConfigurator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function configure(Recipe $recipe, $vars, array $options = [])
2222
{
2323
$this->write('Added entries to .gitignore');
2424

25-
$gitignore = getcwd().'/.gitignore';
25+
$gitignore = $this->options->get('root-dir').'/.gitignore';
2626
if ($this->isFileMarked($recipe, $gitignore)) {
2727
return;
2828
}
@@ -37,7 +37,7 @@ public function configure(Recipe $recipe, $vars, array $options = [])
3737

3838
public function unconfigure(Recipe $recipe, $vars)
3939
{
40-
$file = getcwd().'/.gitignore';
40+
$file = $this->options->get('root-dir').'/.gitignore';
4141
if (!file_exists($file)) {
4242
return;
4343
}

src/Configurator/MakefileConfigurator.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ public function configure(Recipe $recipe, $definitions, array $options = [])
2222
{
2323
$this->write('Added Makefile entries');
2424

25-
$makefile = getcwd().'/Makefile';
25+
$makefile = $this->options->get('root-dir').'/Makefile';
2626
if ($this->isFileMarked($recipe, $makefile)) {
2727
return;
2828
}
2929

30-
$data = $this->markData($recipe, implode("\n", $definitions));
30+
$data = $this->options->expandTargetDir(implode("\n", $definitions));
31+
$data = $this->markData($recipe, $data);
3132

3233
if (!file_exists($makefile)) {
3334
file_put_contents(
34-
getcwd().'/Makefile',
35+
$this->options->get('root-dir').'/Makefile',
3536
<<<EOF
3637
ifndef APP_ENV
3738
include .env
@@ -45,12 +46,12 @@ public function configure(Recipe $recipe, $definitions, array $options = [])
4546
EOF
4647
);
4748
}
48-
file_put_contents(getcwd().'/Makefile', "\n".ltrim($data, "\r\n"), FILE_APPEND);
49+
file_put_contents($this->options->get('root-dir').'/Makefile', "\n".ltrim($data, "\r\n"), FILE_APPEND);
4950
}
5051

5152
public function unconfigure(Recipe $recipe, $vars)
5253
{
53-
if (!file_exists($makefile = getcwd().'/Makefile')) {
54+
if (!file_exists($makefile = $this->options->get('root-dir').'/Makefile')) {
5455
return;
5556
}
5657

src/Flex.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ public function update(Event $event, $operations = [])
292292
if ($operations) {
293293
$this->operations = $operations;
294294
}
295-
$cwd = getcwd();
295+
$rootDir = $this->options->get('root-dir');
296296

297-
if (!file_exists("$cwd/.env") && !file_exists("$cwd/.env.local") && file_exists("$cwd/.env.dist") && false === strpos(file_get_contents("$cwd/.env.dist"), '.env.local')) {
298-
copy(getcwd().'/.env.dist', getcwd().'/.env');
297+
if (!file_exists("$rootDir/.env") && !file_exists("$rootDir/.env.local") && file_exists("$rootDir/.env.dist") && false === strpos(file_get_contents("$rootDir/.env.dist"), '.env.local')) {
298+
copy($rootDir.'/.env.dist', $rootDir.'/.env');
299299
}
300300

301301
$recipes = $this->fetchRecipes();
@@ -621,14 +621,17 @@ private function fetchRecipes(): array
621621

622622
private function initOptions(): Options
623623
{
624+
$extra = $this->composer->getPackage()->getExtra();
625+
624626
$options = array_merge([
625627
'bin-dir' => 'bin',
626628
'conf-dir' => 'conf',
627629
'config-dir' => 'config',
628630
'src-dir' => 'src',
629631
'var-dir' => 'var',
630632
'public-dir' => 'public',
631-
], $this->composer->getPackage()->getExtra());
633+
'root-dir' => $extra['symfony']['root-dir'] ?? '.',
634+
], $extra);
632635

633636
return new Options($options, $this->io);
634637
}

src/ScriptExecutor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function expandSymfonyCmd(string $cmd)
9999
return null;
100100
}
101101

102-
$console = ProcessExecutor::escape($this->options->get('bin-dir').'/console');
102+
$console = ProcessExecutor::escape($this->options->get('root-dir').'/'.$this->options->get('bin-dir').'/console');
103103
if ($this->io->isDecorated()) {
104104
$console .= ' --ansi';
105105
}

0 commit comments

Comments
 (0)