Passed
Push — master ( dbf10f...5d13c8 )
by Alain
03:33 queued 01:09
created
src/View/ViewBuilder.php 1 patch
Indentation   +205 added lines, -205 removed lines patch added patch discarded remove patch
@@ -32,209 +32,209 @@
 block discarded – undo
32 32
 class ViewBuilder
33 33
 {
34 34
 
35
-    use ConfigTrait;
36
-
37
-    const ENGINE_FINDER_KEY = 'EngineFinder';
38
-    const VIEW_FINDER_KEY   = 'ViewFinder';
39
-
40
-    /**
41
-     * ViewFinder instance.
42
-     *
43
-     * @since 0.1.0
44
-     *
45
-     * @var ViewFinderInterface
46
-     */
47
-    protected $viewFinder;
48
-
49
-    /**
50
-     * EngineFinder instance.
51
-     *
52
-     * @since 0.1.0
53
-     *
54
-     * @var EngineFinderInterface
55
-     */
56
-    protected $engineFinder;
57
-
58
-    /**
59
-     * Locations to scan for views.
60
-     *
61
-     * @since 0.1.0
62
-     *
63
-     * @var LocationInterface[]
64
-     */
65
-    protected $locations;
66
-
67
-    /**
68
-     * Instantiate a ViewBuilder object.
69
-     *
70
-     * @since 0.1.0
71
-     *
72
-     * @param ConfigInterface            $config       Configuration settings.
73
-     * @param ViewFinderInterface|null   $viewFinder   ViewFinder instance.
74
-     * @param EngineFinderInterface|null $engineFinder EngineFinder instance.
75
-     *
76
-     * @throws FailedToProcessConfigException If the config could not be processed.
77
-     */
78
-    public function __construct(
79
-        ConfigInterface $config,
80
-        ViewFinderInterface $viewFinder = null,
81
-        EngineFinderInterface $engineFinder = null
82
-    ) {
83
-        $this->processConfig($config);
84
-        $this->viewFinder   = $viewFinder;
85
-        $this->engineFinder = $engineFinder;
86
-    }
87
-
88
-    /**
89
-     * Create a new view for a given URI.
90
-     *
91
-     * @since 0.1.0
92
-     *
93
-     * @param string      $view View identifier to create a view for.
94
-     * @param string|null $type Type of view to create.
95
-     *
96
-     * @return ViewInterface Instance of the requested view.
97
-     */
98
-    public function create($view, $type = null)
99
-    {
100
-        $uri    = $this->scanLocations([$view]);
101
-        $engine = $this->getEngine($uri);
102
-
103
-        return $this->getView($uri, $engine, $type);
104
-    }
105
-
106
-    /**
107
-     * Get an Engine that can deal with the given URI.
108
-     *
109
-     * @since 0.1.0
110
-     *
111
-     * @param string|false $uri URI to get an engine for.
112
-     *
113
-     * @return EngineInterface Instance of an engine that can deal with the given URI.
114
-     */
115
-    public function getEngine($uri)
116
-    {
117
-        return $this->getEngineFinder()->find([$uri]);
118
-    }
119
-
120
-    /**
121
-     * Get a view for a given URI, engine and type.
122
-     *
123
-     * @since 0.1.0
124
-     *
125
-     * @param string          $uri    URI to get a view for.
126
-     * @param EngineInterface $engine Engine to use for the view.
127
-     * @param mixed           $type   Type of view to get.
128
-     *
129
-     * @return ViewInterface View that matches the given requirements.
130
-     */
131
-    public function getView($uri, EngineInterface $engine, $type = null)
132
-    {
133
-        if (null === $type) {
134
-            return $this->getViewFinder()->find([$uri], $engine);
135
-        }
136
-
137
-        return $this->resolveType($type, $uri, $engine);
138
-    }
139
-
140
-    /**
141
-     * Get the ViewFinder instance.
142
-     *
143
-     * @since 0.1.0
144
-     *
145
-     * @return ViewFinderInterface Instance of a ViewFinder.
146
-     */
147
-    public function getViewFinder()
148
-    {
149
-        if (null === $this->viewFinder) {
150
-            $viewFinderClass  = $this->config->getKey(static::VIEW_FINDER_KEY, 'ClassName');
151
-            $this->viewFinder = new $viewFinderClass($this->config->getSubConfig(static::VIEW_FINDER_KEY));
152
-        }
153
-
154
-        return $this->viewFinder;
155
-    }
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
-        if (null === $this->engineFinder) {
167
-            $engineFinderClass  = $this->config->getKey(static::ENGINE_FINDER_KEY, 'ClassName');
168
-            $this->engineFinder = new $engineFinderClass($this->config->getSubConfig(static::ENGINE_FINDER_KEY));
169
-        }
170
-
171
-        return $this->engineFinder;
172
-    }
173
-
174
-    /**
175
-     * Add a location to scan with the ViewFinder.
176
-     *
177
-     * @since 0.1.0
178
-     *
179
-     * @param LocationInterface $location Location to scan with the ViewFinder.
180
-     */
181
-    public function addLocation(LocationInterface $location)
182
-    {
183
-        $this->locations[] = $location;
184
-    }
185
-
186
-    /**
187
-     * Scan Locations for an URI that matches the specified criteria.
188
-     *
189
-     * @since 0.1.0
190
-     *
191
-     * @param array $criteria Criteria to match.
192
-     *
193
-     * @return string|bool URI of the requested view, or false if not found.
194
-     */
195
-    public function scanLocations(array $criteria)
196
-    {
197
-        /** @var LocationInterface $location */
198
-        foreach ($this->locations as $location) {
199
-            if ($uri = $location->getURI($criteria)) {
200
-                return $uri;
201
-            }
202
-        }
203
-
204
-        return false;
205
-    }
206
-
207
-    /**
208
-     * Resolve the view type.
209
-     *
210
-     * @since 0.1.0
211
-     *
212
-     * @param mixed           $type   Type of view that was requested.
213
-     * @param string          $uri    URI to get a view for.
214
-     * @param EngineInterface $engine Engine to use for the view.
215
-     *
216
-     * @return ViewInterface Resolved View object.
217
-     * @throws FailedToInstantiateViewException If the view type could not be resolved.
218
-     */
219
-    protected function resolveType($type, $uri, EngineInterface $engine = null)
220
-    {
221
-        if (is_string($type) && $this->config->hasKey(static::VIEW_FINDER_KEY)) {
222
-            $type = new $type($uri, $engine);
223
-        }
224
-
225
-        if (is_callable($type)) {
226
-            $type = $type($uri, $engine);
227
-        }
228
-
229
-        if (! $type instanceof ViewInterface) {
230
-            throw new FailedToInstantiateViewException(
231
-                sprintf(
232
-                    _('Could not instantiate view "%s".'),
233
-                    serialize($type)
234
-                )
235
-            );
236
-        }
237
-
238
-        return $type;
239
-    }
35
+	use ConfigTrait;
36
+
37
+	const ENGINE_FINDER_KEY = 'EngineFinder';
38
+	const VIEW_FINDER_KEY   = 'ViewFinder';
39
+
40
+	/**
41
+	 * ViewFinder instance.
42
+	 *
43
+	 * @since 0.1.0
44
+	 *
45
+	 * @var ViewFinderInterface
46
+	 */
47
+	protected $viewFinder;
48
+
49
+	/**
50
+	 * EngineFinder instance.
51
+	 *
52
+	 * @since 0.1.0
53
+	 *
54
+	 * @var EngineFinderInterface
55
+	 */
56
+	protected $engineFinder;
57
+
58
+	/**
59
+	 * Locations to scan for views.
60
+	 *
61
+	 * @since 0.1.0
62
+	 *
63
+	 * @var LocationInterface[]
64
+	 */
65
+	protected $locations;
66
+
67
+	/**
68
+	 * Instantiate a ViewBuilder object.
69
+	 *
70
+	 * @since 0.1.0
71
+	 *
72
+	 * @param ConfigInterface            $config       Configuration settings.
73
+	 * @param ViewFinderInterface|null   $viewFinder   ViewFinder instance.
74
+	 * @param EngineFinderInterface|null $engineFinder EngineFinder instance.
75
+	 *
76
+	 * @throws FailedToProcessConfigException If the config could not be processed.
77
+	 */
78
+	public function __construct(
79
+		ConfigInterface $config,
80
+		ViewFinderInterface $viewFinder = null,
81
+		EngineFinderInterface $engineFinder = null
82
+	) {
83
+		$this->processConfig($config);
84
+		$this->viewFinder   = $viewFinder;
85
+		$this->engineFinder = $engineFinder;
86
+	}
87
+
88
+	/**
89
+	 * Create a new view for a given URI.
90
+	 *
91
+	 * @since 0.1.0
92
+	 *
93
+	 * @param string      $view View identifier to create a view for.
94
+	 * @param string|null $type Type of view to create.
95
+	 *
96
+	 * @return ViewInterface Instance of the requested view.
97
+	 */
98
+	public function create($view, $type = null)
99
+	{
100
+		$uri    = $this->scanLocations([$view]);
101
+		$engine = $this->getEngine($uri);
102
+
103
+		return $this->getView($uri, $engine, $type);
104
+	}
105
+
106
+	/**
107
+	 * Get an Engine that can deal with the given URI.
108
+	 *
109
+	 * @since 0.1.0
110
+	 *
111
+	 * @param string|false $uri URI to get an engine for.
112
+	 *
113
+	 * @return EngineInterface Instance of an engine that can deal with the given URI.
114
+	 */
115
+	public function getEngine($uri)
116
+	{
117
+		return $this->getEngineFinder()->find([$uri]);
118
+	}
119
+
120
+	/**
121
+	 * Get a view for a given URI, engine and type.
122
+	 *
123
+	 * @since 0.1.0
124
+	 *
125
+	 * @param string          $uri    URI to get a view for.
126
+	 * @param EngineInterface $engine Engine to use for the view.
127
+	 * @param mixed           $type   Type of view to get.
128
+	 *
129
+	 * @return ViewInterface View that matches the given requirements.
130
+	 */
131
+	public function getView($uri, EngineInterface $engine, $type = null)
132
+	{
133
+		if (null === $type) {
134
+			return $this->getViewFinder()->find([$uri], $engine);
135
+		}
136
+
137
+		return $this->resolveType($type, $uri, $engine);
138
+	}
139
+
140
+	/**
141
+	 * Get the ViewFinder instance.
142
+	 *
143
+	 * @since 0.1.0
144
+	 *
145
+	 * @return ViewFinderInterface Instance of a ViewFinder.
146
+	 */
147
+	public function getViewFinder()
148
+	{
149
+		if (null === $this->viewFinder) {
150
+			$viewFinderClass  = $this->config->getKey(static::VIEW_FINDER_KEY, 'ClassName');
151
+			$this->viewFinder = new $viewFinderClass($this->config->getSubConfig(static::VIEW_FINDER_KEY));
152
+		}
153
+
154
+		return $this->viewFinder;
155
+	}
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
+		if (null === $this->engineFinder) {
167
+			$engineFinderClass  = $this->config->getKey(static::ENGINE_FINDER_KEY, 'ClassName');
168
+			$this->engineFinder = new $engineFinderClass($this->config->getSubConfig(static::ENGINE_FINDER_KEY));
169
+		}
170
+
171
+		return $this->engineFinder;
172
+	}
173
+
174
+	/**
175
+	 * Add a location to scan with the ViewFinder.
176
+	 *
177
+	 * @since 0.1.0
178
+	 *
179
+	 * @param LocationInterface $location Location to scan with the ViewFinder.
180
+	 */
181
+	public function addLocation(LocationInterface $location)
182
+	{
183
+		$this->locations[] = $location;
184
+	}
185
+
186
+	/**
187
+	 * Scan Locations for an URI that matches the specified criteria.
188
+	 *
189
+	 * @since 0.1.0
190
+	 *
191
+	 * @param array $criteria Criteria to match.
192
+	 *
193
+	 * @return string|bool URI of the requested view, or false if not found.
194
+	 */
195
+	public function scanLocations(array $criteria)
196
+	{
197
+		/** @var LocationInterface $location */
198
+		foreach ($this->locations as $location) {
199
+			if ($uri = $location->getURI($criteria)) {
200
+				return $uri;
201
+			}
202
+		}
203
+
204
+		return false;
205
+	}
206
+
207
+	/**
208
+	 * Resolve the view type.
209
+	 *
210
+	 * @since 0.1.0
211
+	 *
212
+	 * @param mixed           $type   Type of view that was requested.
213
+	 * @param string          $uri    URI to get a view for.
214
+	 * @param EngineInterface $engine Engine to use for the view.
215
+	 *
216
+	 * @return ViewInterface Resolved View object.
217
+	 * @throws FailedToInstantiateViewException If the view type could not be resolved.
218
+	 */
219
+	protected function resolveType($type, $uri, EngineInterface $engine = null)
220
+	{
221
+		if (is_string($type) && $this->config->hasKey(static::VIEW_FINDER_KEY)) {
222
+			$type = new $type($uri, $engine);
223
+		}
224
+
225
+		if (is_callable($type)) {
226
+			$type = $type($uri, $engine);
227
+		}
228
+
229
+		if (! $type instanceof ViewInterface) {
230
+			throw new FailedToInstantiateViewException(
231
+				sprintf(
232
+					_('Could not instantiate view "%s".'),
233
+					serialize($type)
234
+				)
235
+			);
236
+		}
237
+
238
+		return $type;
239
+	}
240 240
 }
Please login to merge, or discard this patch.