Skip to content

Commit 6730be5

Browse files
committed
FEATURE: Add Sitegeist.Monocle:StaticUri prototype
The prototype creates an URI that will return the content of the file and the contentType for the given key. ``` endpointUrl = Sitegeist.Monocle:StaticUri { key = 'example' } ``` The path and content type for each key are configured via Settings: ```yaml Sitegeist: Monocle: uriMock: static: example: path: 'resource://Vendor.Package/Private/Json/example.json' contentType: 'application/json' ```
1 parent 0e28f2e commit 6730be5

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

Classes/Sitegeist/Monocle/Controller/MockController.php

+25
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515

1616
use Neos\Flow\Annotations as Flow;
1717
use Neos\Flow\Mvc\Controller\ActionController;
18+
use Neos\Flow\Property\Exception\TargetNotFoundException;
1819

1920
/**
2021
* Class MockController
2122
* @package Sitegeist\Monocle\Controller
2223
*/
2324
class MockController extends ActionController
2425
{
26+
27+
/**
28+
* @var array
29+
* @Flow\InjectConfiguration(path="uriMock.static")
30+
*/
31+
protected $staticUriMocks;
32+
2533
/**
2634
* Return the given content and the type header
2735
*
@@ -34,4 +42,21 @@ public function mirrorAction($content = '', $type = 'text/html')
3442
$this->response->setHeader('Content-Type', $type);
3543
return $content;
3644
}
45+
46+
/**
47+
* Return the given static content as defined in the configuration
48+
*
49+
* @param string $key
50+
* @return void
51+
*/
52+
public function staticAction($key)
53+
{
54+
if ($key && is_array($this->staticUriMocks) && array_key_exists($key, $this->staticUriMocks)) {
55+
$config = $this->staticUriMocks[$key];
56+
$this->response->setHeader('Content-Type', $config['contentType']);
57+
return file_get_contents($config['path']);
58+
}
59+
60+
throw new TargetNotFoundException();
61+
}
3762
}

Configuration/Policy.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ privilegeTargets:
99
'Sitegeist.Monocle:Styleguide.Api':
1010
matcher: 'method(Sitegeist\Monocle\Controller\ApiController->(styleguideObjects|styleguideResources|sitePackages|viewportPresets|localePresets|renderPrototype)Action())'
1111
'Sitegeist.Monocle:Styleguide.Mock':
12-
matcher: 'method(Sitegeist\Monocle\Controller\MockController->(mirror)Action())'
12+
matcher: 'method(Sitegeist\Monocle\Controller\MockController->(mirror|static)Action())'
1313

1414
roles:
1515
'Neos.Neos:AbstractEditor':

Configuration/Settings.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

22
Sitegeist:
33
Monocle:
4+
uriMock:
5+
#the static file uris to mock
6+
static: {}
7+
# key:
8+
# path: 'resource://Vendor.Package/Private/Json/example.json'
9+
# contentType: 'application/json'
10+
411
fusion:
512
enableObjectTreeCache: true
613

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ For convenience special prototypes for json and text exist:
154154
- `Sitegeist.Monocle:MirrorUri.Text`: And endpoint-mock with media-type `text/plain`
155155

156156

157+
#### `Sitegeist.Monocle:StaticUri`
158+
159+
Create an URI that will return the content of the file and the contentType for the given key.
160+
161+
```
162+
endpointUrl = Sitegeist.Monocle:StaticUri {
163+
key = 'example'
164+
}
165+
```
166+
167+
The path and content type for each key are configured via Settings:
168+
169+
```yaml
170+
Sitegeist:
171+
Monocle:
172+
uriMock:
173+
static:
174+
example:
175+
path: 'resource://Vendor.Package/Private/Json/example.json'
176+
contentType: 'application/json'
177+
```
178+
157179
#### Mocking Uris inside the styleguide
158180
159181
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
prototype(Sitegeist.Monocle:StaticUri) < prototype(Neos.Fusion:Component) {
2+
key = null
3+
4+
renderer = Neos.Fusion:UriBuilder {
5+
package = 'Sitegeist.Monocle'
6+
controller = 'Mock'
7+
action = 'static'
8+
format = 'text'
9+
arguments {
10+
key = ${props.key}
11+
}
12+
}
13+
14+
}

0 commit comments

Comments
 (0)