|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
| 5 | +const ENVIRONMENT_DEVELOPMENT = 'development'; |
| 6 | +const ENVIRONMENT_PRODUCTION = 'production'; |
| 7 | + |
5 | 8 | // phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols |
6 | 9 |
|
7 | 10 | /** |
8 | | - * @param array{source: string, destination: string} $file |
| 11 | + * @param array{source: string, destination: string, environment: array<string>} $file |
9 | 12 | */ |
10 | 13 | function copyFile(array $file): void |
11 | 14 | { |
| 15 | + if (! in_array(getEnvironment(), $file['environment'])) { |
| 16 | + echo "Skipping the copy of {$file['source']} due to environment settings." . PHP_EOL; |
| 17 | + return; |
| 18 | + } |
| 19 | + |
12 | 20 | if (is_readable($file['destination'])) { |
13 | | - echo "File {$file['destination']} already exists." . PHP_EOL; |
| 21 | + echo "File {$file['destination']} already exists. Skipping..." . PHP_EOL; |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + if (! copy($file['source'], $file['destination'])) { |
| 26 | + echo "Cannot copy {$file['source']} file to {$file['destination']}" . PHP_EOL; |
14 | 27 | } else { |
15 | | - if (! copy($file['source'], $file['destination'])) { |
16 | | - echo "Cannot copy {$file['source']} file to {$file['destination']}" . PHP_EOL; |
17 | | - } else { |
18 | | - echo "File {$file['source']} copied successfully to {$file['destination']}." . PHP_EOL; |
19 | | - } |
| 28 | + echo "File {$file['source']} copied successfully to {$file['destination']}." . PHP_EOL; |
20 | 29 | } |
21 | 30 | } |
22 | 31 |
|
| 32 | +function getEnvironment(): string |
| 33 | +{ |
| 34 | + return getenv('COMPOSER_DEV_MODE') === '1' ? ENVIRONMENT_DEVELOPMENT : ENVIRONMENT_PRODUCTION; |
| 35 | +} |
| 36 | + |
23 | 37 | $files = [ |
24 | 38 | [ |
25 | 39 | 'source' => 'config/autoload/local.php.dist', |
26 | 40 | 'destination' => 'config/autoload/local.php', |
| 41 | + 'environment' => [ENVIRONMENT_DEVELOPMENT, ENVIRONMENT_PRODUCTION], |
| 42 | + ], |
| 43 | + [ |
| 44 | + 'source' => 'config/autoload/local.test.php.dist', |
| 45 | + 'destination' => 'config/autoload/local.test.php', |
| 46 | + 'environment' => [ENVIRONMENT_DEVELOPMENT], |
27 | 47 | ], |
28 | 48 | ]; |
29 | 49 |
|
| 50 | +echo "Using environment setting: " . getEnvironment() . PHP_EOL; |
| 51 | + |
30 | 52 | array_walk($files, 'copyFile'); |
0 commit comments