|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +require_once 'vendor/autoload.php'; |
| 6 | + |
| 7 | +const ENVIRONMENT_DEVELOPMENT = 'development'; |
| 8 | +const ENVIRONMENT_PRODUCTION = 'production'; |
| 9 | + |
| 10 | +// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols |
| 11 | + |
| 12 | +function copyFile(array $file): void |
| 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 | + |
| 19 | + if (is_readable($file['destination'])) { |
| 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; |
| 26 | + } else { |
| 27 | + echo "File {$file['source']} copied successfully to {$file['destination']}." . PHP_EOL; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function getEnvironment(): string |
| 32 | +{ |
| 33 | + return getenv('COMPOSER_DEV_MODE') === '1' ? ENVIRONMENT_DEVELOPMENT : ENVIRONMENT_PRODUCTION; |
| 34 | +} |
| 35 | + |
| 36 | +// when adding files to the below array the `source` and `destination` paths must be relative to the project root folder |
| 37 | +// the `environment` key will indicate on what environments the file will be copied, |
| 38 | +$files = [ |
| 39 | + [ |
| 40 | + 'source' => 'config/autoload/local.php.dist', |
| 41 | + 'destination' => 'config/autoload/local.php', |
| 42 | + 'environment' => [ENVIRONMENT_DEVELOPMENT, ENVIRONMENT_PRODUCTION], |
| 43 | + ], |
| 44 | + [ |
| 45 | + 'source' => 'config/autoload/local.test.php.dist', |
| 46 | + 'destination' => 'config/autoload/local.test.php', |
| 47 | + 'environment' => [ENVIRONMENT_DEVELOPMENT], |
| 48 | + ], |
| 49 | + [ |
| 50 | + 'source' => 'vendor/dotkernel/dot-mail/config/mail.global.php.dist', |
| 51 | + 'destination' => 'config/autoload/mail.global.php', |
| 52 | + 'environment' => [ENVIRONMENT_DEVELOPMENT, ENVIRONMENT_PRODUCTION], |
| 53 | + ], |
| 54 | +]; |
| 55 | + |
| 56 | +echo "Using environment setting: " . getEnvironment() . PHP_EOL; |
| 57 | + |
| 58 | +array_walk($files, 'copyFile'); |
0 commit comments