|
4 | 4 |
|
5 | 5 | namespace Qossmic\TwigDocBundle\Service; |
6 | 6 |
|
| 7 | +use Psr\Cache\InvalidArgumentException; |
7 | 8 | use Qossmic\TwigDocBundle\Component\ComponentInvalid; |
8 | 9 | use Qossmic\TwigDocBundle\Component\ComponentItem; |
9 | 10 | use Qossmic\TwigDocBundle\Component\ComponentItemFactory; |
| 11 | +use Qossmic\TwigDocBundle\Component\ComponentItemList; |
10 | 12 | use Qossmic\TwigDocBundle\Exception\InvalidComponentConfigurationException; |
| 13 | +use Symfony\Contracts\Cache\CacheInterface; |
11 | 14 |
|
12 | 15 | class ComponentService |
13 | 16 | { |
14 | | - /** |
15 | | - * @var ComponentItem[] |
16 | | - */ |
17 | | - private array $components = []; |
18 | | - |
19 | | - /** |
20 | | - * @var array<string, array<int, ComponentItem>> |
21 | | - */ |
22 | | - private array $categories = []; |
23 | | - |
24 | | - /** |
25 | | - * @var ComponentInvalid[] |
26 | | - */ |
27 | | - private array $invalidComponents = []; |
28 | | - |
29 | 17 | public function __construct( |
30 | 18 | private readonly ComponentItemFactory $itemFactory, |
31 | 19 | private readonly array $componentsConfig, |
| 20 | + private readonly CacheInterface $cache, |
| 21 | + private readonly int $configReadTime = 0 |
32 | 22 | ) { |
33 | | - $this->parse(); |
34 | 23 | } |
35 | 24 |
|
36 | 25 | /** |
37 | | - * @return ComponentItem[] |
| 26 | + * @return ComponentItemList<ComponentItem> |
38 | 27 | */ |
39 | | - public function getComponentsByCategory(string $category): array |
| 28 | + public function getComponentsByCategory(string $category): ComponentItemList |
40 | 29 | { |
41 | | - return $this->categories[$category] ?? []; |
| 30 | + return $this->filter($category, 'category'); |
42 | 31 | } |
43 | 32 |
|
44 | 33 | /** |
45 | | - * @return array<string, array<int, ComponentItem>> |
| 34 | + * @throws InvalidArgumentException |
46 | 35 | */ |
47 | | - public function getCategories(): array |
| 36 | + public function getComponents(): ComponentItemList |
48 | 37 | { |
49 | | - return $this->categories; |
50 | | - } |
51 | | - |
52 | | - private function parse(): void |
53 | | - { |
54 | | - $components = $categories = $invalidComponents = []; |
55 | | - |
56 | | - foreach ($this->componentsConfig as $componentData) { |
57 | | - try { |
58 | | - $item = $this->itemFactory->create($componentData); |
59 | | - } catch (InvalidComponentConfigurationException $e) { |
60 | | - $item = new ComponentInvalid($e->getViolationList(), $componentData); |
61 | | - $invalidComponents[] = $item; |
62 | | - continue; |
63 | | - } |
64 | | - $components[] = $item; |
65 | | - $categories[$item->getMainCategory()->getName()][] = $item; |
66 | | - } |
67 | | - |
68 | | - $this->components = $components; |
69 | | - $this->categories = $categories; |
70 | | - $this->invalidComponents = $invalidComponents; |
71 | | - } |
72 | | - |
73 | | - public function filter(string $filterQuery, string $filterType): array |
74 | | - { |
75 | | - $components = array_unique($this->filterComponents($filterQuery, $filterType), \SORT_REGULAR); |
76 | | - |
77 | | - $result = []; |
78 | | - |
79 | | - foreach ($components as $component) { |
80 | | - $result[$component->getMainCategory()->getName()][] = $component; |
81 | | - } |
| 38 | + return new ComponentItemList( |
| 39 | + $this->cache->get('twig_doc.parsed.components'.$this->configReadTime, function () { |
| 40 | + $components = []; |
| 41 | + foreach ($this->componentsConfig as $componentData) { |
| 42 | + try { |
| 43 | + $components[] = $this->itemFactory->create($componentData); |
| 44 | + } catch (InvalidComponentConfigurationException) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + } |
82 | 48 |
|
83 | | - return $result; |
| 49 | + return $components; |
| 50 | + }) |
| 51 | + ); |
84 | 52 | } |
85 | 53 |
|
86 | | - private function filterComponents(string $filterQuery, string $filterType): array |
| 54 | + public function filter(string $filterQuery, string $filterType): ComponentItemList |
87 | 55 | { |
88 | | - $components = []; |
89 | | - switch ($filterType) { |
90 | | - case 'category': |
91 | | - $components = array_filter($this->categories, fn (string $category) => strtolower($category) === strtolower($filterQuery), \ARRAY_FILTER_USE_KEY); |
92 | | - |
93 | | - return $components[array_key_first($components)] ?? []; |
94 | | - case 'sub_category': |
95 | | - $components = array_filter( |
96 | | - $this->components, |
97 | | - fn (ComponentItem $item) => $item->getCategory()->getParent() !== null |
98 | | - && strtolower($item->getCategory()->getName()) === strtolower($filterQuery) |
99 | | - ); |
100 | | - |
101 | | - break; |
102 | | - case 'tags': |
103 | | - $tags = array_map('trim', explode(',', strtolower($filterQuery))); |
104 | | - $components = array_filter($this->components, function (ComponentItem $item) use ($tags) { |
105 | | - return array_intersect($tags, array_map('strtolower', $item->getTags())) !== []; |
106 | | - }); |
107 | | - |
108 | | - break; |
109 | | - case 'name': |
110 | | - $components = array_filter( |
111 | | - $this->components, |
112 | | - fn (ComponentItem $item) => str_contains(strtolower($item->getName()), strtolower($filterQuery))); |
113 | | - |
114 | | - break; |
115 | | - default: |
116 | | - foreach (['category', 'sub_category', 'tags', 'name'] as $type) { |
117 | | - $components = array_merge($components, $this->filterComponents($filterQuery, $type)); |
118 | | - } |
119 | | - |
120 | | - break; |
121 | | - } |
| 56 | + $hash = sprintf('twig_doc_bundle.search.%s.%s', md5($filterQuery.$filterType), $this->configReadTime); |
122 | 57 |
|
123 | | - return $components; |
| 58 | + return $this->cache->get($hash, function () use ($filterQuery, $filterType) { |
| 59 | + return $this->getComponents()->filter($filterQuery, $filterType); |
| 60 | + }); |
124 | 61 | } |
125 | 62 |
|
126 | 63 | /** |
127 | 64 | * @return ComponentInvalid[] |
| 65 | + * |
| 66 | + * @throws InvalidArgumentException |
128 | 67 | */ |
129 | 68 | public function getInvalidComponents(): array |
130 | 69 | { |
131 | | - return $this->invalidComponents; |
| 70 | + return $this->cache->get('twig_doc_bundle.invalid_components'.$this->configReadTime, function () { |
| 71 | + $invalid = array_filter($this->componentsConfig, function ($cmpData) { |
| 72 | + foreach ($this->getComponents()->getArrayCopy() as $cmp) { |
| 73 | + if ($cmp->getName() === $cmpData['name'] ?? null) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + }); |
| 80 | + $invalidComponents = []; |
| 81 | + |
| 82 | + foreach ($invalid as $cmpData) { |
| 83 | + try { |
| 84 | + $this->itemFactory->create($cmpData); |
| 85 | + } catch (InvalidComponentConfigurationException $e) { |
| 86 | + $invalidComponents[] = new ComponentInvalid($e->getViolationList(), $cmpData); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return $invalidComponents; |
| 91 | + }); |
132 | 92 | } |
133 | 93 |
|
134 | 94 | public function getComponent(string $name): ?ComponentItem |
135 | 95 | { |
136 | | - return array_values(array_filter($this->components, fn (ComponentItem $c) => $c->getName() === $name))[0] ?? null; |
| 96 | + return array_values(array_filter((array) $this->getComponents(), fn (ComponentItem $c) => $c->getName() === $name))[0] ?? null; |
137 | 97 | } |
138 | 98 | } |
0 commit comments