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