|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* (c) Anton Medvedev <anton@medv.io> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Deployer\Import; |
| 12 | + |
| 13 | +use Deployer\Exception\Exception; |
| 14 | + |
| 15 | +class Import |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @param string|string[] $paths |
| 19 | + * @throws Exception |
| 20 | + */ |
| 21 | + public static function import(mixed $paths): void |
| 22 | + { |
| 23 | + if (!is_array($paths)) { |
| 24 | + $paths = [$paths]; |
| 25 | + } |
| 26 | + foreach ($paths as $path) { |
| 27 | + if (preg_match('/\.php$/i', $path)) { |
| 28 | + // Prevent variable leak into deploy.php file |
| 29 | + call_user_func(function () use ($path) { |
| 30 | + // Reorder autoload stack |
| 31 | + $originStack = spl_autoload_functions(); |
| 32 | + |
| 33 | + require $path; |
| 34 | + |
| 35 | + $newStack = spl_autoload_functions(); |
| 36 | + if ($originStack[0] !== $newStack[0]) { |
| 37 | + foreach (array_reverse($originStack) as $loader) { |
| 38 | + spl_autoload_unregister($loader); |
| 39 | + spl_autoload_register($loader, true, true); |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + } elseif (preg_match('/\.maml$/i', $path)) { |
| 44 | + $recipe = new MamlRecipe($path); |
| 45 | + $recipe->run(); |
| 46 | + } elseif (preg_match('/\.ya?ml$/i', $path)) { |
| 47 | + YamlRecipe::exec($path); |
| 48 | + } else { |
| 49 | + throw new Exception("Unknown file format: $path"); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments