From 9a5b30c4e3a239f3e127ff3d1593d39236039754 Mon Sep 17 00:00:00 2001 From: bjoern Date: Fri, 27 Sep 2019 19:26:44 +0200 Subject: [PATCH] adding --- README.md | 13 ++ composer.json | 19 +++ .../Collections/AbstractCollection.php | 114 ++++++++++++++++++ .../Directus/Collections/ItemCollection.php | 28 +++++ .../DirectusControllerAbstract.php | 69 +++++++++++ src/SuperGear/Directus/FlightAbstract.php | 27 +++++ src/SuperGear/Directus/Functions.php | 12 ++ .../Directus/Helpers/MarkdownHelper.php | 47 ++++++++ .../Directus/Repositories/Manager.php | 49 ++++++++ .../Repositories/RepositoryAbstract.php | 38 ++++++ 10 files changed, 416 insertions(+) create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/SuperGear/Directus/Collections/AbstractCollection.php create mode 100644 src/SuperGear/Directus/Collections/ItemCollection.php create mode 100644 src/SuperGear/Directus/Controllers/DirectusControllerAbstract.php create mode 100644 src/SuperGear/Directus/FlightAbstract.php create mode 100644 src/SuperGear/Directus/Functions.php create mode 100644 src/SuperGear/Directus/Helpers/MarkdownHelper.php create mode 100644 src/SuperGear/Directus/Repositories/Manager.php create mode 100644 src/SuperGear/Directus/Repositories/RepositoryAbstract.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..d00107f --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Gear - Directus PHP Client + +Small Libary to request Directus API. + +## ItemCollection + +**URL** +**Token** + +```PHP +$itemCollection = new ItemCollection($url, $token); +$articles = $itemCollection->find('articles', ['status' => 'published']); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3adf1e8 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "super-gear/directus", + "version": "0.1.0", + "type": "library", + "license": "MIT", + "authors": [ + { "name": "Björn Hase", "email": "me@tentakelfabrik.de" } + ], + "require": { + "php": "^7.0", + "mikecao/flight": "^1.3", + "erusev/parsedown": "^1.7" + }, + "autoload": { + "psr-4": { + "SuperGear\\": "src/" + } + } +} diff --git a/src/SuperGear/Directus/Collections/AbstractCollection.php b/src/SuperGear/Directus/Collections/AbstractCollection.php new file mode 100644 index 0000000..18cbad1 --- /dev/null +++ b/src/SuperGear/Directus/Collections/AbstractCollection.php @@ -0,0 +1,114 @@ +url = $url; + $this->token = $token; + } + + /** + * + * + * @param string $name + * @param array $parameters + * @return mixed + */ + public function findOne($name, $parameters = []) + { + // adding single to parameters + $parameters['single'] = true; + $response = $this->request($name, $this->endpoint, $parameters); + + return $response; + } + + /** + * + * + * @param string $name + * @param array $parameters + * @return mixed + * + */ + public function find($name, $parameters = []) + { + return $this->request($name, $this->endpoint, $parameters); + } + + /** + * request $endpoint + * + * @param string $name + * @param string $endpoint + * @param array $parameters + * @return mixed + * + */ + protected function request($name, $endpoint, $parameters = []) + { + // init curl and setup token + $curl = curl_init(); + + curl_setopt($curl, CURLOPT_HTTPHEADER, [ + 'Accept: application/json', + 'Content-Type: application/json', + 'Authorization: Bearer '.$this->token + ]); + + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + + $response = []; + + if (count($parameters) > 0) { + $query = http_build_query($parameters); + } + + $url = $this->url.$endpoint.'/'.$name; + + // query parameters are set, add them to url + if (isset($query)) { + $url = $url.'?'.$query; + } + + curl_setopt($curl, CURLOPT_URL, $url); + + $response = curl_exec($curl); + $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $info = curl_getinfo($curl); + + curl_close($curl); + $response = json_decode($response, true); + + return $response; + } +} diff --git a/src/SuperGear/Directus/Collections/ItemCollection.php b/src/SuperGear/Directus/Collections/ItemCollection.php new file mode 100644 index 0000000..2a75cfc --- /dev/null +++ b/src/SuperGear/Directus/Collections/ItemCollection.php @@ -0,0 +1,28 @@ +endpoint = '/items'; + + parent::__construct($url, $token); + } +} diff --git a/src/SuperGear/Directus/Controllers/DirectusControllerAbstract.php b/src/SuperGear/Directus/Controllers/DirectusControllerAbstract.php new file mode 100644 index 0000000..5049c97 --- /dev/null +++ b/src/SuperGear/Directus/Controllers/DirectusControllerAbstract.php @@ -0,0 +1,69 @@ +app->get('flight.views.path').'/'.$view.'.blade.php'); + } + + /** + * + * @param [type] $page [description] + * @param array $data [description] + * @return [type] [description] + */ + protected function render($item, $data = []) + { + $view = $this->defaultView; + + // if view isset in page and file exists + if (isset($item['data']['view'])) { + if ($this->viewExists($item['data']['view'])) { + $view = $item['data']['view']; + } else { + throw new Exception('View '.$item['data']['view'].' not exists'); + } + } else if (!$this->viewExists($view)) { + throw new Exception('View '.$view.' not exists'); + } + + $this->app->render($view, array_merge([ + 'page' => $item + ], + $data + )); + } +} diff --git a/src/SuperGear/Directus/FlightAbstract.php b/src/SuperGear/Directus/FlightAbstract.php new file mode 100644 index 0000000..679b9eb --- /dev/null +++ b/src/SuperGear/Directus/FlightAbstract.php @@ -0,0 +1,27 @@ +app = Flight::app(); + } +} diff --git a/src/SuperGear/Directus/Functions.php b/src/SuperGear/Directus/Functions.php new file mode 100644 index 0000000..44653aa --- /dev/null +++ b/src/SuperGear/Directus/Functions.php @@ -0,0 +1,12 @@ +name) { + throw new \Exception('$name is not set!'); + }; + + $this->itemCollection = new ItemCollection( + env('DIRECTUS_API_URL'), + env('DIRECTUS_API_TOKEN') + ); + } +}