Skip to content

Commit

Permalink
Merge pull request #13 from benrowe/releases/build-3
Browse files Browse the repository at this point in the history
Releases/build 3
  • Loading branch information
benrowe committed Dec 27, 2016
2 parents bdc2d8d + e6132e1 commit 2d15286
Show file tree
Hide file tree
Showing 16 changed files with 1,869 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cache:

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev
- composer install --prefer-dist --no-interaction

script:
- vendor/bin/phpunit
9 changes: 9 additions & 0 deletions app/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Exceptions;

use Exception;

class HttpException extends Exception
{
}
94 changes: 94 additions & 0 deletions app/Http/Controllers/AbstractController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Http\Controllers;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;

/**
* Class AbstractController
*
* @package App\Http\Controllers
*/
abstract class AbstractController
{
private $request;
private $response;

/**
* AbstractController constructor.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*/
public function __construct(ServerRequestInterface $request, ResponseInterface $response)
{
$this->request = $request;
$this->response = $response;
}

/**
* Get the request (get/post) param from the request handler
*
* @param string $key
* @param mixed $default
* @return mixed
*/
protected function getRequestParam($key, $default = null)
{
$params = $this->request->getQueryParams();
if (array_key_exists($key, $params)) {
return $params[$key];
}

return $default;
}

/**
* Verify the existence of a request param
*
* @param string $key
* @return bool
*/
protected function hasRequestParam($key)
{
$params = $this->request->getQueryParams();

return array_key_exists($key, $params);
}

/**
* Allow a controllers actions to be called statically be invoking a new instance of the controller
*
* @param string $method
* @param array $params
* @return string|array|ResponseInterface
*/
public static function __callStatic($method, $params)
{
$methodName = 'action'.ucfirst($method);

if (method_exists(static::class, $methodName)) {
$callback = [new static(array_shift($params), array_shift($params)), $methodName];

return call_user_func_array($callback, $params[0] ?: []);
}
throw new RuntimeException(sprintf("Unknown static method %s::%s()", static::class, $method));
}

/**
* Load the requested view add write it to the body
*
* @param string $path
* @param array $params
* @return \Psr\Http\Message\StreamInterface
*/
protected function view($path, array $params = [])
{
$view = \App\app()->get('view')->view();
$rendered = $view->make($path, $params)->render();
$this->response->getBody()->write($rendered);
return $this->response;
}
}
19 changes: 19 additions & 0 deletions app/Http/Controllers/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers;

/**
* Class DefaultController
*
* @package App\Http\Controllers
*/
class DefaultController extends AbstractController
{
/**
* Load the application homepage
*/
public function actionIndex()
{

}
}
13 changes: 5 additions & 8 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?php

use App\Http\Controllers\ArtistController;
use App\Http\Controllers\DefaultController;

/**
* @var \League\Route\RouteCollection $routes
* @var \League\Route\RouteCollection $route
*/
$route->addRoute('GET', '/', function() {
return 'asdf';
});

$route->addRoute('GET', '/ajax', function() {
return ['foo' => 'bar'];
});
$route->addRoute('GET', '/', DefaultController::class.'::index');
54 changes: 39 additions & 15 deletions app/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

namespace App\Models;

use App\Exceptions\InvalidParamException;

