Passed
Push — master ( 4ac39c...ddccf7 )
by Alain
03:15
created
src/View/ViewBuilder.php 2 patches
Indentation   +213 added lines, -213 removed lines patch added patch discarded remove patch
@@ -34,241 +34,241 @@
 block discarded – undo
34 34
 class ViewBuilder
35 35
 {
36 36
 
37
-    use ConfigTrait;
37
+	use ConfigTrait;
38 38
 
39
-    const ENGINE_FINDER_KEY = 'EngineFinder';
40
-    const VIEW_FINDER_KEY   = 'ViewFinder';
39
+	const ENGINE_FINDER_KEY = 'EngineFinder';
40
+	const VIEW_FINDER_KEY   = 'ViewFinder';
41 41
 
42
-    /**
43
-     * ViewFinder instance.
44
-     *
45
-     * @since 0.1.0
46
-     *
47
-     * @var ViewFinderInterface
48
-     */
49
-    protected $viewFinder;
42
+	/**
43
+	 * ViewFinder instance.
44
+	 *
45
+	 * @since 0.1.0
46
+	 *
47
+	 * @var ViewFinderInterface
48
+	 */
49
+	protected $viewFinder;
50 50
 
51
-    /**
52
-     * EngineFinder instance.
53
-     *
54
-     * @since 0.1.0
55
-     *
56
-     * @var EngineFinderInterface
57
-     */
58
-    protected $engineFinder;
51
+	/**
52
+	 * EngineFinder instance.
53
+	 *
54
+	 * @since 0.1.0
55
+	 *
56
+	 * @var EngineFinderInterface
57
+	 */
58
+	protected $engineFinder;
59 59
 
60
-    /**
61
-     * Locations to scan for views.
62
-     *
63
-     * @since 0.1.0
64
-     *
65
-     * @var LocationCollection
66
-     */
67
-    protected $locations;
60
+	/**
61
+	 * Locations to scan for views.
62
+	 *
63
+	 * @since 0.1.0
64
+	 *
65
+	 * @var LocationCollection
66
+	 */
67
+	protected $locations;
68 68
 
69
-    /**
70
-     * Instantiate a ViewBuilder object.
71
-     *
72
-     * @since 0.1.0
73
-     *
74
-     * @param ConfigInterface            $config       Configuration settings.
75
-     * @param ViewFinderInterface|null   $viewFinder   ViewFinder instance.
76
-     * @param EngineFinderInterface|null $engineFinder EngineFinder instance.
77
-     *
78
-     * @throws FailedToProcessConfigException If the config could not be processed.
79
-     */
80
-    public function __construct(
81
-        ConfigInterface $config,
82
-        ViewFinderInterface $viewFinder = null,
83
-        EngineFinderInterface $engineFinder = null
84
-    ) {
85
-        $this->processConfig($config);
86
-        $this->viewFinder   = $viewFinder;
87
-        $this->engineFinder = $engineFinder;
88
-        $this->locations    = new LocationCollection();
89
-    }
69
+	/**
70
+	 * Instantiate a ViewBuilder object.
71
+	 *
72
+	 * @since 0.1.0
73
+	 *
74
+	 * @param ConfigInterface            $config       Configuration settings.
75
+	 * @param ViewFinderInterface|null   $viewFinder   ViewFinder instance.
76
+	 * @param EngineFinderInterface|null $engineFinder EngineFinder instance.
77
+	 *
78
+	 * @throws FailedToProcessConfigException If the config could not be processed.
79
+	 */
80
+	public function __construct(
81
+		ConfigInterface $config,
82
+		ViewFinderInterface $viewFinder = null,
83
+		EngineFinderInterface $engineFinder = null
84
+	) {
85
+		$this->processConfig($config);
86
+		$this->viewFinder   = $viewFinder;
87
+		$this->engineFinder = $engineFinder;
88
+		$this->locations    = new LocationCollection();
89
+	}
90 90
 
91
-    /**
92
-     * Create a new view for a given URI.
93
-     *
94
-     * @since 0.1.0
95
-     *
96
-     * @param string $view View identifier to create a view for.
97
-     * @param mixed  $type Type of view to create.
98
-     *
99
-     * @return ViewInterface Instance of the requested view.
100
-     */
101
-    public function create($view, $type = null)
102
-    {
103
-        $uri    = $this->scanLocations([$view]);
104
-        $engine = $this->getEngine($uri);
91
+	/**
92
+	 * Create a new view for a given URI.
93
+	 *
94
+	 * @since 0.1.0
95
+	 *
96
+	 * @param string $view View identifier to create a view for.
97
+	 * @param mixed  $type Type of view to create.
98
+	 *
99
+	 * @return ViewInterface Instance of the requested view.
100
+	 */
101
+	public function create($view, $type = null)
102
+	{
103
+		$uri    = $this->scanLocations([$view]);
104
+		$engine = $this->getEngine($uri);
105 105
 
106
-        return $uri
107
-            ? $this->getView($uri, $engine, $type)
108
-            : $this->getViewFinder()->getNullObject();
109
-    }
106
+		return $uri
107
+			? $this->getView($uri, $engine, $type)
108
+			: $this->getViewFinder()->getNullObject();
109
+	}
110 110
 
111
-    /**
112
-     * Get an Engine that can deal with the given URI.
113
-     *
114
-     * @since 0.1.0
115
-     *
116
-     * @param string|false $uri URI to get an engine for.
117
-     *
118
-     * @return EngineInterface Instance of an engine that can deal with the given URI.
119
-     */
120
-    public function getEngine($uri)
121
-    {
122
-        return $this->getEngineFinder()->find([$uri]);
123
-    }
111
+	/**
112
+	 * Get an Engine that can deal with the given URI.
113
+	 *
114
+	 * @since 0.1.0
115
+	 *
116
+	 * @param string|false $uri URI to get an engine for.
117
+	 *
118
+	 * @return EngineInterface Instance of an engine that can deal with the given URI.
119
+	 */
120
+	public function getEngine($uri)
121
+	{
122
+		return $this->getEngineFinder()->find([$uri]);
123
+	}
124 124
 
125
-    /**
126
-     * Get a view for a given URI, engine and type.
127
-     *
128
-     * @since 0.1.0
129
-     *
130
-     * @param string          $uri    URI to get a view for.
131
-     * @param EngineInterface $engine Engine to use for the view.
132
-     * @param mixed           $type   Type of view to get.
133
-     *
134
-     * @return ViewInterface View that matches the given requirements.
135
-     */
136
-    public function getView($uri, EngineInterface $engine, $type = null)
137
-    {
138
-        if (null === $type) {
139
-            return $this->getViewFinder()->find([$uri], $engine);
140
-        }
125
+	/**
126
+	 * Get a view for a given URI, engine and type.
127
+	 *
128
+	 * @since 0.1.0
129
+	 *
130
+	 * @param string          $uri    URI to get a view for.
131
+	 * @param EngineInterface $engine Engine to use for the view.
132
+	 * @param mixed           $type   Type of view to get.
133
+	 *
134
+	 * @return ViewInterface View that matches the given requirements.
135
+	 */
136
+	public function getView($uri, EngineInterface $engine, $type = null)
137
+	{
138
+		if (null === $type) {
139
+			return $this->getViewFinder()->find([$uri], $engine);
140
+		}
141 141
 
142
-        return $this->resolveType($type, $uri, $engine);
143
-    }
142
+		return $this->resolveType($type, $uri, $engine);
143
+	}
144 144
 
145
-    /**
146
-     * Get the ViewFinder instance.
147
-     *
148
-     * @since 0.1.0
149
-     *
150
-     * @return ViewFinderInterface Instance of a ViewFinder.
151
-     */
152
-    public function getViewFinder()
153
-    {
154
-        return $this->getFinder($this->viewFinder, static::VIEW_FINDER_KEY);
155
-    }
145
+	/**
146
+	 * Get the ViewFinder instance.
147
+	 *
148
+	 * @since 0.1.0
149
+	 *
150
+	 * @return ViewFinderInterface Instance of a ViewFinder.
151
+	 */
152
+	public function getViewFinder()
153
+	{
154
+		return $this->getFinder($this->viewFinder, static::VIEW_FINDER_KEY);
155
+	}
156 156
 
157
-    /**
158
-     * Get the EngineFinder instance.
159
-     *
160
-     * @since 0.1.0
161
-     *
162
-     * @return EngineFinderInterface Instance of a EngineFinder.
163
-     */
164
-    public function getEngineFinder()
165
-    {
166
-        return $this->getFinder($this->engineFinder, static::ENGINE_FINDER_KEY);
167
-    }
157
+	/**
158
+	 * Get the EngineFinder instance.
159
+	 *
160
+	 * @since 0.1.0
161
+	 *
162
+	 * @return EngineFinderInterface Instance of a EngineFinder.
163
+	 */
164
+	public function getEngineFinder()
165
+	{
166
+		return $this->getFinder($this->engineFinder, static::ENGINE_FINDER_KEY);
167
+	}
168 168
 
169
-    /**
170
-     * Add a location to scan with the ViewFinder.
171
-     *
172
-     * @since 0.1.0
173
-     *
174
-     * @param LocationInterface $location Location to scan with the ViewFinder.
175
-     */
176
-    public function addLocation(LocationInterface $location)
177
-    {
178
-        $this->locations->add($location);
179
-    }
169
+	/**
170
+	 * Add a location to scan with the ViewFinder.
171
+	 *
172
+	 * @since 0.1.0
173
+	 *
174
+	 * @param LocationInterface $location Location to scan with the ViewFinder.
175
+	 */
176
+	public function addLocation(LocationInterface $location)
177
+	{
178
+		$this->locations->add($location);
179
+	}
180 180
 
181
-    /**
182
-     * Get the collection of locations registered with this ViewBuilder.
183
-     *
184
-     * @since 0.1.3
185
-     *
186
-     * @return LocationCollection Collection of locations.
187
-     */
188
-    public function getLocations()
189
-    {
190
-        return $this->locations;
191
-    }
181
+	/**
182
+	 * Get the collection of locations registered with this ViewBuilder.
183
+	 *
184
+	 * @since 0.1.3
185
+	 *
186
+	 * @return LocationCollection Collection of locations.
187
+	 */
188
+	public function getLocations()
189
+	{
190
+		return $this->locations;
191
+	}
192 192
 
193
-    /**
194
-     * Scan Locations for an URI that matches the specified criteria.
195
-     *
196
-     * @since 0.1.0
197
-     *
198
-     * @param array $criteria Criteria to match.
199
-     *
200
-     * @return string|false URI of the requested view, or false if not found.
201
-     */
202
-    public function scanLocations(array $criteria)
203
-    {
204
-        $uris = $this->locations->map(function ($location) use ($criteria) {
205
-            /** @var LocationInterface $location */
206
-            return $location->getURI($criteria);
207
-        })->filter(function ($uri) {
208
-            return false !== $uri;
209
-        });
193
+	/**
194
+	 * Scan Locations for an URI that matches the specified criteria.
195
+	 *
196
+	 * @since 0.1.0
197
+	 *
198
+	 * @param array $criteria Criteria to match.
199
+	 *
200
+	 * @return string|false URI of the requested view, or false if not found.
201
+	 */
202
+	public function scanLocations(array $criteria)
203
+	{
204
+		$uris = $this->locations->map(function ($location) use ($criteria) {
205
+			/** @var LocationInterface $location */
206
+			return $location->getURI($criteria);
207
+		})->filter(function ($uri) {
208
+			return false !== $uri;
209
+		});
210 210
 
211
-        return $uris->count() > 0 ? $uris->first() : false;
212
-    }
211
+		return $uris->count() > 0 ? $uris->first() : false;
212
+	}
213 213
 
214
-    /**
215
-     * Get a finder instance.
216
-     *
217
-     * @since 0.1.1
218
-     *
219
-     * @param mixed  $property Property to use.
220
-     * @param string $key      Configuration key to use.
221
-     *
222
-     * @return FinderInterface The requested finder instance.
223
-     */
224
-    protected function getFinder(&$property, $key)
225
-    {
226
-        if (null === $property) {
227
-            $engineFinderClass = $this->config->getKey($key, 'ClassName');
228
-            $property          = new $engineFinderClass($this->config->getSubConfig($key));
229
-        }
214
+	/**
215
+	 * Get a finder instance.
216
+	 *
217
+	 * @since 0.1.1
218
+	 *
219
+	 * @param mixed  $property Property to use.
220
+	 * @param string $key      Configuration key to use.
221
+	 *
222
+	 * @return FinderInterface The requested finder instance.
223
+	 */
224
+	protected function getFinder(&$property, $key)
225
+	{
226
+		if (null === $property) {
227
+			$engineFinderClass = $this->config->getKey($key, 'ClassName');
228
+			$property          = new $engineFinderClass($this->config->getSubConfig($key));
229
+		}
230 230
 
231
-        return $property;
232
-    }
231
+		return $property;
232
+	}
233 233
 
234
-    /**
235
-     * Resolve the view type.
236
-     *
237
-     * @since 0.1.0
238
-     *
239
-     * @param mixed                $type   Type of view that was requested.
240
-     * @param string               $uri    URI to get a view for.
241
-     * @param EngineInterface|null $engine Engine to use for the view.
242
-     *
243
-     * @return ViewInterface Resolved View object.
244
-     * @throws FailedToInstantiateViewException If the view type could not be resolved.
245
-     */
246
-    protected function resolveType($type, $uri, EngineInterface $engine = null)
247
-    {
248
-        $configKey = [static::VIEW_FINDER_KEY, 'Views', $type];
234
+	/**
235
+	 * Resolve the view type.
236
+	 *
237
+	 * @since 0.1.0
238
+	 *
239
+	 * @param mixed                $type   Type of view that was requested.
240
+	 * @param string               $uri    URI to get a view for.
241
+	 * @param EngineInterface|null $engine Engine to use for the view.
242
+	 *
243
+	 * @return ViewInterface Resolved View object.
244
+	 * @throws FailedToInstantiateViewException If the view type could not be resolved.
245
+	 */
246
+	protected function resolveType($type, $uri, EngineInterface $engine = null)
247
+	{
248
+		$configKey = [static::VIEW_FINDER_KEY, 'Views', $type];
249 249
 
250
-        if (is_string($type) && $this->config->hasKey($configKey)) {
251
-            $className = $this->config->getKey($configKey);
252
-            $type      = new $className($uri, $engine);
253
-        }
250
+		if (is_string($type) && $this->config->hasKey($configKey)) {
251
+			$className = $this->config->getKey($configKey);
252
+			$type      = new $className($uri, $engine);
253
+		}
254 254
 
255
-        if (is_string($type)) {
256
-            $type = new $type($uri, $engine);
257
-        }
255
+		if (is_string($type)) {
256
+			$type = new $type($uri, $engine);
257
+		}
258 258
 
259
-        if (is_callable($type)) {
260
-            $type = $type($uri, $engine);
261
-        }
259
+		if (is_callable($type)) {
260
+			$type = $type($uri, $engine);
261
+		}
262 262
 
263
-        if (! $type instanceof ViewInterface) {
264
-            throw new FailedToInstantiateViewException(
265
-                sprintf(
266
-                    _('Could not instantiate view "%s".'),
267
-                    serialize($type)
268
-                )
269
-            );
270
-        }
263
+		if (! $type instanceof ViewInterface) {
264
+			throw new FailedToInstantiateViewException(
265
+				sprintf(
266
+					_('Could not instantiate view "%s".'),
267
+					serialize($type)
268
+				)
269
+			);
270
+		}
271 271
 
272
-        return $type;
273
-    }
272
+		return $type;
273
+	}
274 274
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -201,10 +201,10 @@  discard block
 block discarded – undo
201 201
      */
202 202
     public function scanLocations(array $criteria)
203 203
     {
204
-        $uris = $this->locations->map(function ($location) use ($criteria) {
204
+        $uris = $this->locations->map(function($location) use ($criteria) {
205 205
             /** @var LocationInterface $location */
206 206
             return $location->getURI($criteria);
207
-        })->filter(function ($uri) {
207
+        })->filter(function($uri) {
208 208
             return false !== $uri;
209 209
         });
210 210
 
@@ -260,7 +260,7 @@  discard block
 block discarded – undo
260 260
             $type = $type($uri, $engine);
261 261
         }
262 262
 
263
-        if (! $type instanceof ViewInterface) {
263
+        if ( ! $type instanceof ViewInterface) {
264 264
             throw new FailedToInstantiateViewException(
265 265
                 sprintf(
266 266
                     _('Could not instantiate view "%s".'),
Please login to merge, or discard this patch.
src/View/Location/LocationInterface.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -22,25 +22,25 @@
 block discarded – undo
22 22
 interface LocationInterface
23 23
 {
24 24
 
25
-    /**
26
-     * Get the first URI that matches the given criteria.
27
-     *
28
-     * @since 0.1.0
29
-     *
30
-     * @param array $criteria Criteria to match.
31
-     *
32
-     * @return string|false URI that matches the criteria or false if none found.
33
-     */
34
-    public function getURI(array $criteria);
25
+	/**
26
+	 * Get the first URI that matches the given criteria.
27
+	 *
28
+	 * @since 0.1.0
29
+	 *
30
+	 * @param array $criteria Criteria to match.
31
+	 *
32
+	 * @return string|false URI that matches the criteria or false if none found.
33
+	 */
34
+	public function getURI(array $criteria);
35 35
 
36
-    /**
37
-     * Get all URIs that match the given criteria.
38
-     *
39
-     * @since 0.1.1
40
-     *
41
-     * @param array $criteria Criteria to match.
42
-     *
43
-     * @return URICollection Collection of URIs that matches the criteria or an empty collection if none found.
44
-     */
45
-    public function getURIs(array $criteria);
36
+	/**
37
+	 * Get all URIs that match the given criteria.
38
+	 *
39
+	 * @since 0.1.1
40
+	 *
41
+	 * @param array $criteria Criteria to match.
42
+	 *
43
+	 * @return URICollection Collection of URIs that matches the criteria or an empty collection if none found.
44
+	 */
45
+	public function getURIs(array $criteria);
46 46
 }
Please login to merge, or discard this patch.
src/View/Location/URICollection.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -25,19 +25,19 @@
 block discarded – undo
25 25
 class URICollection extends ArrayCollection
26 26
 {
27 27
 
28
-    /**
29
-     * Create a new URICollection from a Symfony Finder instance.
30
-     *
31
-     * @since 0.1.3
32
-     *
33
-     * @param Finder $finder The Finder instance to create the URI collection from.
34
-     *
35
-     * @return static New URICollection instance.
36
-     */
37
-    public static function fromFinder(Finder $finder)
38
-    {
39
-        $elements = array_keys(iterator_to_array($finder));
28
+	/**
29
+	 * Create a new URICollection from a Symfony Finder instance.
30
+	 *
31
+	 * @since 0.1.3
32
+	 *
33
+	 * @param Finder $finder The Finder instance to create the URI collection from.
34
+	 *
35
+	 * @return static New URICollection instance.
36
+	 */
37
+	public static function fromFinder(Finder $finder)
38
+	{
39
+		$elements = array_keys(iterator_to_array($finder));
40 40
 
41
-        return new static($elements);
42
-    }
41
+		return new static($elements);
42
+	}
43 43
 }
Please login to merge, or discard this patch.
src/View/Location/FilesystemLocation.php 2 patches
Indentation   +158 added lines, -158 removed lines patch added patch discarded remove patch
@@ -28,162 +28,162 @@
 block discarded – undo
28 28
 class FilesystemLocation implements LocationInterface
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 ExtensionCollection
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 ExtensionCollection|array|string|null $extensions Optional. Extensions that this location can accept.
56
-     */
57
-    public function __construct($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
-            ? $this->getURIs($criteria)->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 URICollection URIs that match the criteria or an empty collection if none found.
89
-     */
90
-    public function getURIs(array $criteria)
91
-    {
92
-        $uris = new URICollection();
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, $extension)
124
-    {
125
-        $names = [];
126
-
127
-        $names[] = array_map(function ($criterion) use ($extension) {
128
-            $criterion = URIHelper::getFilename($criterion);
129
-
130
-            return empty($extension) || URIHelper::hasExtension($criterion, $extension)
131
-                ? $criterion
132
-                : $criterion . $extension;
133
-        }, $criteria)[0];
134
-
135
-        return $this->arrayToRegexPattern(array_unique($names));
136
-    }
137
-
138
-    /**
139
-     * Get the path pattern to pass to the file finder.
140
-     *
141
-     * @since 0.1.3
142
-     *
143
-     * @return string Path pattern to pass to the file finder.
144
-     */
145
-    protected function getPathPattern()
146
-    {
147
-        return $this->path;
148
-    }
149
-
150
-    /**
151
-     * Get an array as a regular expression pattern string.
152
-     *
153
-     * @since 0.1.3
154
-     *
155
-     * @param array $array Array to generate the pattern for.
156
-     *
157
-     * @return string Generated regular expression pattern.
158
-     */
159
-    protected function arrayToRegexPattern(array $array)
160
-    {
161
-        $array = array_map(function ($entry) {
162
-            return preg_quote($entry);
163
-        }, $array);
164
-
165
-        return '/' . implode('|', $array) . '/';
166
-    }
167
-
168
-    /**
169
-     * Validate the extensions and return a collection.
170
-     *
171
-     * @since 0.1.1
172
-     *
173
-     * @param ExtensionCollection|array|string|null $extensions Extensions to validate.
174
-     *
175
-     * @return ExtensionCollection Validated extensions collection.
176
-     */
177
-    protected function validateExtensions($extensions)
178
-    {
179
-        if (empty($extensions)) {
180
-            $extensions = new ExtensionCollection(['']);
181
-        }
182
-
183
-        if (! $extensions instanceof ExtensionCollection) {
184
-            $extensions = new ExtensionCollection((array)$extensions);
185
-        }
186
-
187
-        return $extensions;
188
-    }
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 ExtensionCollection
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 ExtensionCollection|array|string|null $extensions Optional. Extensions that this location can accept.
56
+	 */
57
+	public function __construct($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
+			? $this->getURIs($criteria)->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 URICollection URIs that match the criteria or an empty collection if none found.
89
+	 */
90
+	public function getURIs(array $criteria)
91
+	{
92
+		$uris = new URICollection();
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, $extension)
124
+	{
125
+		$names = [];
126
+
127
+		$names[] = array_map(function ($criterion) use ($extension) {
128
+			$criterion = URIHelper::getFilename($criterion);
129
+
130
+			return empty($extension) || URIHelper::hasExtension($criterion, $extension)
131
+				? $criterion
132
+				: $criterion . $extension;
133
+		}, $criteria)[0];
134
+
135
+		return $this->arrayToRegexPattern(array_unique($names));
136
+	}
137
+
138
+	/**
139
+	 * Get the path pattern to pass to the file finder.
140
+	 *
141
+	 * @since 0.1.3
142
+	 *
143
+	 * @return string Path pattern to pass to the file finder.
144
+	 */
145
+	protected function getPathPattern()
146
+	{
147
+		return $this->path;
148
+	}
149
+
150
+	/**
151
+	 * Get an array as a regular expression pattern string.
152
+	 *
153
+	 * @since 0.1.3
154
+	 *
155
+	 * @param array $array Array to generate the pattern for.
156
+	 *
157
+	 * @return string Generated regular expression pattern.
158
+	 */
159
+	protected function arrayToRegexPattern(array $array)
160
+	{
161
+		$array = array_map(function ($entry) {
162
+			return preg_quote($entry);
163
+		}, $array);
164
+
165
+		return '/' . implode('|', $array) . '/';
166
+	}
167
+
168
+	/**
169
+	 * Validate the extensions and return a collection.
170
+	 *
171
+	 * @since 0.1.1
172
+	 *
173
+	 * @param ExtensionCollection|array|string|null $extensions Extensions to validate.
174
+	 *
175
+	 * @return ExtensionCollection Validated extensions collection.
176
+	 */
177
+	protected function validateExtensions($extensions)
178
+	{
179
+		if (empty($extensions)) {
180
+			$extensions = new ExtensionCollection(['']);
181
+		}
182
+
183
+		if (! $extensions instanceof ExtensionCollection) {
184
+			$extensions = new ExtensionCollection((array)$extensions);
185
+		}
186
+
187
+		return $extensions;
188
+	}
189 189
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -124,12 +124,12 @@  discard block
 block discarded – undo
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
             $criterion = URIHelper::getFilename($criterion);
129 129
 
130 130
             return empty($extension) || URIHelper::hasExtension($criterion, $extension)
131 131
                 ? $criterion
132
-                : $criterion . $extension;
132
+                : $criterion.$extension;
133 133
         }, $criteria)[0];
134 134
 
135 135
         return $this->arrayToRegexPattern(array_unique($names));
@@ -158,11 +158,11 @@  discard block
 block discarded – undo
158 158
      */
159 159
     protected function arrayToRegexPattern(array $array)
160 160
     {
161
-        $array = array_map(function ($entry) {
161
+        $array = array_map(function($entry) {
162 162
             return preg_quote($entry);
163 163
         }, $array);
164 164
 
165
-        return '/' . implode('|', $array) . '/';
165
+        return '/'.implode('|', $array).'/';
166 166
     }
167 167
 
168 168
     /**
@@ -180,8 +180,8 @@  discard block
 block discarded – undo
180 180
             $extensions = new ExtensionCollection(['']);
181 181
         }
182 182
 
183
-        if (! $extensions instanceof ExtensionCollection) {
184
-            $extensions = new ExtensionCollection((array)$extensions);
183
+        if ( ! $extensions instanceof ExtensionCollection) {
184
+            $extensions = new ExtensionCollection((array) $extensions);
185 185
         }
186 186
 
187 187
         return $extensions;
Please login to merge, or discard this patch.
src/View/Support/URIHelper.php 1 patch
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -22,38 +22,38 @@
 block discarded – undo
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($uri, $extension)
36
-    {
37
-        $uriLength       = mb_strlen($uri);
38
-        $extensionLength = mb_strlen($extension);
39
-        if ($extensionLength > $uriLength) {
40
-            return false;
41
-        }
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($uri, $extension)
36
+	{
37
+		$uriLength       = mb_strlen($uri);
38
+		$extensionLength = mb_strlen($extension);
39
+		if ($extensionLength > $uriLength) {
40
+			return false;
41
+		}
42 42
 
43
-        return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
44
-    }
43
+		return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
44
+	}
45 45
 
46
-    /**
47
-     * Get the filename for an URI.
48
-     *
49
-     * @since 0.1.3
50
-     *
51
-     * @param string $uri URI to get the filename from.
52
-     *
53
-     * @return string Filename without path.
54
-     */
55
-    public static function getFilename($uri)
56
-    {
57
-        return basename($uri);
58
-    }
46
+	/**
47
+	 * Get the filename for an URI.
48
+	 *
49
+	 * @since 0.1.3
50
+	 *
51
+	 * @param string $uri URI to get the filename from.
52
+	 *
53
+	 * @return string Filename without path.
54
+	 */
55
+	public static function getFilename($uri)
56
+	{
57
+		return basename($uri);
58
+	}
59 59
 }
Please login to merge, or discard this patch.
src/View/Support/ExtensionCollection.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -24,24 +24,24 @@
 block discarded – undo
24 24
 class ExtensionCollection extends ArrayCollection
25 25
 {
26 26
 
27
-    /**
28
-     * Check whether a given URI has a specific extension.
29
-     *
30
-     * @since 0.1.0
31
-     *
32
-     * @param string $uri       URI to check the extension of.
33
-     * @param string $extension Extension to check for.
34
-     *
35
-     * @return bool
36
-     */
37
-    public static function hasExtension($uri, $extension)
38
-    {
39
-        $uriLength       = mb_strlen($uri);
40
-        $extensionLength = mb_strlen($extension);
41
-        if ($extensionLength > $uriLength) {
42
-            return false;
43
-        }
27
+	/**
28
+	 * Check whether a given URI has a specific extension.
29
+	 *
30
+	 * @since 0.1.0
31
+	 *
32
+	 * @param string $uri       URI to check the extension of.
33
+	 * @param string $extension Extension to check for.
34
+	 *
35
+	 * @return bool
36
+	 */
37
+	public static function hasExtension($uri, $extension)
38
+	{
39
+		$uriLength       = mb_strlen($uri);
40
+		$extensionLength = mb_strlen($extension);
41
+		if ($extensionLength > $uriLength) {
42
+			return false;
43
+		}
44 44
 
45
-        return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
46
-    }
45
+		return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
46
+	}
47 47
 }
Please login to merge, or discard this patch.
src/View/Engine/PHPEngine.php 1 patch
Indentation   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -26,73 +26,73 @@
 block discarded – undo
26 26
 class PHPEngine extends AbstractEngine
27 27
 {
28 28
 
29
-    const PHP_EXTENSION = '.php';
29
+	const PHP_EXTENSION = '.php';
30 30
 
31
-    /**
32
-     * Check whether the Findable can handle an individual criterion.
33
-     *
34
-     * @since 0.1.0
35
-     *
36
-     * @param mixed $criterion Criterion to check.
37
-     *
38
-     * @return bool Whether the Findable can handle the criterion.
39
-     */
40
-    public function canHandle($criterion)
41
-    {
42
-        return URIHelper::hasExtension($criterion, static::PHP_EXTENSION)
43
-               && is_readable($criterion);
44
-    }
31
+	/**
32
+	 * Check whether the Findable can handle an individual criterion.
33
+	 *
34
+	 * @since 0.1.0
35
+	 *
36
+	 * @param mixed $criterion Criterion to check.
37
+	 *
38
+	 * @return bool Whether the Findable can handle the criterion.
39
+	 */
40
+	public function canHandle($criterion)
41
+	{
42
+		return URIHelper::hasExtension($criterion, static::PHP_EXTENSION)
43
+			   && is_readable($criterion);
44
+	}
45 45
 
46
-    /**
47
-     * Render a given URI.
48
-     *
49
-     * @since 0.1.0
50
-     *
51
-     * @param string $uri     URI to render.
52
-     * @param array  $context Context in which to render.
53
-     *
54
-     * @return string Rendered HTML.
55
-     * @throws FailedToLoadViewException If the View URI is not accessible or readable.
56
-     * @throws FailedToLoadViewException If the View URI could not be loaded.
57
-     */
58
-    public function render($uri, array $context = [])
59
-    {
60
-        if (! is_readable($uri)) {
61
-            throw new FailedToLoadViewException(
62
-                sprintf(
63
-                    _('The View URI "%1$s" is not accessible or readable.'),
64
-                    $uri
65
-                )
66
-            );
67
-        }
46
+	/**
47
+	 * Render a given URI.
48
+	 *
49
+	 * @since 0.1.0
50
+	 *
51
+	 * @param string $uri     URI to render.
52
+	 * @param array  $context Context in which to render.
53
+	 *
54
+	 * @return string Rendered HTML.
55
+	 * @throws FailedToLoadViewException If the View URI is not accessible or readable.
56
+	 * @throws FailedToLoadViewException If the View URI could not be loaded.
57
+	 */
58
+	public function render($uri, array $context = [])
59
+	{
60
+		if (! is_readable($uri)) {
61
+			throw new FailedToLoadViewException(
62
+				sprintf(
63
+					_('The View URI "%1$s" is not accessible or readable.'),
64
+					$uri
65
+				)
66
+			);
67
+		}
68 68
 
69
-        extract($context, EXTR_SKIP);
69
+		extract($context, EXTR_SKIP);
70 70
 
71
-        // Save current buffering level so we can backtrack in case of an error.
72
-        // This is needed because the view itself might also add an unknown number of output buffering levels.
73
-        $bufferLevel = ob_get_level();
74
-        ob_start();
71
+		// Save current buffering level so we can backtrack in case of an error.
72
+		// This is needed because the view itself might also add an unknown number of output buffering levels.
73
+		$bufferLevel = ob_get_level();
74
+		ob_start();
75 75
 
76
-        try {
77
-            include($uri);
78
-        } catch (Exception $exception) {
76
+		try {
77
+			include($uri);
78
+		} catch (Exception $exception) {
79 79
 
80
-            // Remove whatever levels were added up until now.
81
-            while (ob_get_level() > $bufferLevel) {
82
-                ob_end_clean();
83
-            }
80
+			// Remove whatever levels were added up until now.
81
+			while (ob_get_level() > $bufferLevel) {
82
+				ob_end_clean();
83
+			}
84 84
 
85
-            throw new FailedToLoadViewException(
86
-                sprintf(
87
-                    _('Could not load the View URI "%1$s". Reason: "%2$s".'),
88
-                    $uri,
89
-                    $exception->getMessage()
90
-                ),
91
-                $exception->getCode(),
92
-                $exception
93
-            );
94
-        }
85
+			throw new FailedToLoadViewException(
86
+				sprintf(
87
+					_('Could not load the View URI "%1$s". Reason: "%2$s".'),
88
+					$uri,
89
+					$exception->getMessage()
90
+				),
91
+				$exception->getCode(),
92
+				$exception
93
+			);
94
+		}
95 95
 
96
-        return ob_get_clean();
97
-    }
96
+		return ob_get_clean();
97
+	}
98 98
 }
Please login to merge, or discard this patch.