rest-proxy.php

Simple proxy for RESTful APIs
git clone git://git.finwo.net/app/rest-proxy.php
Log | Files | Refs | README

commit 991820196d870dee500650677c04bef9b047bca3
parent 06f43f1c08ef14cb1496efd85dd274d5bbf2c3ef
Author: finwo <finwo@pm.me>
Date:   Sun, 17 Apr 2016 02:49:36 +0200

Created basic structure

Diffstat:
Mcomposer.json | 19++++++++++++++-----
Mcomposer.lock | 86++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Aconfig/autoload.php | 8++++++++
Aconfig/config.yml | 8++++++++
Asrc/Finwo/Framework/Application.php | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/Finwo/Framework/Controller.php | 17+++++++++++++++++
Asrc/Finwo/Framework/ParameterBag.php | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/Finwo/RestProxy/Controller/DefaultController.php | 12++++++++++++
Asrc/Finwo/RestProxy/RestProxy.php | 11+++++++++++
Aweb/.htaccess | 4++++
Aweb/app.php | 12++++++++++++
Aweb/app_dev.php | 8++++++++
12 files changed, 357 insertions(+), 8 deletions(-)

diff --git a/composer.json b/composer.json @@ -7,11 +7,20 @@ "email": "robin@finwo.nl" } ], - "repositories": [{ - "type": "vcs", - "url": "git@github.com:finwo/rest-client-php.git" - }], + "autoload": { + "psr-4": { + "": "src" + } + }, + "repositories": [ + { + "type": "vcs", + "url": "git@github.com:finwo/rest-client-php.git" + } + ], "require": { - "finwo/rest-client": "*@dev" + "finwo/rest-client": "*@dev", + "mustangostang/spyc": "^0.5.1", + "finwo/property-accessor": "*@dev" } } diff --git a/composer.lock b/composer.lock @@ -4,10 +4,39 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "745cd7f82dbb55d994c4a195e3f467de", - "content-hash": "62f0be611c724df32f386940c28fd301", + "hash": "2fe81bc1ad1a89bc15cec332e767fe59", + "content-hash": "ad44324d856de87133bbeba4a30ca9a0", "packages": [ { + "name": "finwo/property-accessor", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/finwo/property-accessor.git", + "reference": "e5c274de0b05fedb4f8abdafbaeec5ffaf9eed68" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/finwo/property-accessor/zipball/e5c274de0b05fedb4f8abdafbaeec5ffaf9eed68", + "reference": "e5c274de0b05fedb4f8abdafbaeec5ffaf9eed68", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Finwo\\PropertyAccessor\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "authors": [ + { + "name": "Robin Bron", + "email": "robin@finwo.nl" + } + ], + "time": "2016-04-17 00:14:13" + }, + { "name": "finwo/rest-client", "version": "dev-master", "source": { @@ -215,6 +244,56 @@ "time": "2016-04-13 19:56:01" }, { + "name": "mustangostang/spyc", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/mustangostang/spyc.git", + "reference": "699184c170688363ab82f97afb8085e39d7a1769" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mustangostang/spyc/zipball/699184c170688363ab82f97afb8085e39d7a1769", + "reference": "699184c170688363ab82f97afb8085e39d7a1769", + "shasum": "" + }, + "require": { + "php": ">=5.3.1" + }, + "require-dev": { + "phpunit/phpunit": "4.3.*@dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.5.x-dev" + } + }, + "autoload": { + "files": [ + "Spyc.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "mustangostang", + "email": "vlad.andersen@gmail.com" + } + ], + "description": "A simple YAML loader/dumper class for PHP", + "homepage": "https://github.com/mustangostang/spyc/", + "keywords": [ + "spyc", + "yaml", + "yml" + ], + "time": "2015-08-13 21:51:08" + }, + { "name": "psr/http-message", "version": "dev-master", "source": { @@ -268,7 +347,8 @@ "aliases": [], "minimum-stability": "dev", "stability-flags": { - "finwo/rest-client": 20 + "finwo/rest-client": 20, + "finwo/property-accessor": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/config/autoload.php b/config/autoload.php @@ -0,0 +1,7 @@ +<?php + +// Act like our parent folder is the document root +$_SERVER['DOCUMENT_ROOT'] = realpath(__DIR__ . '/../'); + +// Include composer's autoloader +include __DIR__ . '/../vendor/autoload.php'; +\ No newline at end of file diff --git a/config/config.yml b/config/config.yml @@ -0,0 +1,7 @@ + +# Application class +application: Finwo\RestProxy\RestProxy + +# Note of the authors +authors: + - Robin Bron <robin@finwo.nl> +\ No newline at end of file diff --git a/src/Finwo/Framework/Application.php b/src/Finwo/Framework/Application.php @@ -0,0 +1,114 @@ +<?php + +namespace Finwo\Framework; + +use Finwo\PropertyAccessor\PropertyAccessor; + +class Application +{ + /** + * @var ParameterBag + */ + protected $container; + + /** + * @var ParameterBag + */ + protected $config; + + /** + * @var PropertyAccessor + */ + protected $propertyAccessor; + + /** + * @var Application + */ + protected $app; + + /** + * @return PropertyAccessor + */ + protected function getPropertyAccessor() + { + if(is_null($this->propertyAccessor)) { + $this->propertyAccessor = new PropertyAccessor(); + } + return $this->propertyAccessor; + } + + public function __construct() + { + // Don't do anything if we're an actual application + if ( get_class($this) !== 'Finwo\\Framework\\Application' ) { + return; + } + + // Load basic container + $this->container = new ParameterBag(array( + + // Relevant about the application + 'document_root' => $_SERVER['DOCUMENT_ROOT'], + + // Relevant about the current request + 'method' => $_SERVER['REQUEST_METHOD'] + )); + + // Very useful extensions + $accessor = $this->getPropertyAccessor(); + $loader = new \Spyc(); + + // Empty array, because we want to be fancy later on + $config = array(); + + // Load configuration files + $files = glob($this->container->get('document_root') . '/config/*.yml'); + foreach ($files as $file) { + $accessor->mergeArrays( + $config, + $loader->loadFile($file) + ); + } + $this->config = new ParameterBag($config); + + // Check if we can do something useful with the application + $app = $this->getApplicationObject( $this->config->get('application') ); + if ($app === false) { + throw new \Exception('Application does not exist!'); + } + + // Save the app for later use + $this->app = $app; + } + + protected function getApplicationObject( $name ) + { + // Try name directly + if (class_exists($name)) { + return new $name(); + } + + // Try name with ending "application" + $tryname = $name . '\\Application'; + if (class_exists($tryname)) { + return new $tryname(); + } + + // Try name with double ending + $tryname = explode('\\', $name); + $last = array_pop($tryname); + array_push($tryname, $last); + array_push($tryname, $last); + $tryname = implode('\\', $tryname); + if (class_exists($tryname)) { + return new $tryname(); + } + + return false; + } + + public function launch() + { + die("I've launched"); + } +} +\ No newline at end of file diff --git a/src/Finwo/Framework/Controller.php b/src/Finwo/Framework/Controller.php @@ -0,0 +1,16 @@ +<?php + +namespace Finwo\Framework; + +class Controller +{ + /** + * @var ParameterBag + */ + protected $container; + + public function __construct(ParameterBag $container) + { + $this->container = $container; + } +} +\ No newline at end of file diff --git a/src/Finwo/Framework/ParameterBag.php b/src/Finwo/Framework/ParameterBag.php @@ -0,0 +1,64 @@ +<?php + +namespace Finwo\Framework; + +use Finwo\PropertyAccessor\PropertyAccessor; + +class ParameterBag +{ + /** + * Parameters + * Contain the actual stuff + * + * @var array + */ + protected $parameters = array(); + + /** + * @var PropertyAccessor + */ + protected $accessor; + + /** + * @return PropertyAccessor + */ + protected function getAccessor() + { + if ( !($this->accessor instanceof PropertyAccessor) ) { + $this->accessor = new PropertyAccessor(); + } + return $this->accessor; + } + + /** + * ParameterBag constructor. + * @param array $data + */ + public function __construct($data = array()) + { + // Insert data if provided + if (is_array($data)) { + foreach ($data as $key => $value) { + $this->set($key, $value); + } + } + } + + public function toArray() + { + return $this->parameters; + } + + public function set($key, $value) + { + $acc = $this->getAccessor(); + $acc->set($this->parameters, $key, $value); + return $this; + } + + public function get($key) + { + $acc = $this->getAccessor(); + return $acc->get($this->parameters, $key); + } +} +\ No newline at end of file diff --git a/src/Finwo/RestProxy/Controller/DefaultController.php b/src/Finwo/RestProxy/Controller/DefaultController.php @@ -0,0 +1,11 @@ +<?php + +namespace Finwo\RestProxy\Controller; + +class DefaultController +{ + public function indexAction() + { + return 'Hello World'; + } +} +\ No newline at end of file diff --git a/src/Finwo/RestProxy/RestProxy.php b/src/Finwo/RestProxy/RestProxy.php @@ -0,0 +1,10 @@ +<?php + +namespace Finwo\RestProxy; + +use Finwo\Framework\Application; + +class RestProxy extends Application +{ + +} +\ No newline at end of file diff --git a/web/.htaccess b/web/.htaccess @@ -0,0 +1,3 @@ +# We'll route everything to our app +RewriteEngine on +RewriteRule ^(.*)$ /app.php [NC,L,QSA] +\ No newline at end of file diff --git a/web/app.php b/web/app.php @@ -0,0 +1,11 @@ +<?php + +// Include autoloader +include __DIR__ . '/../config/autoload.php'; + +// Uses +use Finwo\Framework\Application; + +// Start the application +$app = new Application(); +$app->launch(); +\ No newline at end of file diff --git a/web/app_dev.php b/web/app_dev.php @@ -0,0 +1,7 @@ +<?php + +ini_set('display_errors', 1); +ini_set('display_startup_errors', 1); +error_reporting(E_ALL); + +include "app.php"; +\ No newline at end of file