1 | <?php |
||
22 | class CollectionManager extends TrackingManager |
||
23 | { |
||
24 | /** @var string[][] A copy of the collection definitions to be available for later usage. */ |
||
25 | private $collectionDefinitions; |
||
26 | private $configuration; |
||
27 | private $eventDispatcher; |
||
28 | private $logger; |
||
29 | |||
30 | /** |
||
31 | * CollectionManager constructor. |
||
32 | */ |
||
33 | 36 | public function __construct(Configuration $configuration, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
|
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 6 | public function compileManager() |
|
44 | { |
||
45 | 6 | if (!$this->configuration->hasCollections()) |
|
46 | 6 | { |
|
47 | $this->logger->notice('No Collections defined... Ignoring'); |
||
48 | return; |
||
49 | } |
||
50 | |||
51 | 6 | $this->parseCollections($this->configuration->getCollectionsFolders()); |
|
52 | 6 | } |
|
53 | |||
54 | /** |
||
55 | * Get all of the ContentItems grouped by Collection name. |
||
56 | * |
||
57 | * ```php |
||
58 | * [ |
||
59 | * 'collection name' => [ |
||
60 | * new ContentItem(), |
||
61 | * new ContentItem(), |
||
62 | * ] |
||
63 | * ] |
||
64 | * ``` |
||
65 | * |
||
66 | * @return ContentItem[][] |
||
67 | */ |
||
68 | 15 | public function &getCollections() |
|
72 | |||
73 | /** |
||
74 | * Get a ContentItem from a Collection passed on it's path. |
||
75 | * |
||
76 | * @param string $filePath |
||
77 | * |
||
78 | * @throws TrackedItemNotFoundException |
||
79 | * |
||
80 | * @return ContentItem |
||
81 | */ |
||
82 | 1 | public function &getContentItem($filePath) |
|
83 | { |
||
84 | 1 | if (!isset($this->trackedItemsFlattened[$filePath])) |
|
85 | 1 | { |
|
86 | throw new TrackedItemNotFoundException("The ContentItem at '$filePath' was not found."); |
||
87 | } |
||
88 | |||
89 | 1 | return $this->trackedItemsFlattened[$filePath]; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * A jailed representation of CollectionManager::getCollections(). |
||
94 | * |
||
95 | * @return JailedDocument[][] |
||
|
|||
96 | */ |
||
97 | 19 | public function getJailedCollections() |
|
101 | |||
102 | /** |
||
103 | * Parse every collection and store them in the manager. |
||
104 | * |
||
105 | * @param string[][] $collections An array of definitions for collections |
||
106 | */ |
||
107 | 36 | public function parseCollections($collections) |
|
108 | { |
||
109 | 36 | if ($collections == null || empty($collections)) |
|
110 | 36 | { |
|
111 | 1 | $this->logger->debug('No collections found, nothing to parse.'); |
|
112 | |||
113 | 1 | return; |
|
114 | } |
||
115 | |||
116 | 36 | $this->collectionDefinitions = $collections; |
|
117 | |||
118 | /** |
||
119 | * The information which each collection has taken from the configuration file. |
||
120 | * |
||
121 | * $collection['name'] string The name of the collection |
||
122 | * ['folder'] string The folder where this collection has its ContentItems |
||
123 | * |
||
124 | * @var array |
||
125 | */ |
||
126 | 36 | foreach ($collections as $collection) |
|
127 | { |
||
128 | 36 | $this->logger->notice('Loading "{name}" collection...', [ |
|
129 | 36 | 'name' => $collection['name'], |
|
130 | 36 | ]); |
|
131 | |||
132 | 36 | $collectionFolder = fs::absolutePath($collection['folder']); |
|
133 | |||
134 | 36 | if (!fs::exists($collectionFolder)) |
|
135 | 36 | { |
|
136 | $this->logger->warning('The folder "{folder}" could not be found for the "{name}" collection', [ |
||
137 | 'folder' => $collection['folder'], |
||
138 | 'name' => $collection['name'], |
||
139 | ]); |
||
140 | |||
141 | continue; |
||
142 | } |
||
143 | |||
144 | 36 | $this->saveFolderDefinition($collection['folder'], $collection); |
|
145 | 36 | $this->scanTrackableItems($collectionFolder, [ |
|
146 | 36 | 'namespace' => $collection['name'], |
|
147 | 36 | ]); |
|
148 | 36 | } |
|
149 | 36 | } |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 1 | public function createNewItem($filePath) |
|
155 | { |
||
156 | 1 | $collection = $this->getCollectionNameFromPath($filePath); |
|
157 | |||
158 | 1 | return $this->handleTrackableItem($filePath, [ |
|
159 | 1 | 'namespace' => $collection, |
|
160 | 1 | ]); |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function refreshItem($filePath) |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | 36 | protected function handleTrackableItem(File $filePath, array $options = []) |
|
192 | |||
193 | /** |
||
194 | * Get the name of the Collection this ContentItem belongs to based on its location. |
||
195 | * |
||
196 | * @param File $file |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | 1 | private function getCollectionNameFromPath(File $file) |
|
212 | } |
||
213 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.