@@ -26,29 +26,29 @@ |
||
26 | 26 | interface ViewFinder extends Finder |
27 | 27 | { |
28 | 28 | |
29 | - // Constants to be used for the Config file sections. |
|
30 | - const CLASS_NAME_KEY = 'ClassName'; |
|
31 | - const VIEWS_KEY = 'Views'; |
|
32 | - const NULL_OBJECT = 'NullObject'; |
|
29 | + // Constants to be used for the Config file sections. |
|
30 | + const CLASS_NAME_KEY = 'ClassName'; |
|
31 | + const VIEWS_KEY = 'Views'; |
|
32 | + const NULL_OBJECT = 'NullObject'; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Find a result based on a specific criteria. |
|
36 | - * |
|
37 | - * @since 0.1.0 |
|
38 | - * |
|
39 | - * @param array $criteria Criteria to search for. |
|
40 | - * @param Engine|null $engine Optional. Engine to use with the view. |
|
41 | - * |
|
42 | - * @return View View that was found. |
|
43 | - */ |
|
44 | - public function find(array $criteria, Engine $engine = null): View; |
|
34 | + /** |
|
35 | + * Find a result based on a specific criteria. |
|
36 | + * |
|
37 | + * @since 0.1.0 |
|
38 | + * |
|
39 | + * @param array $criteria Criteria to search for. |
|
40 | + * @param Engine|null $engine Optional. Engine to use with the view. |
|
41 | + * |
|
42 | + * @return View View that was found. |
|
43 | + */ |
|
44 | + public function find(array $criteria, Engine $engine = null): View; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the NullObject. |
|
48 | - * |
|
49 | - * @since 0.1.1 |
|
50 | - * |
|
51 | - * @return NullView NullObject for the current Finder. |
|
52 | - */ |
|
53 | - public function getNullObject(): NullView; |
|
46 | + /** |
|
47 | + * Get the NullObject. |
|
48 | + * |
|
49 | + * @since 0.1.1 |
|
50 | + * |
|
51 | + * @return NullView NullObject for the current Finder. |
|
52 | + */ |
|
53 | + public function getNullObject(): NullView; |
|
54 | 54 | } |
@@ -27,56 +27,56 @@ |
||
27 | 27 | class BaseViewFinder extends AbstractFinder implements ViewFinder |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Find a result based on a specific criteria. |
|
32 | - * |
|
33 | - * @since 0.1.0 |
|
34 | - * |
|
35 | - * @param array $criteria Criteria to search for. |
|
36 | - * @param Engine|null $engine Optional. Engine to use with the view. |
|
37 | - * |
|
38 | - * @return View View that was found. |
|
39 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
40 | - */ |
|
41 | - public function find(array $criteria, Engine $engine = null): View |
|
42 | - { |
|
43 | - $uri = $criteria[0]; |
|
30 | + /** |
|
31 | + * Find a result based on a specific criteria. |
|
32 | + * |
|
33 | + * @since 0.1.0 |
|
34 | + * |
|
35 | + * @param array $criteria Criteria to search for. |
|
36 | + * @param Engine|null $engine Optional. Engine to use with the view. |
|
37 | + * |
|
38 | + * @return View View that was found. |
|
39 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
40 | + */ |
|
41 | + public function find(array $criteria, Engine $engine = null): View |
|
42 | + { |
|
43 | + $uri = $criteria[0]; |
|
44 | 44 | |
45 | - $views = $this->initializeFindables([$uri, $engine]); |
|
45 | + $views = $this->initializeFindables([$uri, $engine]); |
|
46 | 46 | |
47 | - foreach ($criteria as $entry) { |
|
48 | - foreach ($views as $viewObject) { |
|
49 | - if ($viewObject->canHandle($entry)) { |
|
50 | - return $viewObject; |
|
51 | - } |
|
52 | - } |
|
53 | - } |
|
47 | + foreach ($criteria as $entry) { |
|
48 | + foreach ($views as $viewObject) { |
|
49 | + if ($viewObject->canHandle($entry)) { |
|
50 | + return $viewObject; |
|
51 | + } |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - return $this->getNullObject(); |
|
56 | - } |
|
55 | + return $this->getNullObject(); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Get the config key for the Findables definitions. |
|
60 | - * |
|
61 | - * @since 0.1.0 |
|
62 | - * |
|
63 | - * @return string Config key use to define the Findables. |
|
64 | - */ |
|
65 | - protected function getFindablesConfigKey(): string |
|
66 | - { |
|
67 | - return 'Views'; |
|
68 | - } |
|
58 | + /** |
|
59 | + * Get the config key for the Findables definitions. |
|
60 | + * |
|
61 | + * @since 0.1.0 |
|
62 | + * |
|
63 | + * @return string Config key use to define the Findables. |
|
64 | + */ |
|
65 | + protected function getFindablesConfigKey(): string |
|
66 | + { |
|
67 | + return 'Views'; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get the NullObject. |
|
72 | - * |
|
73 | - * @since 0.1.1 |
|
74 | - * |
|
75 | - * @return NullView NullObject for the current Finder. |
|
76 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
77 | - */ |
|
78 | - public function getNullObject(): NullView |
|
79 | - { |
|
80 | - return parent::getNullObject(); |
|
81 | - } |
|
70 | + /** |
|
71 | + * Get the NullObject. |
|
72 | + * |
|
73 | + * @since 0.1.1 |
|
74 | + * |
|
75 | + * @return NullView NullObject for the current Finder. |
|
76 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
77 | + */ |
|
78 | + public function getNullObject(): NullView |
|
79 | + { |
|
80 | + return parent::getNullObject(); |
|
81 | + } |
|
82 | 82 | } |
@@ -23,17 +23,17 @@ |
||
23 | 23 | */ |
24 | 24 | interface EngineFinder extends Finder |
25 | 25 | { |
26 | - // Constants to be used for the Config file sections. |
|
27 | - const CLASS_NAME_KEY = 'ClassName'; |
|
28 | - const ENGINES_KEY = 'Engines'; |
|
29 | - const NULL_OBJECT = 'NullObject'; |
|
26 | + // Constants to be used for the Config file sections. |
|
27 | + const CLASS_NAME_KEY = 'ClassName'; |
|
28 | + const ENGINES_KEY = 'Engines'; |
|
29 | + const NULL_OBJECT = 'NullObject'; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Get the NullObject. |
|
33 | - * |
|
34 | - * @since 0.1.1 |
|
35 | - * |
|
36 | - * @return NullEngine NullObject for the current Finder. |
|
37 | - */ |
|
38 | - public function getNullObject(): NullEngine; |
|
31 | + /** |
|
32 | + * Get the NullObject. |
|
33 | + * |
|
34 | + * @since 0.1.1 |
|
35 | + * |
|
36 | + * @return NullEngine NullObject for the current Finder. |
|
37 | + */ |
|
38 | + public function getNullObject(): NullEngine; |
|
39 | 39 | } |
@@ -25,53 +25,53 @@ |
||
25 | 25 | class BaseEngineFinder extends AbstractFinder implements EngineFinder |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Find a result based on a specific criteria. |
|
30 | - * |
|
31 | - * @since 0.1.0 |
|
32 | - * |
|
33 | - * @param array $criteria Criteria to search for. |
|
34 | - * |
|
35 | - * @return Engine Result of the search. |
|
36 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
37 | - */ |
|
38 | - public function find(array $criteria): Engine |
|
39 | - { |
|
40 | - $this->findables = $this->initializeFindables(); |
|
28 | + /** |
|
29 | + * Find a result based on a specific criteria. |
|
30 | + * |
|
31 | + * @since 0.1.0 |
|
32 | + * |
|
33 | + * @param array $criteria Criteria to search for. |
|
34 | + * |
|
35 | + * @return Engine Result of the search. |
|
36 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
37 | + */ |
|
38 | + public function find(array $criteria): Engine |
|
39 | + { |
|
40 | + $this->findables = $this->initializeFindables(); |
|
41 | 41 | |
42 | - foreach ($criteria as $entry) { |
|
43 | - foreach ($this->findables as $engine) { |
|
44 | - if ($engine->canHandle($entry)) { |
|
45 | - return $engine; |
|
46 | - } |
|
47 | - } |
|
48 | - } |
|
42 | + foreach ($criteria as $entry) { |
|
43 | + foreach ($this->findables as $engine) { |
|
44 | + if ($engine->canHandle($entry)) { |
|
45 | + return $engine; |
|
46 | + } |
|
47 | + } |
|
48 | + } |
|
49 | 49 | |
50 | - return $this->getNullObject(); |
|
51 | - } |
|
50 | + return $this->getNullObject(); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the config key for the Findables definitions. |
|
55 | - * |
|
56 | - * @since 0.1.0 |
|
57 | - * |
|
58 | - * @return string Config key use to define the Findables. |
|
59 | - */ |
|
60 | - protected function getFindablesConfigKey(): string |
|
61 | - { |
|
62 | - return 'Engines'; |
|
63 | - } |
|
53 | + /** |
|
54 | + * Get the config key for the Findables definitions. |
|
55 | + * |
|
56 | + * @since 0.1.0 |
|
57 | + * |
|
58 | + * @return string Config key use to define the Findables. |
|
59 | + */ |
|
60 | + protected function getFindablesConfigKey(): string |
|
61 | + { |
|
62 | + return 'Engines'; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Get the NullObject. |
|
67 | - * |
|
68 | - * @since 0.1.1 |
|
69 | - * |
|
70 | - * @return NullEngine NullObject for the current Finder. |
|
71 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
72 | - */ |
|
73 | - public function getNullObject(): NullEngine |
|
74 | - { |
|
75 | - return parent::getNullObject(); |
|
76 | - } |
|
65 | + /** |
|
66 | + * Get the NullObject. |
|
67 | + * |
|
68 | + * @since 0.1.1 |
|
69 | + * |
|
70 | + * @return NullEngine NullObject for the current Finder. |
|
71 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
72 | + */ |
|
73 | + public function getNullObject(): NullEngine |
|
74 | + { |
|
75 | + return parent::getNullObject(); |
|
76 | + } |
|
77 | 77 | } |
@@ -22,23 +22,23 @@ |
||
22 | 22 | interface Finder |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Find a result based on a specific criteria. |
|
27 | - * |
|
28 | - * @since 0.1.0 |
|
29 | - * |
|
30 | - * @param array $criteria Criteria to search for. |
|
31 | - * |
|
32 | - * @return mixed Result of the search. |
|
33 | - */ |
|
34 | - public function find(array $criteria); |
|
25 | + /** |
|
26 | + * Find a result based on a specific criteria. |
|
27 | + * |
|
28 | + * @since 0.1.0 |
|
29 | + * |
|
30 | + * @param array $criteria Criteria to search for. |
|
31 | + * |
|
32 | + * @return mixed Result of the search. |
|
33 | + */ |
|
34 | + public function find(array $criteria); |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get the NullObject. |
|
38 | - * |
|
39 | - * @since 0.1.1 |
|
40 | - * |
|
41 | - * @return NullFindable NullObject for the current Finder. |
|
42 | - */ |
|
43 | - public function getNullObject(); |
|
36 | + /** |
|
37 | + * Get the NullObject. |
|
38 | + * |
|
39 | + * @since 0.1.1 |
|
40 | + * |
|
41 | + * @return NullFindable NullObject for the current Finder. |
|
42 | + */ |
|
43 | + public function getNullObject(); |
|
44 | 44 | } |
@@ -22,58 +22,58 @@ |
||
22 | 22 | class URIHelper |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Check whether a given URI has a specific extension. |
|
27 | - * |
|
28 | - * @since 0.1.3 |
|
29 | - * |
|
30 | - * @param string $uri URI to check the extension of. |
|
31 | - * @param string $extension Extension to check for. |
|
32 | - * |
|
33 | - * @return bool |
|
34 | - */ |
|
35 | - public static function hasExtension(string $uri, string $extension): bool |
|
36 | - { |
|
37 | - $extension = '.' . ltrim($extension, '.'); |
|
38 | - $uriLength = mb_strlen($uri); |
|
39 | - $extensionLength = mb_strlen($extension); |
|
40 | - if ($extensionLength > $uriLength) { |
|
41 | - return false; |
|
42 | - } |
|
25 | + /** |
|
26 | + * Check whether a given URI has a specific extension. |
|
27 | + * |
|
28 | + * @since 0.1.3 |
|
29 | + * |
|
30 | + * @param string $uri URI to check the extension of. |
|
31 | + * @param string $extension Extension to check for. |
|
32 | + * |
|
33 | + * @return bool |
|
34 | + */ |
|
35 | + public static function hasExtension(string $uri, string $extension): bool |
|
36 | + { |
|
37 | + $extension = '.' . ltrim($extension, '.'); |
|
38 | + $uriLength = mb_strlen($uri); |
|
39 | + $extensionLength = mb_strlen($extension); |
|
40 | + if ($extensionLength > $uriLength) { |
|
41 | + return false; |
|
42 | + } |
|
43 | 43 | |
44 | - return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0; |
|
45 | - } |
|
44 | + return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Check whether a given URI contains an extension and return it. |
|
49 | - * |
|
50 | - * @param string $uri URI to check. |
|
51 | - * @return string Extension that was found, empty string if none found. |
|
52 | - */ |
|
53 | - public static function containsExtension(string $uri): string |
|
54 | - { |
|
55 | - $pathParts = explode('/', $uri); |
|
56 | - $filename = array_pop($pathParts); |
|
57 | - $filenameParts = explode('.', $filename); |
|
47 | + /** |
|
48 | + * Check whether a given URI contains an extension and return it. |
|
49 | + * |
|
50 | + * @param string $uri URI to check. |
|
51 | + * @return string Extension that was found, empty string if none found. |
|
52 | + */ |
|
53 | + public static function containsExtension(string $uri): string |
|
54 | + { |
|
55 | + $pathParts = explode('/', $uri); |
|
56 | + $filename = array_pop($pathParts); |
|
57 | + $filenameParts = explode('.', $filename); |
|
58 | 58 | |
59 | - if (count($filenameParts) > 1) { |
|
60 | - return array_pop($filenameParts); |
|
61 | - } |
|
59 | + if (count($filenameParts) > 1) { |
|
60 | + return array_pop($filenameParts); |
|
61 | + } |
|
62 | 62 | |
63 | - return ''; |
|
64 | - } |
|
63 | + return ''; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Get the filename for an URI. |
|
68 | - * |
|
69 | - * @since 0.1.3 |
|
70 | - * |
|
71 | - * @param string $uri URI to get the filename from. |
|
72 | - * |
|
73 | - * @return string Filename without path. |
|
74 | - */ |
|
75 | - public static function getFilename(string $uri): String |
|
76 | - { |
|
77 | - return basename($uri); |
|
78 | - } |
|
66 | + /** |
|
67 | + * Get the filename for an URI. |
|
68 | + * |
|
69 | + * @since 0.1.3 |
|
70 | + * |
|
71 | + * @param string $uri URI to get the filename from. |
|
72 | + * |
|
73 | + * @return string Filename without path. |
|
74 | + */ |
|
75 | + public static function getFilename(string $uri): String |
|
76 | + { |
|
77 | + return basename($uri); |
|
78 | + } |
|
79 | 79 | } |
@@ -34,7 +34,7 @@ |
||
34 | 34 | */ |
35 | 35 | public static function hasExtension(string $uri, string $extension): bool |
36 | 36 | { |
37 | - $extension = '.' . ltrim($extension, '.'); |
|
37 | + $extension = '.'.ltrim($extension, '.'); |
|
38 | 38 | $uriLength = mb_strlen($uri); |
39 | 39 | $extensionLength = mb_strlen($extension); |
40 | 40 | if ($extensionLength > $uriLength) { |
@@ -27,216 +27,216 @@ |
||
27 | 27 | abstract class AbstractFinder implements Finder |
28 | 28 | { |
29 | 29 | |
30 | - use ConfigTrait; |
|
31 | - |
|
32 | - /** |
|
33 | - * Findable collection that the Finder can iterate through to find a match. |
|
34 | - * |
|
35 | - * @since 0.1.0 |
|
36 | - * |
|
37 | - * @var Findables |
|
38 | - */ |
|
39 | - protected $findables; |
|
40 | - |
|
41 | - /** |
|
42 | - * NullObject that is returned if the Finder could not find a match. |
|
43 | - * |
|
44 | - * @since 0.1.0 |
|
45 | - * |
|
46 | - * @var NullFindable |
|
47 | - */ |
|
48 | - protected $nullObject; |
|
49 | - |
|
50 | - /** |
|
51 | - * Instantiate an AbstractFinder object. |
|
52 | - * |
|
53 | - * @since 0.1.0 |
|
54 | - * |
|
55 | - * @param ConfigInterface $config Configuration of the AbstractFinder. |
|
56 | - * |
|
57 | - * @throws FailedToProcessConfigException If the config could not be processed. |
|
58 | - */ |
|
59 | - public function __construct(ConfigInterface $config) |
|
60 | - { |
|
61 | - $this->processConfig($config); |
|
62 | - $this->findables = new Findables(); |
|
63 | - $this->registerFindables($this->config); |
|
64 | - $this->registerNullObject($this->config); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Register the Findables defined in the given configuration. |
|
69 | - * |
|
70 | - * @since 0.1.0 |
|
71 | - * |
|
72 | - * @param ConfigInterface $config Configuration to register the Findables from. |
|
73 | - */ |
|
74 | - public function registerFindables(ConfigInterface $config) |
|
75 | - { |
|
76 | - $findables = (array) $config->getKey($this->getFindablesConfigKey()); |
|
77 | - foreach ($findables as $findableKey => $findableObject) { |
|
78 | - $this->findables->set($findableKey, $findableObject); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Register the NullObject defined in the given configuration. |
|
84 | - * |
|
85 | - * @since 0.1.0 |
|
86 | - * |
|
87 | - * @param ConfigInterface $config Configuration to register the NullObject from. |
|
88 | - */ |
|
89 | - public function registerNullObject(ConfigInterface $config) |
|
90 | - { |
|
91 | - $this->nullObject = $config->getKey($this->getNullObjectConfigKey()); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Get the NullObject. |
|
96 | - * |
|
97 | - * @since 0.1.1 |
|
98 | - * |
|
99 | - * @return NullFindable NullObject for the current Finder. |
|
100 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
101 | - */ |
|
102 | - public function getNullObject() |
|
103 | - { |
|
104 | - $this->initializeNullObject(); |
|
105 | - |
|
106 | - return $this->nullObject; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Get the config key for the Findables definitions. |
|
111 | - * |
|
112 | - * @since 0.1.0 |
|
113 | - * |
|
114 | - * @return string Config key use to define the Findables. |
|
115 | - */ |
|
116 | - protected function getFindablesConfigKey(): string |
|
117 | - { |
|
118 | - return 'Findables'; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Get the config key for the NullObject definitions. |
|
123 | - * |
|
124 | - * @since 0.1.0 |
|
125 | - * |
|
126 | - * @return string Config key use to define the NullObject. |
|
127 | - */ |
|
128 | - protected function getNullObjectConfigKey(): string |
|
129 | - { |
|
130 | - return 'NullObject'; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Initialize the NullObject. |
|
135 | - * |
|
136 | - * @since 0.1.1 |
|
137 | - * |
|
138 | - * @param mixed $arguments Optional. Arguments to use. |
|
139 | - * |
|
140 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
141 | - */ |
|
142 | - protected function initializeNullObject($arguments = null) |
|
143 | - { |
|
144 | - $this->nullObject = $this->maybeInstantiateFindable($this->nullObject, $arguments); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Initialize the Findables that can be iterated. |
|
149 | - * |
|
150 | - * @param mixed $arguments Optional. Arguments to use. |
|
151 | - * |
|
152 | - * @since 0.1.0 |
|
153 | - * |
|
154 | - * @return Findables Collection of Findables. |
|
155 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
156 | - */ |
|
157 | - protected function initializeFindables($arguments = null): Findables |
|
158 | - { |
|
159 | - return $this->findables->map(function ($findable) use ($arguments) { |
|
160 | - return $this->initializeFindable($findable, $arguments); |
|
161 | - }); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Initialize a single findable by instantiating class name strings and calling closures. |
|
166 | - * |
|
167 | - * @since 0.1.0 |
|
168 | - * |
|
169 | - * @param mixed $findable Findable to instantiate. |
|
170 | - * @param mixed $arguments Optional. Arguments to use. |
|
171 | - * |
|
172 | - * @return Findable Instantiated findable. |
|
173 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
174 | - */ |
|
175 | - protected function initializeFindable($findable, $arguments = null): Findable |
|
176 | - { |
|
177 | - return $this->maybeInstantiateFindable($findable, $arguments); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Maybe instantiate a Findable if it is not yet an object. |
|
182 | - * |
|
183 | - * @since 0.1.1 |
|
184 | - * |
|
185 | - * @param mixed $findable Findable to instantiate. |
|
186 | - * @param mixed $arguments Optional. Arguments to use. |
|
187 | - * |
|
188 | - * @return Findable Instantiated findable. |
|
189 | - * @throws FailedToInstantiateFindable If the findable could not be instantiated. |
|
190 | - */ |
|
191 | - protected function maybeInstantiateFindable($findable, $arguments = null): Findable |
|
192 | - { |
|
193 | - if (is_string($findable)) { |
|
194 | - $findable = $this->instantiateFindableFromString($findable, $arguments); |
|
195 | - } |
|
196 | - |
|
197 | - if (is_callable($findable)) { |
|
198 | - $findable = $this->instantiateFindableFromCallable($findable, $arguments); |
|
199 | - } |
|
200 | - |
|
201 | - if (! $findable instanceof Findable) { |
|
202 | - throw new FailedToInstantiateFindable( |
|
203 | - sprintf( |
|
204 | - _('Could not instantiate Findable "%s".'), |
|
205 | - serialize($findable) |
|
206 | - ) |
|
207 | - ); |
|
208 | - } |
|
209 | - |
|
210 | - return $findable; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Instantiate a Findable from a string. |
|
215 | - * |
|
216 | - * @since 0.1.1 |
|
217 | - * |
|
218 | - * @param string $string String to use for instantiation. |
|
219 | - * @param mixed $arguments Optional. Arguments to use for instantiation. |
|
220 | - * |
|
221 | - * @return Findable Instantiated Findable. |
|
222 | - */ |
|
223 | - protected function instantiateFindableFromString(string $string, $arguments = []): Findable |
|
224 | - { |
|
225 | - return new $string(...(array)$arguments); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Instantiate a Findable from a callable. |
|
230 | - * |
|
231 | - * @since 0.1.1 |
|
232 | - * |
|
233 | - * @param callable $callable Callable to use for instantiation. |
|
234 | - * @param mixed $arguments Optional. Arguments to use for instantiation. |
|
235 | - * |
|
236 | - * @return Findable Instantiated Findable. |
|
237 | - */ |
|
238 | - protected function instantiateFindableFromCallable(callable $callable, $arguments = []): Findable |
|
239 | - { |
|
240 | - return $callable(...(array)$arguments); |
|
241 | - } |
|
30 | + use ConfigTrait; |
|
31 | + |
|
32 | + /** |
|
33 | + * Findable collection that the Finder can iterate through to find a match. |
|
34 | + * |
|
35 | + * @since 0.1.0 |
|
36 | + * |
|
37 | + * @var Findables |
|
38 | + */ |
|
39 | + protected $findables; |
|
40 | + |
|
41 | + /** |
|
42 | + * NullObject that is returned if the Finder could not find a match. |
|
43 | + * |
|
44 | + * @since 0.1.0 |
|
45 | + * |
|
46 | + * @var NullFindable |
|
47 | + */ |
|
48 | + protected $nullObject; |
|
49 | + |
|
50 | + /** |
|
51 | + * Instantiate an AbstractFinder object. |
|
52 | + * |
|
53 | + * @since 0.1.0 |
|
54 | + * |
|
55 | + * @param ConfigInterface $config Configuration of the AbstractFinder. |
|
56 | + * |
|
57 | + * @throws FailedToProcessConfigException If the config could not be processed. |
|
58 | + */ |
|
59 | + public function __construct(ConfigInterface $config) |
|
60 | + { |
|
61 | + $this->processConfig($config); |
|
62 | + $this->findables = new Findables(); |
|
63 | + $this->registerFindables($this->config); |
|
64 | + $this->registerNullObject($this->config); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Register the Findables defined in the given configuration. |
|
69 | + * |
|
70 | + * @since 0.1.0 |
|
71 | + * |
|
72 | + * @param ConfigInterface $config Configuration to register the Findables from. |
|
73 | + */ |
|
74 | + public function registerFindables(ConfigInterface $config) |
|
75 | + { |
|
76 | + $findables = (array) $config->getKey($this->getFindablesConfigKey()); |
|
77 | + foreach ($findables as $findableKey => $findableObject) { |
|
78 | + $this->findables->set($findableKey, $findableObject); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Register the NullObject defined in the given configuration. |
|
84 | + * |
|
85 | + * @since 0.1.0 |
|
86 | + * |
|
87 | + * @param ConfigInterface $config Configuration to register the NullObject from. |
|
88 | + */ |
|
89 | + public function registerNullObject(ConfigInterface $config) |
|
90 | + { |
|
91 | + $this->nullObject = $config->getKey($this->getNullObjectConfigKey()); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Get the NullObject. |
|
96 | + * |
|
97 | + * @since 0.1.1 |
|
98 | + * |
|
99 | + * @return NullFindable NullObject for the current Finder. |
|
100 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
101 | + */ |
|
102 | + public function getNullObject() |
|
103 | + { |
|
104 | + $this->initializeNullObject(); |
|
105 | + |
|
106 | + return $this->nullObject; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Get the config key for the Findables definitions. |
|
111 | + * |
|
112 | + * @since 0.1.0 |
|
113 | + * |
|
114 | + * @return string Config key use to define the Findables. |
|
115 | + */ |
|
116 | + protected function getFindablesConfigKey(): string |
|
117 | + { |
|
118 | + return 'Findables'; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Get the config key for the NullObject definitions. |
|
123 | + * |
|
124 | + * @since 0.1.0 |
|
125 | + * |
|
126 | + * @return string Config key use to define the NullObject. |
|
127 | + */ |
|
128 | + protected function getNullObjectConfigKey(): string |
|
129 | + { |
|
130 | + return 'NullObject'; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Initialize the NullObject. |
|
135 | + * |
|
136 | + * @since 0.1.1 |
|
137 | + * |
|
138 | + * @param mixed $arguments Optional. Arguments to use. |
|
139 | + * |
|
140 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
141 | + */ |
|
142 | + protected function initializeNullObject($arguments = null) |
|
143 | + { |
|
144 | + $this->nullObject = $this->maybeInstantiateFindable($this->nullObject, $arguments); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Initialize the Findables that can be iterated. |
|
149 | + * |
|
150 | + * @param mixed $arguments Optional. Arguments to use. |
|
151 | + * |
|
152 | + * @since 0.1.0 |
|
153 | + * |
|
154 | + * @return Findables Collection of Findables. |
|
155 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
156 | + */ |
|
157 | + protected function initializeFindables($arguments = null): Findables |
|
158 | + { |
|
159 | + return $this->findables->map(function ($findable) use ($arguments) { |
|
160 | + return $this->initializeFindable($findable, $arguments); |
|
161 | + }); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Initialize a single findable by instantiating class name strings and calling closures. |
|
166 | + * |
|
167 | + * @since 0.1.0 |
|
168 | + * |
|
169 | + * @param mixed $findable Findable to instantiate. |
|
170 | + * @param mixed $arguments Optional. Arguments to use. |
|
171 | + * |
|
172 | + * @return Findable Instantiated findable. |
|
173 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
174 | + */ |
|
175 | + protected function initializeFindable($findable, $arguments = null): Findable |
|
176 | + { |
|
177 | + return $this->maybeInstantiateFindable($findable, $arguments); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Maybe instantiate a Findable if it is not yet an object. |
|
182 | + * |
|
183 | + * @since 0.1.1 |
|
184 | + * |
|
185 | + * @param mixed $findable Findable to instantiate. |
|
186 | + * @param mixed $arguments Optional. Arguments to use. |
|
187 | + * |
|
188 | + * @return Findable Instantiated findable. |
|
189 | + * @throws FailedToInstantiateFindable If the findable could not be instantiated. |
|
190 | + */ |
|
191 | + protected function maybeInstantiateFindable($findable, $arguments = null): Findable |
|
192 | + { |
|
193 | + if (is_string($findable)) { |
|
194 | + $findable = $this->instantiateFindableFromString($findable, $arguments); |
|
195 | + } |
|
196 | + |
|
197 | + if (is_callable($findable)) { |
|
198 | + $findable = $this->instantiateFindableFromCallable($findable, $arguments); |
|
199 | + } |
|
200 | + |
|
201 | + if (! $findable instanceof Findable) { |
|
202 | + throw new FailedToInstantiateFindable( |
|
203 | + sprintf( |
|
204 | + _('Could not instantiate Findable "%s".'), |
|
205 | + serialize($findable) |
|
206 | + ) |
|
207 | + ); |
|
208 | + } |
|
209 | + |
|
210 | + return $findable; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Instantiate a Findable from a string. |
|
215 | + * |
|
216 | + * @since 0.1.1 |
|
217 | + * |
|
218 | + * @param string $string String to use for instantiation. |
|
219 | + * @param mixed $arguments Optional. Arguments to use for instantiation. |
|
220 | + * |
|
221 | + * @return Findable Instantiated Findable. |
|
222 | + */ |
|
223 | + protected function instantiateFindableFromString(string $string, $arguments = []): Findable |
|
224 | + { |
|
225 | + return new $string(...(array)$arguments); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Instantiate a Findable from a callable. |
|
230 | + * |
|
231 | + * @since 0.1.1 |
|
232 | + * |
|
233 | + * @param callable $callable Callable to use for instantiation. |
|
234 | + * @param mixed $arguments Optional. Arguments to use for instantiation. |
|
235 | + * |
|
236 | + * @return Findable Instantiated Findable. |
|
237 | + */ |
|
238 | + protected function instantiateFindableFromCallable(callable $callable, $arguments = []): Findable |
|
239 | + { |
|
240 | + return $callable(...(array)$arguments); |
|
241 | + } |
|
242 | 242 | } |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | */ |
157 | 157 | protected function initializeFindables($arguments = null): Findables |
158 | 158 | { |
159 | - return $this->findables->map(function ($findable) use ($arguments) { |
|
159 | + return $this->findables->map(function($findable) use ($arguments) { |
|
160 | 160 | return $this->initializeFindable($findable, $arguments); |
161 | 161 | }); |
162 | 162 | } |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | $findable = $this->instantiateFindableFromCallable($findable, $arguments); |
199 | 199 | } |
200 | 200 | |
201 | - if (! $findable instanceof Findable) { |
|
201 | + if ( ! $findable instanceof Findable) { |
|
202 | 202 | throw new FailedToInstantiateFindable( |
203 | 203 | sprintf( |
204 | 204 | _('Could not instantiate Findable "%s".'), |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | */ |
223 | 223 | protected function instantiateFindableFromString(string $string, $arguments = []): Findable |
224 | 224 | { |
225 | - return new $string(...(array)$arguments); |
|
225 | + return new $string(...(array) $arguments); |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | /** |
@@ -237,6 +237,6 @@ discard block |
||
237 | 237 | */ |
238 | 238 | protected function instantiateFindableFromCallable(callable $callable, $arguments = []): Findable |
239 | 239 | { |
240 | - return $callable(...(array)$arguments); |
|
240 | + return $callable(...(array) $arguments); |
|
241 | 241 | } |
242 | 242 | } |
@@ -24,74 +24,74 @@ |
||
24 | 24 | interface View extends Findable |
25 | 25 | { |
26 | 26 | |
27 | - const MERGE = 'merge'; |
|
28 | - const REPLACE = 'replace'; |
|
29 | - const ADD_ONLY = 'add-only'; |
|
30 | - const REPLACE_ONLY = 'replace-only'; |
|
31 | - const MERGE_ONLY = 'merge-only'; |
|
27 | + const MERGE = 'merge'; |
|
28 | + const REPLACE = 'replace'; |
|
29 | + const ADD_ONLY = 'add-only'; |
|
30 | + const REPLACE_ONLY = 'replace-only'; |
|
31 | + const MERGE_ONLY = 'merge-only'; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Check whether the Findable can handle an individual criterion. |
|
35 | - * |
|
36 | - * @since 0.1.0 |
|
37 | - * |
|
38 | - * @param mixed $criterion Criterion to check. |
|
39 | - * |
|
40 | - * @return bool Whether the Findable can handle the criterion. |
|
41 | - */ |
|
42 | - public function canHandle($criterion): bool; |
|
33 | + /** |
|
34 | + * Check whether the Findable can handle an individual criterion. |
|
35 | + * |
|
36 | + * @since 0.1.0 |
|
37 | + * |
|
38 | + * @param mixed $criterion Criterion to check. |
|
39 | + * |
|
40 | + * @return bool Whether the Findable can handle the criterion. |
|
41 | + */ |
|
42 | + public function canHandle($criterion): bool; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Render the view. |
|
46 | - * |
|
47 | - * @since 0.1.0 |
|
48 | - * |
|
49 | - * @param array $context Optional. The context in which to render the view. |
|
50 | - * |
|
51 | - * @return string Rendered HTML. |
|
52 | - */ |
|
53 | - public function render(array $context = []): string; |
|
44 | + /** |
|
45 | + * Render the view. |
|
46 | + * |
|
47 | + * @since 0.1.0 |
|
48 | + * |
|
49 | + * @param array $context Optional. The context in which to render the view. |
|
50 | + * |
|
51 | + * @return string Rendered HTML. |
|
52 | + */ |
|
53 | + public function render(array $context = []): string; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Render a partial view (or section) for a given URI. |
|
57 | - * |
|
58 | - * @since 0.2.0 |
|
59 | - * |
|
60 | - * @param string $view View identifier to create a view for. |
|
61 | - * @param array $context Optional. The context in which to render the view. |
|
62 | - * @param string|null $type Type of view to create. |
|
63 | - * |
|
64 | - * @return string Rendered HTML content. |
|
65 | - */ |
|
66 | - public function section(string $view, array $context = [], $type = null): string; |
|
55 | + /** |
|
56 | + * Render a partial view (or section) for a given URI. |
|
57 | + * |
|
58 | + * @since 0.2.0 |
|
59 | + * |
|
60 | + * @param string $view View identifier to create a view for. |
|
61 | + * @param array $context Optional. The context in which to render the view. |
|
62 | + * @param string|null $type Type of view to create. |
|
63 | + * |
|
64 | + * @return string Rendered HTML content. |
|
65 | + */ |
|
66 | + public function section(string $view, array $context = [], $type = null): string; |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get the entire array of contextual data. |
|
70 | - * |
|
71 | - * @since 0.4.0 |
|
72 | - * |
|
73 | - * @return array Array of contextual data. |
|
74 | - */ |
|
75 | - public function getContext(): array; |
|
68 | + /** |
|
69 | + * Get the entire array of contextual data. |
|
70 | + * |
|
71 | + * @since 0.4.0 |
|
72 | + * |
|
73 | + * @return array Array of contextual data. |
|
74 | + */ |
|
75 | + public function getContext(): array; |
|
76 | 76 | |
77 | - /** |
|
78 | - * Add information to the context. |
|
79 | - * |
|
80 | - * @param string $key Context key to add. |
|
81 | - * @param mixed $value Value to add under the given key. |
|
82 | - * @param string $behavior Behavior to use for adapting the context. |
|
83 | - * @return View |
|
84 | - */ |
|
85 | - public function addToContext(string $key, $value, string $behavior): View; |
|
77 | + /** |
|
78 | + * Add information to the context. |
|
79 | + * |
|
80 | + * @param string $key Context key to add. |
|
81 | + * @param mixed $value Value to add under the given key. |
|
82 | + * @param string $behavior Behavior to use for adapting the context. |
|
83 | + * @return View |
|
84 | + */ |
|
85 | + public function addToContext(string $key, $value, string $behavior): View; |
|
86 | 86 | |
87 | - /** |
|
88 | - * Associate a view builder with this view. |
|
89 | - * |
|
90 | - * @since 0.2.0 |
|
91 | - * |
|
92 | - * @param ViewBuilder $builder |
|
93 | - * |
|
94 | - * @return View |
|
95 | - */ |
|
96 | - public function setBuilder(ViewBuilder $builder): View; |
|
87 | + /** |
|
88 | + * Associate a view builder with this view. |
|
89 | + * |
|
90 | + * @since 0.2.0 |
|
91 | + * |
|
92 | + * @param ViewBuilder $builder |
|
93 | + * |
|
94 | + * @return View |
|
95 | + */ |
|
96 | + public function setBuilder(ViewBuilder $builder): View; |
|
97 | 97 | } |
@@ -28,184 +28,184 @@ |
||
28 | 28 | class FilesystemLocation implements Location |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * Path that this location points to. |
|
33 | - * |
|
34 | - * @since 0.1.0 |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $path; |
|
39 | - |
|
40 | - /** |
|
41 | - * Extensions that this location can accept. |
|
42 | - * |
|
43 | - * @since 0.1.0 |
|
44 | - * |
|
45 | - * @var Extensions |
|
46 | - */ |
|
47 | - protected $extensions; |
|
48 | - |
|
49 | - /** |
|
50 | - * Instantiate a FilesystemLocation object. |
|
51 | - * |
|
52 | - * @since 0.1.0 |
|
53 | - * |
|
54 | - * @param string $path Path that this location points to. |
|
55 | - * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept. |
|
56 | - */ |
|
57 | - public function __construct(string $path, $extensions = null) |
|
58 | - { |
|
59 | - $this->path = $path; |
|
60 | - $this->extensions = $this->validateExtensions($extensions); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Get the first URI that matches the given criteria. |
|
65 | - * |
|
66 | - * @since 0.1.0 |
|
67 | - * |
|
68 | - * @param array $criteria Criteria to match. |
|
69 | - * |
|
70 | - * @return string|false URI that matches the criteria or false if none found. |
|
71 | - */ |
|
72 | - public function getURI(array $criteria) |
|
73 | - { |
|
74 | - $uris = $this->getURIs($criteria); |
|
75 | - |
|
76 | - return $uris->count() > 0 |
|
77 | - ? $uris->first() |
|
78 | - : false; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Get all URIs that match the given criteria. |
|
83 | - * |
|
84 | - * @since 0.1.1 |
|
85 | - * |
|
86 | - * @param array $criteria Criteria to match. |
|
87 | - * |
|
88 | - * @return URIs URIs that match the criteria or an empty collection if none found. |
|
89 | - */ |
|
90 | - public function getURIs(array $criteria): URIs |
|
91 | - { |
|
92 | - $uris = new URIs(); |
|
93 | - |
|
94 | - foreach ($this->extensions as $extension) { |
|
95 | - $finder = new Finder(); |
|
96 | - |
|
97 | - try { |
|
98 | - $finder->files() |
|
99 | - ->name($this->getNamePattern($criteria, $extension)) |
|
100 | - ->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
101 | - foreach ($finder as $file) { |
|
102 | - /** @var SplFileInfo $file */ |
|
103 | - $uris->add($file->getPathname()); |
|
104 | - } |
|
105 | - } catch (Exception $exception) { |
|
106 | - // Fail silently; |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - return $uris; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get the name pattern to pass to the file finder. |
|
115 | - * |
|
116 | - * @since 0.1.3 |
|
117 | - * |
|
118 | - * @param array $criteria Criteria to match. |
|
119 | - * @param string $extension Extension to match. |
|
120 | - * |
|
121 | - * @return string Name pattern to pass to the file finder. |
|
122 | - */ |
|
123 | - protected function getNamePattern(array $criteria, string $extension): string |
|
124 | - { |
|
125 | - $names = []; |
|
126 | - |
|
127 | - $names[] = array_map(function ($criterion) use ($extension) { |
|
128 | - $uriExtension = URIHelper::containsExtension($criterion); |
|
129 | - if (! empty($extension)) { |
|
130 | - $extension = ltrim($extension, '.'); |
|
131 | - |
|
132 | - if ($uriExtension === $extension) { |
|
133 | - $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
134 | - } |
|
135 | - } else { |
|
136 | - $extension = URIHelper::containsExtension($criterion); |
|
137 | - if (!empty($extension)) { |
|
138 | - $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
143 | - |
|
144 | - return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
145 | - ? "^{$criterion}(?:\..*?)$" |
|
146 | - : "^{$criterion}\.{$extension}$"; |
|
147 | - }, $criteria)[0]; |
|
148 | - |
|
149 | - return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Get the path pattern to pass to the file finder. |
|
154 | - * |
|
155 | - * @since 0.4.7 |
|
156 | - * |
|
157 | - * @param string $relativePath Relative path that was included in the |
|
158 | - * criterion. |
|
159 | - * @return string Path pattern to pass to the file finder. |
|
160 | - */ |
|
161 | - protected function getPathPattern(string $relativePath): string |
|
162 | - { |
|
163 | - if (empty($relativePath)) { |
|
164 | - return $this->path; |
|
165 | - } |
|
166 | - |
|
167 | - return rtrim($this->path, '/') . '/' . ltrim($relativePath, '/'); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Get the relative path that is included in the criterion. |
|
172 | - * |
|
173 | - * @since 0.4.7 |
|
174 | - * |
|
175 | - * @param array $criteria Criteria to extract the relative path from. |
|
176 | - * @return string Relative path included in the criterion. |
|
177 | - */ |
|
178 | - protected function getRelativePath($criteria): string |
|
179 | - { |
|
180 | - $criterion = current($criteria); |
|
181 | - $components = explode('/', $criterion); |
|
182 | - |
|
183 | - if (count($components) < 2) { |
|
184 | - return ''; |
|
185 | - } |
|
186 | - |
|
187 | - return implode('/', array_slice($components, 0, -1)); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Validate the extensions and return a collection. |
|
192 | - * |
|
193 | - * @since 0.1.1 |
|
194 | - * |
|
195 | - * @param Extensions|array|string|null $extensions Extensions to validate. |
|
196 | - * |
|
197 | - * @return Extensions Validated extensions collection. |
|
198 | - */ |
|
199 | - protected function validateExtensions($extensions): Extensions |
|
200 | - { |
|
201 | - if (empty($extensions)) { |
|
202 | - $extensions = new Extensions(['']); |
|
203 | - } |
|
204 | - |
|
205 | - if (! $extensions instanceof Extensions) { |
|
206 | - $extensions = new Extensions((array)$extensions); |
|
207 | - } |
|
208 | - |
|
209 | - return $extensions; |
|
210 | - } |
|
31 | + /** |
|
32 | + * Path that this location points to. |
|
33 | + * |
|
34 | + * @since 0.1.0 |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $path; |
|
39 | + |
|
40 | + /** |
|
41 | + * Extensions that this location can accept. |
|
42 | + * |
|
43 | + * @since 0.1.0 |
|
44 | + * |
|
45 | + * @var Extensions |
|
46 | + */ |
|
47 | + protected $extensions; |
|
48 | + |
|
49 | + /** |
|
50 | + * Instantiate a FilesystemLocation object. |
|
51 | + * |
|
52 | + * @since 0.1.0 |
|
53 | + * |
|
54 | + * @param string $path Path that this location points to. |
|
55 | + * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept. |
|
56 | + */ |
|
57 | + public function __construct(string $path, $extensions = null) |
|
58 | + { |
|
59 | + $this->path = $path; |
|
60 | + $this->extensions = $this->validateExtensions($extensions); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Get the first URI that matches the given criteria. |
|
65 | + * |
|
66 | + * @since 0.1.0 |
|
67 | + * |
|
68 | + * @param array $criteria Criteria to match. |
|
69 | + * |
|
70 | + * @return string|false URI that matches the criteria or false if none found. |
|
71 | + */ |
|
72 | + public function getURI(array $criteria) |
|
73 | + { |
|
74 | + $uris = $this->getURIs($criteria); |
|
75 | + |
|
76 | + return $uris->count() > 0 |
|
77 | + ? $uris->first() |
|
78 | + : false; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Get all URIs that match the given criteria. |
|
83 | + * |
|
84 | + * @since 0.1.1 |
|
85 | + * |
|
86 | + * @param array $criteria Criteria to match. |
|
87 | + * |
|
88 | + * @return URIs URIs that match the criteria or an empty collection if none found. |
|
89 | + */ |
|
90 | + public function getURIs(array $criteria): URIs |
|
91 | + { |
|
92 | + $uris = new URIs(); |
|
93 | + |
|
94 | + foreach ($this->extensions as $extension) { |
|
95 | + $finder = new Finder(); |
|
96 | + |
|
97 | + try { |
|
98 | + $finder->files() |
|
99 | + ->name($this->getNamePattern($criteria, $extension)) |
|
100 | + ->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
101 | + foreach ($finder as $file) { |
|
102 | + /** @var SplFileInfo $file */ |
|
103 | + $uris->add($file->getPathname()); |
|
104 | + } |
|
105 | + } catch (Exception $exception) { |
|
106 | + // Fail silently; |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + return $uris; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get the name pattern to pass to the file finder. |
|
115 | + * |
|
116 | + * @since 0.1.3 |
|
117 | + * |
|
118 | + * @param array $criteria Criteria to match. |
|
119 | + * @param string $extension Extension to match. |
|
120 | + * |
|
121 | + * @return string Name pattern to pass to the file finder. |
|
122 | + */ |
|
123 | + protected function getNamePattern(array $criteria, string $extension): string |
|
124 | + { |
|
125 | + $names = []; |
|
126 | + |
|
127 | + $names[] = array_map(function ($criterion) use ($extension) { |
|
128 | + $uriExtension = URIHelper::containsExtension($criterion); |
|
129 | + if (! empty($extension)) { |
|
130 | + $extension = ltrim($extension, '.'); |
|
131 | + |
|
132 | + if ($uriExtension === $extension) { |
|
133 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
134 | + } |
|
135 | + } else { |
|
136 | + $extension = URIHelper::containsExtension($criterion); |
|
137 | + if (!empty($extension)) { |
|
138 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
143 | + |
|
144 | + return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
145 | + ? "^{$criterion}(?:\..*?)$" |
|
146 | + : "^{$criterion}\.{$extension}$"; |
|
147 | + }, $criteria)[0]; |
|
148 | + |
|
149 | + return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Get the path pattern to pass to the file finder. |
|
154 | + * |
|
155 | + * @since 0.4.7 |
|
156 | + * |
|
157 | + * @param string $relativePath Relative path that was included in the |
|
158 | + * criterion. |
|
159 | + * @return string Path pattern to pass to the file finder. |
|
160 | + */ |
|
161 | + protected function getPathPattern(string $relativePath): string |
|
162 | + { |
|
163 | + if (empty($relativePath)) { |
|
164 | + return $this->path; |
|
165 | + } |
|
166 | + |
|
167 | + return rtrim($this->path, '/') . '/' . ltrim($relativePath, '/'); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Get the relative path that is included in the criterion. |
|
172 | + * |
|
173 | + * @since 0.4.7 |
|
174 | + * |
|
175 | + * @param array $criteria Criteria to extract the relative path from. |
|
176 | + * @return string Relative path included in the criterion. |
|
177 | + */ |
|
178 | + protected function getRelativePath($criteria): string |
|
179 | + { |
|
180 | + $criterion = current($criteria); |
|
181 | + $components = explode('/', $criterion); |
|
182 | + |
|
183 | + if (count($components) < 2) { |
|
184 | + return ''; |
|
185 | + } |
|
186 | + |
|
187 | + return implode('/', array_slice($components, 0, -1)); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Validate the extensions and return a collection. |
|
192 | + * |
|
193 | + * @since 0.1.1 |
|
194 | + * |
|
195 | + * @param Extensions|array|string|null $extensions Extensions to validate. |
|
196 | + * |
|
197 | + * @return Extensions Validated extensions collection. |
|
198 | + */ |
|
199 | + protected function validateExtensions($extensions): Extensions |
|
200 | + { |
|
201 | + if (empty($extensions)) { |
|
202 | + $extensions = new Extensions(['']); |
|
203 | + } |
|
204 | + |
|
205 | + if (! $extensions instanceof Extensions) { |
|
206 | + $extensions = new Extensions((array)$extensions); |
|
207 | + } |
|
208 | + |
|
209 | + return $extensions; |
|
210 | + } |
|
211 | 211 | } |
@@ -124,9 +124,9 @@ discard block |
||
124 | 124 | { |
125 | 125 | $names = []; |
126 | 126 | |
127 | - $names[] = array_map(function ($criterion) use ($extension) { |
|
127 | + $names[] = array_map(function($criterion) use ($extension) { |
|
128 | 128 | $uriExtension = URIHelper::containsExtension($criterion); |
129 | - if (! empty($extension)) { |
|
129 | + if ( ! empty($extension)) { |
|
130 | 130 | $extension = ltrim($extension, '.'); |
131 | 131 | |
132 | 132 | if ($uriExtension === $extension) { |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | } |
135 | 135 | } else { |
136 | 136 | $extension = URIHelper::containsExtension($criterion); |
137 | - if (!empty($extension)) { |
|
137 | + if ( ! empty($extension)) { |
|
138 | 138 | $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
139 | 139 | } |
140 | 140 | } |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | : "^{$criterion}\.{$extension}$"; |
147 | 147 | }, $criteria)[0]; |
148 | 148 | |
149 | - return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
149 | + return chr(1).implode('|', array_unique($names)).chr(1); |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | /** |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | return $this->path; |
165 | 165 | } |
166 | 166 | |
167 | - return rtrim($this->path, '/') . '/' . ltrim($relativePath, '/'); |
|
167 | + return rtrim($this->path, '/').'/'.ltrim($relativePath, '/'); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -202,8 +202,8 @@ discard block |
||
202 | 202 | $extensions = new Extensions(['']); |
203 | 203 | } |
204 | 204 | |
205 | - if (! $extensions instanceof Extensions) { |
|
206 | - $extensions = new Extensions((array)$extensions); |
|
205 | + if ( ! $extensions instanceof Extensions) { |
|
206 | + $extensions = new Extensions((array) $extensions); |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | return $extensions; |