Skip to content

Commit

Permalink
Merge pull request #92 from sitegeist/feature/addStaticUriPrototype
Browse files Browse the repository at this point in the history
FEATURE: Add Sitegeist.Monocle:StaticUri prototype
  • Loading branch information
mficzel authored Oct 11, 2018
2 parents 0e28f2e + 6730be5 commit 5910604
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Classes/Sitegeist/Monocle/Controller/MockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Property\Exception\TargetNotFoundException;

/**
* Class MockController
* @package Sitegeist\Monocle\Controller
*/
class MockController extends ActionController
{

/**
* @var array
* @Flow\InjectConfiguration(path="uriMock.static")
*/
protected $staticUriMocks;

/**
* Return the given content and the type header
*
Expand All @@ -34,4 +42,21 @@ public function mirrorAction($content = '', $type = 'text/html')
$this->response->setHeader('Content-Type', $type);
return $content;
}

/**
* Return the given static content as defined in the configuration
*
* @param string $key
* @return void
*/
public function staticAction($key)
{
if ($key && is_array($this->staticUriMocks) && array_key_exists($key, $this->staticUriMocks)) {
$config = $this->staticUriMocks[$key];
$this->response->setHeader('Content-Type', $config['contentType']);
return file_get_contents($config['path']);
}

throw new TargetNotFoundException();
}
}
2 changes: 1 addition & 1 deletion Configuration/Policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ privilegeTargets:
'Sitegeist.Monocle:Styleguide.Api':
matcher: 'method(Sitegeist\Monocle\Controller\ApiController->(styleguideObjects|styleguideResources|sitePackages|viewportPresets|localePresets|renderPrototype)Action())'
'Sitegeist.Monocle:Styleguide.Mock':
matcher: 'method(Sitegeist\Monocle\Controller\MockController->(mirror)Action())'
matcher: 'method(Sitegeist\Monocle\Controller\MockController->(mirror|static)Action())'

roles:
'Neos.Neos:AbstractEditor':
Expand Down
7 changes: 7 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

Sitegeist:
Monocle:
uriMock:
#the static file uris to mock
static: {}
# key:
# path: 'resource://Vendor.Package/Private/Json/example.json'
# contentType: 'application/json'

fusion:
enableObjectTreeCache: true

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ For convenience special prototypes for json and text exist:
- `Sitegeist.Monocle:MirrorUri.Text`: And endpoint-mock with media-type `text/plain`


#### `Sitegeist.Monocle:StaticUri`

Create 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'
```
#### Mocking Uris inside the styleguide
```
Expand Down
14 changes: 14 additions & 0 deletions Resources/Private/Fusion/Prototypes/StaticUri/StaticUri.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
prototype(Sitegeist.Monocle:StaticUri) < prototype(Neos.Fusion:Component) {
key = null

renderer = Neos.Fusion:UriBuilder {
package = 'Sitegeist.Monocle'
controller = 'Mock'
action = 'static'
format = 'text'
arguments {
key = ${props.key}
}
}

}

0 comments on commit 5910604

Please sign in to comment.