class Country
{
const DATA_FILE = 'resources/data/countries.json';
/**
* Raw country data
*
* @todo move this into persistant data store
* @todo move this into persistent data store
* @todo provide full list of countries
* @var array
*/
private static $data = [
'au' => [
'id' => 'au',
'name' => 'Australia'
]
];
private static $data;

private $attributes = [];

/**
* Country constructor.
*
* @param null|array $attributes
*/
public function __construct($attributes = null)
{
if (is_array($attributes)) {
Expand All @@ -28,37 +31,58 @@ public function __construct($attributes = null)
}

/**
* [__get description]
* @param [type] $key [description]
* @return [type] [description]
* Access attributes as properties
*
* @param string $key
* @return mixed
* @throws InvalidParamException
*/
public function __get($key)
{
if (array_key_exists($key, $this->attributes)) {
return $this->attributes[$key];
}
throw new \App\Exceptions\InvalidParamException(sprintf("Unknown property %s", $key));
throw new InvalidParamException(sprintf("Unknown property %s", $key));
}

/**
* @return Country[]
*/
public static function all()
{
$result = [];
foreach (self::$data as $key => $country) {
foreach (self::loadRawData() as $key => $country) {
$result[$key] = new self($country);
}
return $result;
}

/**
* [findById description]
* Find a specific country by its ISO id
*
* @param string $ref country identifier
* @return Country|null
*/
public static function findById($ref)
{
if (isset(self::$data[$ref])) {
return new self(self::$data[$ref]);
$data = self::loadRawData();
$ref = strtoupper($ref);
if (isset($data[$ref])) {
return new self($data[$ref]);
}
return null;
}

/**
* Load and return the raw country data
*/
private static function loadRawData()
{
if (!is_array(self::$data)) {
$path = \App\path(self::DATA_FILE);
$content = file_get_contents($path);
self::$data = json_decode($content, true);
}
return self::$data;
}
}
29 changes: 28 additions & 1 deletion app/Support/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
class Container
{
/**
* @var Container
*/
private static $instance;

/**
* @var DiContainer
*/
Expand All @@ -40,12 +45,34 @@ class Container
*/
public function __construct(DiContainer $di, $path)
{
// auto wire our calls
// auto wire our DI calls
$di->delegate(
new ReflectionContainer
);
$this->dependency = $di;
$this->pathRoot = rtrim($path, '/').'/';

self::$instance = $this;
}

/**
* Get the instance of the container
*
* @return Container
*/
public static function instance()
{
return self::$instance;
}

/**
* The root directory for the application
*
* @return string
*/
public function root()
{
return $this->pathRoot;
}

/**
Expand Down
31 changes: 30 additions & 1 deletion app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace App;

use App\Support\Container;

/**
* Contains quick global helper methods
* These are simple wrappers to more complex objects that need to be accessed
* frequently within the application
*/

/**
Expand All @@ -24,4 +28,29 @@ function array_get(&$data, $path)
}

return $data;
}
}

/**
* Retrieve an instance of the application Container
*
* @return Container
*/
function app()
{
return Container::instance();
}

/**
* Get the absolute path to the resource, based on the root of the application
*
* @param string $relPath
* @return string
*/
function path($relPath = null)
{
$path = app()->root();
if ($relPath) {
$path .= trim($relPath, ' /');
}
return $path;
}
15 changes: 11 additions & 4 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Support\Container;
use League\Container\Container as DiContainer;
use Philo\Blade\Blade;

require_once __DIR__.'/../vendor/autoload.php';

Expand All @@ -14,16 +15,22 @@

$app = new Container(new DiContainer(), realpath(__DIR__.'/../'));

//region application services
//region low level application services
$app->add('request', function () {
return Symfony\Component\HttpFoundation\Request::createFromGlobals();
});
$app->share('response', \Symfony\Component\HttpFoundation\Response::class);;
$app->share('response', \Symfony\Component\HttpFoundation\Response::class);
$app->share('emitter', Zend\Diactoros\Response\SapiEmitter::class);

$app->share('config', function() use ($app) {
$app->share('config', function () use ($app) {
return new \Config\Repository(new \Config\Loader\FileLoader(\App\path('config')), getenv('APP_ENV'));
});

$app->share('view', function () use ($app) {
return new Blade($app->get('config')->get('view.path'), $app->get('config')->get('view.cache'));
});
//endregion

return new \Config\Repository(new \Config\Loader\FileLoader(__DIR__.'/config'), getenv('APP_ENV'));
});
//endregion

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"symfony/http-foundation": "^3.2",
"symfony/psr-http-message-bridge": "^1.0",
"zendframework/zend-diactoros": "^1.3",
"guzzlehttp/guzzle": "^6.2"
"guzzlehttp/guzzle": "^6.2",
"philo/laravel-blade": "^3.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
Expand Down
Loading

0 comments on commit 2d15286

Please sign in to comment.