diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2018-12-15 16:23:19 +0100 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2018-12-15 16:23:19 +0100 |
commit | c4a005a69a854682f064016d428d23e50c6e9dee (patch) | |
tree | 01ca79b09fb7ca7ad7c3be2ded4f5b323e5b3cb5 /app/class | |
parent | fbe5d2beb2bcc7035edc19eb0343f0ee662ba5af (diff) | |
download | wcms-c4a005a69a854682f064016d428d23e50c6e9dee.tar.gz wcms-c4a005a69a854682f064016d428d23e50c6e9dee.zip |
Flywheel extension and custom json formatter
Diffstat (limited to 'app/class')
-rw-r--r-- | app/class/modeldb.php | 10 | ||||
-rw-r--r-- | app/class/wflywheel/formatter/json.php | 20 | ||||
-rw-r--r-- | app/class/wflywheel/predicate.php | 11 | ||||
-rw-r--r-- | app/class/wflywheel/query.php | 16 | ||||
-rw-r--r-- | app/class/wflywheel/repository.php | 30 |
5 files changed, 83 insertions, 4 deletions
diff --git a/app/class/modeldb.php b/app/class/modeldb.php index 94ed084..bc2a0c7 100644 --- a/app/class/modeldb.php +++ b/app/class/modeldb.php @@ -1,10 +1,9 @@ <?php class Modeldb extends Model { - /** @var PDO */ - protected $bdd; protected $arttable; protected $database; + /** @var \WFlywheel\Repository */ protected $artstore; @@ -16,8 +15,11 @@ class Modeldb extends Model public function dbinit() { - $this->database = new \JamesMoss\Flywheel\Config(Model::DATABASE_DIR); - $this->artstore = new \JamesMoss\Flywheel\Repository(Config::arttable(), $this->database); + $this->database = new \JamesMoss\Flywheel\Config(Model::DATABASE_DIR, [ + 'query_class' => "\WFlywheel\Query", + 'formatter' => new \WFlywheel\Formatter\JSON, + ]); + $this->artstore = new \WFlywheel\Repository(Config::arttable(), $this->database); } diff --git a/app/class/wflywheel/formatter/json.php b/app/class/wflywheel/formatter/json.php new file mode 100644 index 0000000..8583802 --- /dev/null +++ b/app/class/wflywheel/formatter/json.php @@ -0,0 +1,20 @@ +<?php +namespace WFlywheel\Formatter; + +class JSON implements \JamesMoss\Flywheel\Formatter\FormatInterface +{ + public function getFileExtension() + { + return 'json'; + } + public function encode(array $data) + { + $options = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : null; + return json_encode($data, $options); + } + public function decode($data) + { + $options = defined('JSON_OBJECT_AS_ARRAY') ? JSON_OBJECT_AS_ARRAY : null; + return json_decode($data, $options); + } +}
\ No newline at end of file diff --git a/app/class/wflywheel/predicate.php b/app/class/wflywheel/predicate.php new file mode 100644 index 0000000..2229461 --- /dev/null +++ b/app/class/wflywheel/predicate.php @@ -0,0 +1,11 @@ +<?php +namespace WFlywheel; + +class Predicate extends \JamesMoss\Flywheel\Predicate +{ + public function __construct() { + $this->operators = array( + '>', '>=', '<', '<=', '==', '===', '!=', '!==', 'IN' + ); + } +} diff --git a/app/class/wflywheel/query.php b/app/class/wflywheel/query.php new file mode 100644 index 0000000..49aff16 --- /dev/null +++ b/app/class/wflywheel/query.php @@ -0,0 +1,16 @@ +<?php +namespace WFlywheel; + +class Query extends \JamesMoss\Flywheel\Query +{ + /** + * Constructor + * + * @param Repository $repository The repo this query will run against. + */ + public function __construct(Repository $repository) + { + parent::__construct($repository); + $this->predicate = new Predicate(); + } +} diff --git a/app/class/wflywheel/repository.php b/app/class/wflywheel/repository.php new file mode 100644 index 0000000..baad42e --- /dev/null +++ b/app/class/wflywheel/repository.php @@ -0,0 +1,30 @@ +<?php +namespace WFlywheel; + +class Repository extends \JamesMoss\Flywheel\Repository +{ + /** + * Get an array containing the path of all files in this repository + * + * @return array An array, item is a file + */ + public function getAllFiles() + { + $ext = $this->formatter->getFileExtension(); + $files = glob($this->path . DIRECTORY_SEPARATOR . '*.' . $ext); + return $files; + } + + /** + * Get an array containing the id of all files in this repository + * + * @return array An array, item is a id + */ + public function getAllIds() + { + $ext = $this->formatter->getFileExtension(); + return array_map(function($path) use ($ext) { + return $this->getIdFromPath($path, $ext); + }, $this->getAllFiles()); + } +} |