create-provider.php
72 lines
| 1 | <?php |
| 2 | /** |
| 3 | * create-provider.php |
| 4 | * |
| 5 | * Script that helps create providers and tests |
| 6 | * |
| 7 | * @package |
| 8 | * @author Michael Pratt <pratt@hablarmierda.net> |
| 9 | * @link http://www.michael-pratt.com/ |
| 10 | * |
| 11 | * For the full copyright and license information, please view the LICENSE |
| 12 | * file that was distributed with this source code. |
| 13 | */ |
| 14 | namespace Embera\bin; |
| 15 | |
| 16 | use RuntimeException; |
| 17 | |
| 18 | if (php_sapi_name() !== 'cli') { |
| 19 | exit; |
| 20 | } |
| 21 | |
| 22 | date_default_timezone_set('UTC'); |
| 23 | require __DIR__.'/../vendor/autoload.php'; |
| 24 | |
| 25 | $usage = 'php create-provider --provider Hostname --host "hostname.com" --endpoint "endpoint.com"'; |
| 26 | $templateDir = __DIR__ . '/templates'; |
| 27 | $providerDir = __DIR__ . '/../src/Embera/Provider/'; |
| 28 | $testProviderDir = __DIR__ . '/../tests/Embera/Provider/'; |
| 29 | |
| 30 | foreach ([$templateDir, $providerDir, $testProviderDir] as $dir) { |
| 31 | if (!is_dir($dir)) { |
| 32 | throw new RuntimeException('ERROR: directory ' . $dir . ' doesnt exists'); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | $options = getopt('', [ 'host:', 'provider:', 'endpoint:']); |
| 37 | $required = [ 'host', 'provider', 'endpoint' ]; |
| 38 | foreach ($required as $r) { |
| 39 | if (!isset($options[$r])) { |
| 40 | throw new RuntimeException('ERROR: Missing Arguments.' . PHP_EOL . 'Usage: ' . $usage); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | $translation = [ |
| 45 | '{host}' => $options['host'], |
| 46 | '{host_escaped}' => preg_quote($options['host'], '~'), |
| 47 | '{provider}' => $options['provider'], |
| 48 | '{endpoint}' => $options['endpoint'], |
| 49 | ]; |
| 50 | |
| 51 | echo sprintf('Creating provider file %s in directory %s', $options['provider'] . '.php', $providerDir) . PHP_EOL; |
| 52 | $data = file_get_contents($templateDir . '/Provider.tpl'); |
| 53 | $data = strtr($data, $translation); |
| 54 | $fileName = $providerDir . '/' . $options['provider'] . '.php'; |
| 55 | if (file_exists($fileName)) { |
| 56 | throw new RuntimeException('ERROR: File ' . $fileName . ' already exists'); |
| 57 | } |
| 58 | |
| 59 | file_put_contents($fileName, $data); |
| 60 | |
| 61 | echo sprintf('Creating provider file %s in directory %s', $options['provider'] . 'Test.php', $testProviderDir) . PHP_EOL; |
| 62 | $data = file_get_contents($templateDir . '/ProviderTest.tpl'); |
| 63 | $data = strtr($data, $translation); |
| 64 | $fileName = $testProviderDir . '/' . $options['provider'] . 'Test.php'; |
| 65 | if (file_exists($fileName)) { |
| 66 | throw new RuntimeException('ERROR: File ' . $fileName . ' already exists'); |
| 67 | } |
| 68 | |
| 69 | file_put_contents($fileName, $data); |
| 70 | |
| 71 | echo 'finished' . PHP_EOL; |
| 72 |