|
4 | 4 |
|
5 | 5 | namespace Shopsys\FrameworkBundle\Component\Image\Config; |
6 | 6 |
|
| 7 | +use ReflectionAttribute; |
| 8 | +use ReflectionClass; |
7 | 9 | use Shopsys\FrameworkBundle\Component\EntityExtension\EntityNameResolver; |
| 10 | +use Shopsys\FrameworkBundle\Component\Image\Config\Attributes\EntityImage; |
| 11 | +use Shopsys\FrameworkBundle\Component\Image\Config\Attributes\EntityImageFolder; |
8 | 12 | use Shopsys\FrameworkBundle\Component\Image\Config\Exception\DuplicateEntityNameExceptionInvalid; |
9 | 13 | use Shopsys\FrameworkBundle\Component\Image\Config\Exception\DuplicateTypeNameExceptionInvalid; |
10 | | -use Shopsys\FrameworkBundle\Component\Image\Config\Exception\EntityParseException; |
11 | | -use Shopsys\FrameworkBundle\Component\Image\Config\Exception\InvalidImageConfigException; |
12 | | -use Symfony\Component\Config\Definition\Processor; |
13 | | -use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
14 | | -use Symfony\Component\Filesystem\Filesystem; |
15 | | -use Symfony\Component\Yaml\Parser; |
16 | 14 |
|
17 | 15 | class ImageConfigLoader |
18 | 16 | { |
19 | 17 | /** |
20 | | - * @var array<class-string, \Shopsys\FrameworkBundle\Component\Image\Config\ImageEntityConfig> |
| 18 | + * @var array<class-string,\Shopsys\FrameworkBundle\Component\Image\Config\ImageEntityConfig> |
21 | 19 | */ |
22 | 20 | protected array $foundEntityConfigs; |
23 | 21 |
|
24 | 22 | /** |
25 | 23 | * @var string[] |
26 | 24 | */ |
27 | | - protected array $foundEntityNames; |
| 25 | + protected array $foundFolderNames; |
28 | 26 |
|
29 | 27 | public function __construct( |
30 | | - protected readonly Filesystem $filesystem, |
31 | 28 | protected readonly EntityNameResolver $entityNameResolver, |
32 | 29 | ) { |
33 | 30 | } |
34 | 31 |
|
35 | | - public function loadFromYaml(string $filename): ImageConfig |
| 32 | + /** |
| 33 | + * @param class-string[] $entityClasses |
| 34 | + */ |
| 35 | + public function loadFromEntityClasses(array $entityClasses): ImageConfig |
36 | 36 | { |
37 | | - $yamlParser = new Parser(); |
| 37 | + $this->foundEntityConfigs = []; |
| 38 | + $this->foundFolderNames = []; |
38 | 39 |
|
39 | | - if (!$this->filesystem->exists($filename)) { |
40 | | - throw new FileNotFoundException( |
41 | | - 'File ' . $filename . ' does not exist', |
42 | | - ); |
43 | | - } |
| 40 | + foreach ($entityClasses as $entityClass) { |
| 41 | + $imageAttributes = $this->getImageAttributesFromClass($entityClass); |
44 | 42 |
|
45 | | - $imageConfigDefinition = new ImageConfigDefinition(); |
46 | | - $processor = new Processor(); |
| 43 | + if ($imageAttributes === []) { |
| 44 | + continue; |
| 45 | + } |
47 | 46 |
|
48 | | - $inputConfig = $yamlParser->parse(file_get_contents($filename)); |
49 | | - $outputConfig = $processor->processConfiguration($imageConfigDefinition, [$inputConfig]); |
| 47 | + $folderName = $this->getFolderName($entityClass); |
50 | 48 |
|
51 | | - $preparedConfig = $this->loadFromArray($outputConfig); |
| 49 | + $this->processEntity($entityClass, $folderName, $imageAttributes); |
| 50 | + } |
52 | 51 |
|
53 | | - return new ImageConfig($preparedConfig, $this->entityNameResolver); |
| 52 | + return new ImageConfig($this->foundEntityConfigs, $this->entityNameResolver); |
54 | 53 | } |
55 | 54 |
|
56 | 55 | /** |
57 | | - * @return \Shopsys\FrameworkBundle\Component\Image\Config\ImageEntityConfig[] |
| 56 | + * @param class-string $entityClass |
| 57 | + * @return array<\Shopsys\FrameworkBundle\Component\Image\Config\Attributes\EntityImage> |
58 | 58 | */ |
59 | | - public function loadFromArray(array $outputConfig): array |
| 59 | + protected function getImageAttributesFromClass(string $entityClass): array |
60 | 60 | { |
61 | | - $this->foundEntityConfigs = []; |
62 | | - $this->foundEntityNames = []; |
63 | | - |
64 | | - foreach ($outputConfig as $entityConfig) { |
65 | | - try { |
66 | | - $this->processEntityConfig($entityConfig); |
67 | | - } catch (InvalidImageConfigException $e) { |
68 | | - throw new EntityParseException( |
69 | | - $entityConfig[ImageConfigDefinition::CONFIG_CLASS], |
70 | | - $e, |
71 | | - ); |
| 61 | + $reflectionClass = new ReflectionClass($entityClass); |
| 62 | + |
| 63 | + do { |
| 64 | + $attributes = $reflectionClass->getAttributes(EntityImage::class, ReflectionAttribute::IS_INSTANCEOF); |
| 65 | + |
| 66 | + if ($attributes !== []) { |
| 67 | + return array_map(static fn (ReflectionAttribute $attribute) => $attribute->newInstance(), $attributes); |
72 | 68 | } |
73 | | - } |
| 69 | + } while ($reflectionClass = $reflectionClass->getParentClass()); |
74 | 70 |
|
75 | | - return $this->foundEntityConfigs; |
| 71 | + return []; |
76 | 72 | } |
77 | 73 |
|
78 | | - protected function processEntityConfig(array $entityConfig): void |
| 74 | + protected function getFolderName(string $entityClass): string |
79 | 75 | { |
80 | | - $entityClass = $entityConfig[ImageConfigDefinition::CONFIG_CLASS]; |
81 | | - $entityName = $entityConfig[ImageConfigDefinition::CONFIG_ENTITY_NAME]; |
| 76 | + $originalReflectionClass = new ReflectionClass($entityClass); |
| 77 | + $reflectionClass = $originalReflectionClass; |
| 78 | + |
| 79 | + do { |
| 80 | + $attributes = $reflectionClass->getAttributes(EntityImageFolder::class, ReflectionAttribute::IS_INSTANCEOF); |
| 81 | + |
| 82 | + if ($attributes !== []) { |
| 83 | + return $attributes[0]->newInstance()->name; |
| 84 | + } |
| 85 | + } while ($reflectionClass = $reflectionClass->getParentClass()); |
82 | 86 |
|
83 | | - if (array_key_exists($entityClass, $this->foundEntityConfigs) |
84 | | - || array_key_exists($entityName, $this->foundEntityNames) |
85 | | - ) { |
86 | | - throw new DuplicateEntityNameExceptionInvalid($entityName); |
| 87 | + return lcfirst($originalReflectionClass->getShortName()); |
| 88 | + } |
| 89 | + |
| 90 | + protected function processEntity(string $entityClass, string $folderName, array $imageAttributes): void |
| 91 | + { |
| 92 | + if (array_key_exists($entityClass, $this->foundEntityConfigs)) { |
| 93 | + throw new DuplicateEntityNameExceptionInvalid($folderName); |
87 | 94 | } |
88 | 95 |
|
89 | | - $types = $this->prepareTypes($entityConfig[ImageConfigDefinition::CONFIG_TYPES]); |
90 | | - $multipleByType = $this->getMultipleByType($entityConfig); |
| 96 | + $types = $this->prepareTypes($imageAttributes); |
| 97 | + $multipleByType = $this->getMultipleByType($imageAttributes); |
| 98 | + |
| 99 | + $imageEntityConfig = new ImageEntityConfig($folderName, $entityClass, $types, $multipleByType); |
| 100 | + |
| 101 | + if (isset($this->foundFolderNames[$folderName])) { |
| 102 | + $existingClass = $this->foundFolderNames[$folderName]; |
| 103 | + |
| 104 | + if (!is_subclass_of($entityClass, $existingClass) && !is_subclass_of($existingClass, $entityClass)) { |
| 105 | + throw new DuplicateEntityNameExceptionInvalid($folderName); |
| 106 | + } |
| 107 | + |
| 108 | + if (is_subclass_of($existingClass, $entityClass)) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + unset($this->foundEntityConfigs[$this->foundFolderNames[$folderName]]); |
| 113 | + } |
91 | 114 |
|
92 | | - $imageEntityConfig = new ImageEntityConfig($entityName, $entityClass, $types, $multipleByType); |
93 | | - $this->foundEntityNames[$entityName] = $entityName; |
94 | 115 | $this->foundEntityConfigs[$entityClass] = $imageEntityConfig; |
| 116 | + $this->foundFolderNames[$folderName] = $entityClass; |
95 | 117 | } |
96 | 118 |
|
97 | 119 | /** |
| 120 | + * @param array<\Shopsys\FrameworkBundle\Component\Image\Config\Attributes\EntityImage> $imageAttributes |
98 | 121 | * @return string[] |
99 | 122 | */ |
100 | | - protected function prepareTypes(array $typesConfig): array |
| 123 | + protected function prepareTypes(array $imageAttributes): array |
101 | 124 | { |
102 | 125 | $result = []; |
103 | 126 |
|
104 | | - foreach ($typesConfig as $typeConfig) { |
105 | | - $typeName = $typeConfig[ImageConfigDefinition::CONFIG_TYPE_NAME]; |
106 | | - |
107 | | - if (array_key_exists($typeName, $result)) { |
108 | | - throw new DuplicateTypeNameExceptionInvalid($typeName); |
| 127 | + foreach ($imageAttributes as $imageAttribute) { |
| 128 | + if (in_array($imageAttribute->name, $result, true)) { |
| 129 | + throw new DuplicateTypeNameExceptionInvalid($imageAttribute->name); |
109 | 130 | } |
110 | 131 |
|
111 | | - $result[$typeName] = $typeName; |
| 132 | + $result[] = $imageAttribute->name; |
112 | 133 | } |
113 | 134 |
|
114 | 135 | return $result; |
115 | 136 | } |
116 | 137 |
|
117 | 138 | /** |
118 | | - * @return array<string, bool> |
| 139 | + * @param array<\Shopsys\FrameworkBundle\Component\Image\Config\Attributes\EntityImage> $imageAttributes |
| 140 | + * @return array<string,bool> |
119 | 141 | */ |
120 | | - protected function getMultipleByType(array $entityConfig): array |
| 142 | + protected function getMultipleByType(array $imageAttributes): array |
121 | 143 | { |
122 | 144 | $multipleByType = []; |
123 | | - $multipleByType[ImageEntityConfig::WITHOUT_NAME_KEY] = $entityConfig[ImageConfigDefinition::CONFIG_MULTIPLE]; |
124 | 145 |
|
125 | | - foreach ($entityConfig[ImageConfigDefinition::CONFIG_TYPES] as $typeConfig) { |
126 | | - $type = $typeConfig[ImageConfigDefinition::CONFIG_TYPE_NAME]; |
127 | | - $multiple = $typeConfig[ImageConfigDefinition::CONFIG_MULTIPLE]; |
128 | | - $multipleByType[$type] = $multiple; |
| 146 | + foreach ($imageAttributes as $imageAttribute) { |
| 147 | + $key = $imageAttribute->name === EntityImage::DEFAULT_NAME ? ImageEntityConfig::WITHOUT_NAME_KEY : $imageAttribute->name; |
| 148 | + |
| 149 | + $multipleByType[$key] = $imageAttribute->multiple; |
129 | 150 | } |
130 | 151 |
|
131 | 152 | return $multipleByType; |
|
0 commit comments