-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add jsonFile based resolver/requirement
- Loading branch information
1 parent
00f2064
commit a02e61b
Showing
7 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/p2ee/SilexPartletDemo/Components/dynamic/JsonItemTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
namespace p2ee\SilexPartletDemo\Components\dynamic; | ||
|
||
use p2ee\SilexPartletDemo\Requirement\JsonFileRequirement; | ||
use p2ee\SilexPartletDemo\SilexPartlet; | ||
|
||
class JsonItemTest extends SilexPartlet | ||
{ | ||
|
||
protected $article; | ||
|
||
public function collect() | ||
{ | ||
yield [ | ||
new JsonFileRequirement('article' , __DIR__ . '/item.json', 'article1'), | ||
]; | ||
} | ||
|
||
public function getData() | ||
{ | ||
|
||
return [ | ||
'title' => $this->article['title'], | ||
'text' => $this->article['text'], | ||
]; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/p2ee/SilexPartletDemo/Components/dynamic/JsonItemTest.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<li> | ||
<article> | ||
<h3>{{ title }}</h3> | ||
|
||
<div>{{ text }}</div> | ||
</article> | ||
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"article1": { | ||
"title": "haw haw", | ||
"text": "how how how" | ||
}, | ||
"article2": { | ||
"title": "lroemia Ipsum", | ||
"text": "dolorte to cetsum" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/p2ee/SilexPartletDemo/Requirement/JsonFileRequirement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* | ||
* | ||
* | ||
* | ||
*/ | ||
|
||
namespace p2ee\SilexPartletDemo\Requirement; | ||
|
||
use p2ee\Preparables\Requirement; | ||
|
||
class JsonFileRequirement extends Requirement | ||
{ | ||
|
||
protected $file; | ||
|
||
protected $jsonElementKey; | ||
|
||
public function __construct($key, $file, $jsonElementKey = null) | ||
{ | ||
$this->key = $key; | ||
$this->file = $file; | ||
$this->jsonElementKey = $jsonElementKey; | ||
} | ||
|
||
public function getFile() | ||
{ | ||
return $this->file; | ||
} | ||
|
||
public function getJsonElementKey() | ||
{ | ||
return $this->jsonElementKey; | ||
} | ||
|
||
public function isCacheable() | ||
{ | ||
return false; | ||
} | ||
|
||
public function getCacheKey() | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* | ||
* | ||
* | ||
* | ||
*/ | ||
|
||
namespace p2ee\SilexPartletDemo\Resolver; | ||
|
||
use p2ee\Preparables\Preparer; | ||
use p2ee\Preparables\Requirement; | ||
use p2ee\Preparables\Resolver; | ||
use p2ee\SilexPartletDemo\Requirement\JsonFileRequirement; | ||
|
||
class JsonFileResolver implements Resolver | ||
{ | ||
|
||
/** | ||
* @param Requirement|JsonFileRequirement $requirement | ||
* @param Preparer $preparer | ||
* | ||
* @return mixed | ||
*/ | ||
public function resolve(Requirement $requirement, Preparer $preparer) | ||
{ | ||
$result = json_decode(file_get_contents($requirement->getFile()), true); | ||
|
||
$key = $requirement->getJsonElementKey(); | ||
if ($key !== null) { | ||
$result = $result[$key]; | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getSupportedType() | ||
{ | ||
return JsonFileRequirement::class; | ||
} | ||
} |