nystudio107 /
craft-vite
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Vite plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * Allows the use of the Vite.js next generation frontend tooling with Craft CMS |
||
| 6 | * |
||
| 7 | * @link https://nystudio107.com |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2022 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\vite\helpers; |
||
| 12 | |||
| 13 | use Craft; |
||
| 14 | use ReflectionClass; |
||
| 15 | use ReflectionException; |
||
| 16 | use ReflectionProperty; |
||
| 17 | |||
| 18 | /** |
||
|
0 ignored issues
–
show
|
|||
| 19 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 20 | * @package Vite |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @since 4.0.3 |
||
|
0 ignored issues
–
show
|
|||
| 22 | */ |
||
|
0 ignored issues
–
show
|
|||
| 23 | class PluginConfig |
||
| 24 | { |
||
| 25 | // Public static methods |
||
| 26 | // ========================================================================= |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Return a service config definition pre-populated with settings from the |
||
| 30 | * $configHandle config/ file |
||
| 31 | * |
||
| 32 | * @param string $configHandle |
||
|
0 ignored issues
–
show
|
|||
| 33 | * @param string $serviceClass |
||
|
0 ignored issues
–
show
|
|||
| 34 | * @return array |
||
|
0 ignored issues
–
show
|
|||
| 35 | */ |
||
| 36 | public static function serviceDefinitionFromConfig(string $configHandle, string $serviceClass): array |
||
| 37 | { |
||
| 38 | $serviceAttrs = []; |
||
|
0 ignored issues
–
show
|
|||
| 39 | // Get the available attributes from the $serviceClass |
||
| 40 | try { |
||
| 41 | $serviceAttrs = self::getClassAttributes($serviceClass); |
||
| 42 | } catch (ReflectionException $e) { |
||
| 43 | // That's fine |
||
| 44 | } |
||
| 45 | // Intersect the settings from the config file with the available service attributes |
||
| 46 | $serviceConfig = array_intersect_key( |
||
| 47 | Craft::$app->getConfig()->getConfigFromFile($configHandle), |
||
| 48 | $serviceAttrs |
||
| 49 | ); |
||
| 50 | |||
| 51 | return array_merge( |
||
| 52 | $serviceConfig, |
||
| 53 | ['class' => $serviceClass] |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
|
0 ignored issues
–
show
|
|||
| 58 | * Return the list of attribute names from a class name |
||
| 59 | * |
||
| 60 | * @return array list of attribute names. |
||
| 61 | * @throws ReflectionException |
||
| 62 | */ |
||
| 63 | public static function getClassAttributes(string $className): array |
||
| 64 | { |
||
| 65 | $class = new ReflectionClass($className); |
||
| 66 | $names = []; |
||
| 67 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
||
| 68 | if (!$property->isStatic()) { |
||
| 69 | $name = $property->getName(); |
||
| 70 | $names[$name] = $name; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | return $names; |
||
| 75 | } |
||
| 76 | } |
||
| 77 |