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
}

tests/Configurator/BundlesConfiguratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ class BundlesConfiguratorTest extends TestCase
2020
{
2121
public function testConfigure()
2222
{
23-
$config = getcwd().'/config/bundles.php';
23+
$config = FLEX_TEST_DIR.'/config/bundles.php';
2424

2525
$configurator = new BundlesConfigurator(
2626
$this->getMockBuilder('Composer\Composer')->getMock(),
2727
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
28-
new Options(['config-dir' => \dirname($config)])
28+
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
2929
);
3030

3131
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();

tests/Configurator/ContainerConfiguratorTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ContainerConfiguratorTest extends TestCase
2323
public function testConfigure()
2424
{
2525
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
26-
$config = getcwd().'/config/services.yaml';
26+
$config = FLEX_TEST_DIR.'/config/services.yaml';
2727
file_put_contents(
2828
$config,
2929
<<<EOF
@@ -37,7 +37,7 @@ public function testConfigure()
3737
$configurator = new ContainerConfigurator(
3838
$this->getMockBuilder(Composer::class)->getMock(),
3939
$this->getMockBuilder(IOInterface::class)->getMock(),
40-
new Options(['config-dir' => \dirname($config)])
40+
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
4141
);
4242
$configurator->configure($recipe, ['locale' => 'en']);
4343
$this->assertEquals(<<<EOF
@@ -64,7 +64,7 @@ public function testConfigure()
6464
public function testConfigureWithoutParametersKey()
6565
{
6666
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
67-
$config = getcwd().'/config/services.yaml';
67+
$config = FLEX_TEST_DIR.'/config/services.yaml';
6868
file_put_contents(
6969
$config,
7070
<<<EOF
@@ -75,7 +75,7 @@ public function testConfigureWithoutParametersKey()
7575
$configurator = new ContainerConfigurator(
7676
$this->getMockBuilder(Composer::class)->getMock(),
7777
$this->getMockBuilder(IOInterface::class)->getMock(),
78-
new Options(['config-dir' => \dirname($config)])
78+
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
7979
);
8080
$configurator->configure($recipe, ['locale' => 'en']);
8181
$this->assertEquals(<<<EOF
@@ -100,7 +100,7 @@ public function testConfigureWithoutParametersKey()
100100
public function testConfigureWithoutDuplicated()
101101
{
102102
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
103-
$config = getcwd().'/config/services.yaml';
103+
$config = FLEX_TEST_DIR.'/config/services.yaml';
104104
file_put_contents(
105105
$config,
106106
<<<EOF
@@ -114,7 +114,7 @@ public function testConfigureWithoutDuplicated()
114114
$configurator = new ContainerConfigurator(
115115
$this->getMockBuilder(Composer::class)->getMock(),
116116
$this->getMockBuilder(IOInterface::class)->getMock(),
117-
new Options(['config-dir' => \dirname($config)])
117+
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
118118
);
119119
$configurator->configure($recipe, ['locale' => 'en']);
120120
$this->assertEquals(<<<EOF
@@ -139,7 +139,7 @@ public function testConfigureWithoutDuplicated()
139139
public function testConfigureWithComplexContent()
140140
{
141141
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
142-
$config = getcwd().'/config/services.yaml';
142+
$config = FLEX_TEST_DIR.'/config/services.yaml';
143143
file_put_contents(
144144
$config,
145145
<<<EOF
@@ -157,7 +157,7 @@ public function testConfigureWithComplexContent()
157157
$configurator = new ContainerConfigurator(
158158
$this->getMockBuilder(Composer::class)->getMock(),
159159
$this->getMockBuilder(IOInterface::class)->getMock(),
160-
new Options(['config-dir' => \dirname($config)])
160+
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
161161
);
162162
$configurator->configure($recipe, ['locale' => 'en', 'foobar' => 'baz']);
163163
$this->assertEquals(<<<EOF

tests/Configurator/CopyDirectoryFromPackageConfiguratorTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ protected function setUp()
5555
{
5656
parent::setUp();
5757

58-
$this->sourceDirectory = getcwd().'/package/files';
58+
$this->sourceDirectory = FLEX_TEST_DIR.'/package/files';
5959
$this->sourceFileRelativePath = 'package/files/';
6060
$this->sourceFiles = [
6161
$this->sourceDirectory.'/file1',
6262
$this->sourceDirectory.'/file2',
6363
];
6464

65-
$this->targetDirectory = getcwd().'/public/files';
65+
$this->targetDirectory = FLEX_TEST_DIR.'/public/files';
6666
$this->targetFileRelativePath = 'public/files/';
6767
$this->targetFiles = [
6868
$this->targetDirectory.'/file1',
@@ -79,7 +79,7 @@ protected function setUp()
7979
$installationManager->expects($this->exactly(1))
8080
->method('getInstallPath')
8181
->with($package)
82-
->willReturn(getcwd())
82+
->willReturn(FLEX_TEST_DIR)
8383
;
8484
$this->composer = $this->getMockBuilder(Composer::class)->getMock();
8585
$this->composer->expects($this->exactly(1))
@@ -102,13 +102,13 @@ protected function tearDown()
102102

103103
private function createConfigurator(): CopyFromPackageConfigurator
104104
{
105-
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options());
105+
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR]));
106106
}
107107

108108
private function cleanUpTargetFiles()
109109
{
110-
$this->rrmdir(getcwd().'/package');
111-
$this->rrmdir(getcwd().'/public');
110+
$this->rrmdir(FLEX_TEST_DIR.'/package');
111+
$this->rrmdir(FLEX_TEST_DIR.'/public');
112112
}
113113

114114
/**

tests/Configurator/CopyFromPackageConfiguratorTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ protected function setUp()
119119
{
120120
parent::setUp();
121121

122-
$this->sourceDirectory = getcwd().'/package';
122+
$this->sourceDirectory = FLEX_TEST_DIR.'/package';
123123
$this->sourceFileRelativePath = 'package/file';
124124
$this->sourceFile = $this->sourceDirectory.'/file';
125125

126-
$this->targetDirectory = getcwd().'/public';
126+
$this->targetDirectory = FLEX_TEST_DIR.'/public';
127127
$this->targetFileRelativePath = 'public/file';
128128
$this->targetFile = $this->targetDirectory.'/file';
129129

@@ -137,7 +137,7 @@ protected function setUp()
137137
$installationManager->expects($this->exactly(1))
138138
->method('getInstallPath')
139139
->with($package)
140-
->willReturn(getcwd())
140+
->willReturn(FLEX_TEST_DIR)
141141
;
142142
$this->composer = $this->getMockBuilder(Composer::class)->getMock();
143143
$this->composer->expects($this->exactly(1))
@@ -158,13 +158,13 @@ protected function tearDown()
158158

159159
private function createConfigurator(): CopyFromPackageConfigurator
160160
{
161-
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options([], $this->io));
161+
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR], $this->io));
162162
}
163163

164164
private function cleanUpTargetFiles()
165165
{
166166
@unlink($this->targetFile);
167-
@rmdir(getcwd().'/package');
168-
@rmdir(getcwd().'/public');
167+
@rmdir(FLEX_TEST_DIR.'/package');
168+
@rmdir(FLEX_TEST_DIR.'/public');
169169
}
170170
}

0 commit comments

Comments
 (0)