Browse Source

adding

develop
Björn 5 years ago
commit
9a5b30c4e3
10 changed files with 416 additions and 0 deletions
  1. +13
    -0
      README.md
  2. +19
    -0
      composer.json
  3. +114
    -0
      src/SuperGear/Directus/Collections/AbstractCollection.php
  4. +28
    -0
      src/SuperGear/Directus/Collections/ItemCollection.php
  5. +69
    -0
      src/SuperGear/Directus/Controllers/DirectusControllerAbstract.php
  6. +27
    -0
      src/SuperGear/Directus/FlightAbstract.php
  7. +12
    -0
      src/SuperGear/Directus/Functions.php
  8. +47
    -0
      src/SuperGear/Directus/Helpers/MarkdownHelper.php
  9. +49
    -0
      src/SuperGear/Directus/Repositories/Manager.php
  10. +38
    -0
      src/SuperGear/Directus/Repositories/RepositoryAbstract.php

+ 13
- 0
README.md View File

@ -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']);
```

+ 19
- 0
composer.json View File

@ -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/"
}
}
}

+ 114
- 0
src/SuperGear/Directus/Collections/AbstractCollection.php View File

@ -0,0 +1,114 @@
<?php
namespace SuperGear\Directus\Collections;
/**
* send request with curl to directus instance
*
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitlab.tentakelfabrik.de/super-gear/directus GitHub Repository
*
*/
class AbstractCollection
{
/** url */
private $url = NULL;
/** token */
private $token = NULL;
/** curl */
private $curl = NULL;
/** endpoint */
protected $endpoint = NULL;
/**
*
* @param string $url
* @param string $token
*/
public function __construct($url, $token)
{
$this->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;
}
}

+ 28
- 0
src/SuperGear/Directus/Collections/ItemCollection.php View File

@ -0,0 +1,28 @@
<?php
namespace SuperGear\Directus\Collections;
/**
* endpoint "items" for directus
*
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitlab.tentakelfabrik.de/super-gear/directus GitHub Repository
*
*/
class ItemCollection extends AbstractCollection
{
/**
*
* @param string $url
* @param string $token
*/
public function __construct($url, $token)
{
// adding endpoint for items
$this->endpoint = '/items';
parent::__construct($url, $token);
}
}

+ 69
- 0
src/SuperGear/Directus/Controllers/DirectusControllerAbstract.php View File

@ -0,0 +1,69 @@
<?php
namespace SuperGear\Directus\Controllers;
use SuperGear\FlightAbstract;
use Exception;
/**
* abstract controller to handle views and response from directus
*
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitlab.tentakelfabrik.de/super-gear/directus GitHub Repository
*
*/
abstract class ControllerAbstract extends FlightAbstract
{
protected $defaultView = NULL;
/**
* if item not found
*
* @param string $page
* @return boolean
*/
protected function notFound($item)
{
return (!$item || ($item && isset($item['error']) && $item['error']['code'] === 203));
}
/**
*
* @param string $view
* @return boolean
*/
protected function viewExists($view)
{
return file_exists($this->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
));
}
}

+ 27
- 0
src/SuperGear/Directus/FlightAbstract.php View File

@ -0,0 +1,27 @@
<?php
namespace SuperGear\Directus;
use Flight;
/**
* abstract FlightAbstract get instance of flight engine
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitlab.tentakelfabrik.de/super-gear/super-gear-directus GitHub Repository
*/
abstract class FlightAbstract
{
/** object of flight */
protected $app;
/**
* getting object of flight
*
*/
public function __construct()
{
$this->app = Flight::app();
}
}

+ 12
- 0
src/SuperGear/Directus/Functions.php View File

@ -0,0 +1,12 @@
<?php
/**
* fake function for blade @inject
*
* @param string $class
* @return object
*/
function app($class)
{
return new $class();
}

+ 47
- 0
src/SuperGear/Directus/Helpers/MarkdownHelper.php View File

@ -0,0 +1,47 @@
<?php
namespace SuperGear\Directus\Helpers;
use Parsedown;
/**
* Helper to extend Parsedown
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitlab.tentakelfabrik.de/super-gear/directus GitHub Repository
*/
class MarkdownHelper extends Parsedown
{
/**
*
* @var string
*/
const EXTERNAL_LINK = "/^(http|https):\/\//";
const INNER_BRACKETS = "/\){(.*?)\}/";
const TARGET_BLANK = "_blank";
const DIVIDER_METHOD = ':';
const DIVIDER_SIZES = 'x';
/**
* extend default function, if a link has http|https in url,
* then handle this link as external and set target to _blank
*
* @param array $excerpt
* @return array
*/
protected function inlineLink($excerpt)
{
$result = parent::inlineLink($excerpt);
if (is_array($result)) {
if (isset($result['element']['attributes'])) {
if (preg_match(self::EXTERNAL_LINK, $result['element']['attributes']['href'])) {
$result['element']['attributes']['target'] = self::TARGET_BLANK;
}
}
return $result;
}
}
}

+ 49
- 0
src/SuperGear/Directus/Repositories/Manager.php View File

@ -0,0 +1,49 @@
<?php
namespace SuperGear\Directus\Repositories;
use Exception;
/**
* Manager Class to create Repository Objects that
* are located in App\Repositories\
*
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitlab.tentakelfabrik.de/super-gear/super-gear-directus GitHub Repository
*/
class Manager
{
/**
* naming of Repository
* @var string
*/
const NAMESPACE = 'App\Repositories\\';
/**
* naming of Repository
* @var string
*/
const REPOSITORY_SUFFIX = 'Repository';
/**
* getting repository object
*
* @param string $repositoryClass
* @return AbstractRepository
*/
public static function get($repositoryName)
{
$repositoryClass = self::NAMESPACE.$repositoryName.self::REPOSITORY_SUFFIX;
if (!class_exists($repositoryClass)) {
throw new Exception('Repository Class '.$repositoryClass.' not exists!');
}
// create respository object
$repository = new $repositoryClass();
return $repository;
}
}

+ 38
- 0
src/SuperGear/Directus/Repositories/RepositoryAbstract.php View File

@ -0,0 +1,38 @@
<?php
namespace SuperGear\Repositories;
use SuperGear\DirectusClient\Collections\ItemCollection;
/**
* Abstract Repository to wrap ItemCollection
*
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitlab.tentakelfabrik.de/super-gear/super-gear-directus GitHub Repository
*/
abstract class RepositoryAbstract
{
/** name of the collection */
protected $name;
/** client for itemCollection */
protected $itemCollection;
/**
*
*
*/
public function __construct()
{
if (!$this->name) {
throw new \Exception('$name is not set!');
};
$this->itemCollection = new ItemCollection(
env('DIRECTUS_API_URL'),
env('DIRECTUS_API_TOKEN')
);
}
}

Loading…
Cancel
Save