|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace nystudio107\crafttwigsandbox\helpers; |
| 4 | + |
| 5 | +use Craft; |
| 6 | +use craft\helpers\ArrayHelper; |
| 7 | +use craft\helpers\StringHelper; |
| 8 | +use nystudio107\crafttwigsandbox\twig\BaseSecurityPolicy; |
| 9 | +use nystudio107\seomatic\Seomatic; |
| 10 | +use function is_array; |
| 11 | + |
| 12 | +class SecurityPolicy |
| 13 | +{ |
| 14 | + // Static Methods |
| 15 | + // ========================================================================= |
| 16 | + |
| 17 | + public static function createFromFile(string $filePath, ?string $alias = null): BaseSecurityPolicy |
| 18 | + { |
| 19 | + $config = self::getConfigFromFile($filePath, $alias); |
| 20 | + |
| 21 | + return Craft::createObject($config); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Loads a config file from, trying @craft/config first, then falling back on |
| 26 | + * the provided $alias, if any |
| 27 | + * |
| 28 | + * @param string $filePath |
| 29 | + * @param string|null $alias |
| 30 | + * |
| 31 | + * @return array |
| 32 | + */ |
| 33 | + public static function getConfigFromFile(string $filePath, ?string $alias = null): array |
| 34 | + { |
| 35 | + // Try craft/config first |
| 36 | + $path = self::getConfigFilePath('@config', $filePath); |
| 37 | + if (!file_exists($path)) { |
| 38 | + if (!$alias) { |
| 39 | + return []; |
| 40 | + } |
| 41 | + // Now the additional alias config |
| 42 | + $path = self::getConfigFilePath($alias, $filePath); |
| 43 | + if (!file_exists($path)) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (!is_array($config = @include $path)) { |
| 49 | + return []; |
| 50 | + } |
| 51 | + |
| 52 | + // If it's not a multi-environment config, return the whole thing |
| 53 | + if (!array_key_exists('*', $config)) { |
| 54 | + return $config; |
| 55 | + } |
| 56 | + |
| 57 | + $mergedConfig = []; |
| 58 | + /** @var array $config */ |
| 59 | + foreach ($config as $env => $envConfig) { |
| 60 | + if ($env === '*' || StringHelper::contains(Seomatic::$environment, $env)) { |
| 61 | + $mergedConfig = ArrayHelper::merge($mergedConfig, $envConfig); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return $mergedConfig; |
| 66 | + } |
| 67 | + |
| 68 | + // Private Methods |
| 69 | + // ========================================================================= |
| 70 | + |
| 71 | + /** |
| 72 | + * Return a path from an alias and a partial path |
| 73 | + * |
| 74 | + * @param string $alias |
| 75 | + * @param string $filePath |
| 76 | + * |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + private static function getConfigFilePath(string $alias, string $filePath): string |
| 80 | + { |
| 81 | + $path = DIRECTORY_SEPARATOR . ltrim($filePath, DIRECTORY_SEPARATOR); |
| 82 | + $path = Craft::getAlias($alias) |
| 83 | + . DIRECTORY_SEPARATOR |
| 84 | + . str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path) |
| 85 | + . '.php'; |
| 86 | + |
| 87 | + return $path; |
| 88 | + } |
| 89 | +} |
0 commit comments