Skip to content

Commit

Permalink
Support at data and yaml (#3)
Browse files Browse the repository at this point in the history
* Init feature updates

* changed function to more general name, support for JSON data as well

* Fixed bug when @DaTa was JSON

* Fixed 5.3.3 compatibility issues

* fixed var name in if statement

* Fixed if statement...again

* Fixed bad tick marks

* Fixed incorrect quote marks in data_replace function
  • Loading branch information
micahgodbolt authored Aug 1, 2016
1 parent 20d9d1f commit 833092f
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"nesbot/Carbon": "~1.6",
"erusev/parsedown": "^1.6",
"mnapoli/front-yaml": "^1.5",
"symfony/twig-bridge": "^2.3"
"symfony/twig-bridge": "^2.3",
"symfony/yaml": "=2.6.13"
},
"autoload": {
"psr-0": {
Expand Down
Binary file added resources/fixtures/cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions resources/fixtures/foo.docs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bar: abc
baz: def
4 changes: 4 additions & 0 deletions resources/fixtures/foo2.docs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bar": "123",
"baz": "456"
}
2 changes: 2 additions & 0 deletions resources/fixtures/image.docs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src: /resources/fixtures/cat.jpg
alt: This is a photo
24 changes: 24 additions & 0 deletions resources/fixtures/image.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Image",
"category": "atom",
"type": "object",
"format": "grid",
"properties": {
"name": {
"type": "string",
"default": "image",
"options": {
"hidden": true
}
},
"src": {
"type": "string"
},
"alt": {
"type": "string"
}
},
"required": ["name"],
"additionalProperties": false
}
1 change: 1 addition & 0 deletions resources/fixtures/image.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img src="{{src}}" alt="{{alt}}" >
7 changes: 0 additions & 7 deletions resources/fixtures/test.docs.json

This file was deleted.

9 changes: 9 additions & 0 deletions resources/fixtures/test.docs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: test
quotation: We think of Red Hat as one of just a handful of superior engineering and support organizations in the United States.
attribution_name: John Defeo
attribution_title: President of Infrastructure, CIGNA
image: @image
foo:
- @foo
- @foo2

17 changes: 17 additions & 0 deletions resources/fixtures/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
"title": "Attribution Title",
"description": "What is their job title?",
"type": "string"
},
"image": {
"$ref": "image.json"
},
"foo": {
"type": "array",
"items": {
"type": "object",
"properties": {
"bar": {
"type": "string"
},
"baz": {
"type": "string"
}
}
}
}
},
"required": ["name", "quotation", "attribution_name"],
Expand Down
9 changes: 7 additions & 2 deletions resources/fixtures/test.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@


<div>
{% include 'image.twig' with image only %}
<blockquote>
<p>{{quotation}}</p>
<footer>
<span>{{attribution_name}}</span>{% if attribution_title %}, {{attribution_title}}{% endif %}
</footer>
</blockquote>
</div>


{% for item in foo %}
{{item.bar}} <br>
{{item.baz}} <br><br>
{% endfor %}
17 changes: 13 additions & 4 deletions src/PatternKit/SchemaControllerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\Yaml\Yaml;


class SchemaControllerProvider implements ControllerProviderInterface
{
Expand Down Expand Up @@ -33,13 +35,20 @@ public function connect(Application $app)
// end navigation

if ($seed_path) {
$seed_file = $retriever->retrieve('file://' . realpath($seed_path));
if (!empty($seed_file)) {
$seed_data = $seed_file;

$seed_file = file_get_contents('file://' . realpath($seed_path));
if (($pathinfo = pathinfo($seed_path)) && isset($pathinfo['extension']) && $pathinfo['extension'] == 'yaml') {

$seed_data = Yaml::parse($seed_file);

}
elseif (!empty($seed_file)) {
$seed_data = json_decode($seed_file, true);
}
else {
$seed_data = array();
$seed_data = array();
}
data_replace($seed_data);
}
else $seed_data = array();

Expand Down
30 changes: 30 additions & 0 deletions src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Yaml\Yaml;
use Carbon\Carbon;
use Mni\FrontYAML\Parser;

Expand Down Expand Up @@ -89,6 +90,29 @@

// Custom Functions


function data_replace(&$data) {
if (is_array($data)) {
foreach ($data as &$value) {
if (is_array($value) ) {
data_replace($value);
}
elseif (is_string($value) && $value[0] == '@') {
$file_path = 'file://' . realpath(get_asset_path(substr($value, 1), 'data'));
if (($pathinfo = pathinfo($file_path)) && isset($pathinfo['extension']) && $pathinfo['extension'] == 'yaml') {
$data_replace_with = Yaml::parse(file_get_contents($file_path));
}
else {
$data_replace_with = json_decode(file_get_contents($file_path), true);
}

$value = data_replace($data_replace_with);
}
}
}
return $data;
}

//// Get path to matching asset
function get_asset_path($name, $type) {
global $app;
Expand All @@ -102,12 +126,18 @@ function get_asset_path($name, $type) {
if ($paths) {
foreach ($paths as $path) {
$extension = $app['config']['extensions'][$type];
$yaml_extension = str_replace('.json', '.yaml', $extension);
$dir = './' . $path;
$file_path = "{$dir}/{$name}{$extension}";
$yaml_file_path = "{$dir}/{$name}{$yaml_extension}";
if (is_dir($dir) && is_readable($file_path)) {
$return = $file_path;
break;
}
else if (is_dir($dir) && is_readable($yaml_file_path)) {
$return = $yaml_file_path;
break;
}
}
}

Expand Down

0 comments on commit 833092f

Please sign in to comment.