@@ -15,35 +15,35 @@ |
||
| 15 | 15 | * Engine finder default configuration. |
| 16 | 16 | */ |
| 17 | 17 | $engineFinder = [ |
| 18 | - // Class to use for instantiating the EngineFinder implementation. |
|
| 19 | - Engine\EngineFinder::CLASS_NAME_KEY => Engine\BaseEngineFinder::class, |
|
| 20 | - // Engine implementations to register with the EngineFinder. |
|
| 21 | - Engine\EngineFinder::ENGINES_KEY => [ |
|
| 22 | - 'PHPEngine' => Engine\PHPEngine::class, |
|
| 23 | - ], |
|
| 24 | - // Null object implementation to use with the EngineFinder. |
|
| 25 | - Engine\EngineFinder::NULL_OBJECT => Engine\NullEngine::class, |
|
| 18 | + // Class to use for instantiating the EngineFinder implementation. |
|
| 19 | + Engine\EngineFinder::CLASS_NAME_KEY => Engine\BaseEngineFinder::class, |
|
| 20 | + // Engine implementations to register with the EngineFinder. |
|
| 21 | + Engine\EngineFinder::ENGINES_KEY => [ |
|
| 22 | + 'PHPEngine' => Engine\PHPEngine::class, |
|
| 23 | + ], |
|
| 24 | + // Null object implementation to use with the EngineFinder. |
|
| 25 | + Engine\EngineFinder::NULL_OBJECT => Engine\NullEngine::class, |
|
| 26 | 26 | ]; |
| 27 | 27 | |
| 28 | 28 | /* |
| 29 | 29 | * View finder default configuration. |
| 30 | 30 | */ |
| 31 | 31 | $viewFinder = [ |
| 32 | - // Class to use for instantiating the ViewFinder implementation. |
|
| 33 | - View\ViewFinder::CLASS_NAME_KEY => View\BaseViewFinder::class, |
|
| 34 | - // View implementations to register with the ViewFinder. |
|
| 35 | - View\ViewFinder::VIEWS_KEY => [ |
|
| 36 | - 'BaseView' => View\BaseView::class, |
|
| 37 | - ], |
|
| 38 | - // Null object implementation to use with the ViewFinder. |
|
| 39 | - View\ViewFinder::NULL_OBJECT => View\NullView::class, |
|
| 32 | + // Class to use for instantiating the ViewFinder implementation. |
|
| 33 | + View\ViewFinder::CLASS_NAME_KEY => View\BaseViewFinder::class, |
|
| 34 | + // View implementations to register with the ViewFinder. |
|
| 35 | + View\ViewFinder::VIEWS_KEY => [ |
|
| 36 | + 'BaseView' => View\BaseView::class, |
|
| 37 | + ], |
|
| 38 | + // Null object implementation to use with the ViewFinder. |
|
| 39 | + View\ViewFinder::NULL_OBJECT => View\NullView::class, |
|
| 40 | 40 | ]; |
| 41 | 41 | |
| 42 | 42 | return [ |
| 43 | - 'BrightNucleus' => [ |
|
| 44 | - 'View' => [ |
|
| 45 | - 'EngineFinder' => $engineFinder, |
|
| 46 | - 'ViewFinder' => $viewFinder, |
|
| 47 | - ], |
|
| 48 | - ], |
|
| 43 | + 'BrightNucleus' => [ |
|
| 44 | + 'View' => [ |
|
| 45 | + 'EngineFinder' => $engineFinder, |
|
| 46 | + 'ViewFinder' => $viewFinder, |
|
| 47 | + ], |
|
| 48 | + ], |
|
| 49 | 49 | ]; |
@@ -46,7 +46,7 @@ |
||
| 46 | 46 | * @param string $uri URI to render. |
| 47 | 47 | * @param array $context Context in which to render. |
| 48 | 48 | * |
| 49 | - * @return callable Rendering callback. |
|
| 49 | + * @return \Closure Rendering callback. |
|
| 50 | 50 | */ |
| 51 | 51 | public function getRenderCallback(string $uri, array $context = []): callable |
| 52 | 52 | { |
@@ -24,32 +24,32 @@ |
||
| 24 | 24 | class NullEngine implements Engine, NullFindable |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Check whether the Findable can handle an individual criterion. |
|
| 29 | - * |
|
| 30 | - * @since 0.1.0 |
|
| 31 | - * |
|
| 32 | - * @param mixed $criterion Criterion to check. |
|
| 33 | - * |
|
| 34 | - * @return bool Whether the Findable can handle the criterion. |
|
| 35 | - */ |
|
| 36 | - public function canHandle($criterion): bool |
|
| 37 | - { |
|
| 38 | - return true; |
|
| 39 | - } |
|
| 27 | + /** |
|
| 28 | + * Check whether the Findable can handle an individual criterion. |
|
| 29 | + * |
|
| 30 | + * @since 0.1.0 |
|
| 31 | + * |
|
| 32 | + * @param mixed $criterion Criterion to check. |
|
| 33 | + * |
|
| 34 | + * @return bool Whether the Findable can handle the criterion. |
|
| 35 | + */ |
|
| 36 | + public function canHandle($criterion): bool |
|
| 37 | + { |
|
| 38 | + return true; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get the rendering callback for a given URI. |
|
| 43 | - * |
|
| 44 | - * @since 0.1.0 |
|
| 45 | - * |
|
| 46 | - * @param string $uri URI to render. |
|
| 47 | - * @param array $context Context in which to render. |
|
| 48 | - * |
|
| 49 | - * @return callable Rendering callback. |
|
| 50 | - */ |
|
| 51 | - public function getRenderCallback(string $uri, array $context = []): callable |
|
| 52 | - { |
|
| 53 | - return function () { return ''; }; |
|
| 54 | - } |
|
| 41 | + /** |
|
| 42 | + * Get the rendering callback for a given URI. |
|
| 43 | + * |
|
| 44 | + * @since 0.1.0 |
|
| 45 | + * |
|
| 46 | + * @param string $uri URI to render. |
|
| 47 | + * @param array $context Context in which to render. |
|
| 48 | + * |
|
| 49 | + * @return callable Rendering callback. |
|
| 50 | + */ |
|
| 51 | + public function getRenderCallback(string $uri, array $context = []): callable |
|
| 52 | + { |
|
| 53 | + return function () { return ''; }; |
|
| 54 | + } |
|
| 55 | 55 | } |
@@ -50,6 +50,6 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | public function getRenderCallback(string $uri, array $context = []): callable |
| 52 | 52 | { |
| 53 | - return function () { return ''; }; |
|
| 53 | + return function() { return ''; }; |
|
| 54 | 54 | } |
| 55 | 55 | } |
@@ -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 | } |
@@ -28,158 +28,158 @@ |
||
| 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()); |
|
| 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.1.3 |
|
| 156 | - * |
|
| 157 | - * @return string Path pattern to pass to the file finder. |
|
| 158 | - */ |
|
| 159 | - protected function getPathPattern(): string |
|
| 160 | - { |
|
| 161 | - return $this->path; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Validate the extensions and return a collection. |
|
| 166 | - * |
|
| 167 | - * @since 0.1.1 |
|
| 168 | - * |
|
| 169 | - * @param Extensions|array|string|null $extensions Extensions to validate. |
|
| 170 | - * |
|
| 171 | - * @return Extensions Validated extensions collection. |
|
| 172 | - */ |
|
| 173 | - protected function validateExtensions($extensions): Extensions |
|
| 174 | - { |
|
| 175 | - if (empty($extensions)) { |
|
| 176 | - $extensions = new Extensions(['']); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - if (! $extensions instanceof Extensions) { |
|
| 180 | - $extensions = new Extensions((array)$extensions); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return $extensions; |
|
| 184 | - } |
|
| 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()); |
|
| 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.1.3 |
|
| 156 | + * |
|
| 157 | + * @return string Path pattern to pass to the file finder. |
|
| 158 | + */ |
|
| 159 | + protected function getPathPattern(): string |
|
| 160 | + { |
|
| 161 | + return $this->path; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Validate the extensions and return a collection. |
|
| 166 | + * |
|
| 167 | + * @since 0.1.1 |
|
| 168 | + * |
|
| 169 | + * @param Extensions|array|string|null $extensions Extensions to validate. |
|
| 170 | + * |
|
| 171 | + * @return Extensions Validated extensions collection. |
|
| 172 | + */ |
|
| 173 | + protected function validateExtensions($extensions): Extensions |
|
| 174 | + { |
|
| 175 | + if (empty($extensions)) { |
|
| 176 | + $extensions = new Extensions(['']); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + if (! $extensions instanceof Extensions) { |
|
| 180 | + $extensions = new Extensions((array)$extensions); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return $extensions; |
|
| 184 | + } |
|
| 185 | 185 | } |
@@ -124,18 +124,18 @@ 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) { |
| 133 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
| 133 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
| 134 | 134 | } |
| 135 | 135 | } else { |
| 136 | 136 | $extension = URIHelper::containsExtension($criterion); |
| 137 | - if (!empty($extension)) { |
|
| 138 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
| 137 | + if ( ! empty($extension)) { |
|
| 138 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | |
@@ -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 | /** |
@@ -176,8 +176,8 @@ discard block |
||
| 176 | 176 | $extensions = new Extensions(['']); |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - if (! $extensions instanceof Extensions) { |
|
| 180 | - $extensions = new Extensions((array)$extensions); |
|
| 179 | + if ( ! $extensions instanceof Extensions) { |
|
| 180 | + $extensions = new Extensions((array) $extensions); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | return $extensions; |
@@ -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) { |