Skip to content

Commit

Permalink
update code/tests and mocking to phpunit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Dec 12, 2017
1 parent 307b413 commit 3d5e2ed
Show file tree
Hide file tree
Showing 64 changed files with 2,153 additions and 1,498 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Ignore configuration files
api/config.php
api/configuration.php

documents

Expand Down
36 changes: 5 additions & 31 deletions api/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,8 @@
// Composer Autoloader
$loader = require __DIR__ . '/../vendor/autoload.php';

// Constants
define('API_PATH', __DIR__);
define('ROOT_PATH', realpath(API_PATH . '/../'));
define('LOG_PATH', API_PATH . '/logs');

// TODO: REMOVE THIS, IT'S TEMPORARY
// BASE_PATH will be base path of directus relative to the host
define('BASE_PATH', ROOT_PATH);

$configFilePath = API_PATH . '/config.php';

// Creates a simple endpoint to test the server rewriting
// If the server responds "pong" it means the rewriting works
if (!file_exists($configFilePath)) {
return create_ping_server();
}

$appConfig = require $configFilePath;
/** @var \Directus\Application\Application $app */
$app = require __DIR__ . '/bootstrap/application.php';

// =============================================================================
// Error reporting
Expand All @@ -35,7 +19,7 @@

$errorReporting = E_ALL;
$displayErrors = 1;
if (\Directus\Util\ArrayUtils::get($appConfig, 'env', 'development') === 'production') {
if ($app->getConfig()->get('env', 'development') === 'production') {
$displayErrors = $errorReporting = 0;
}

Expand All @@ -45,24 +29,14 @@
// =============================================================================
// Timezone
// =============================================================================
date_default_timezone_set(\Directus\Util\ArrayUtils::get($appConfig, 'timezone', 'America/New_York'));

$app = new \Directus\Application\Application($appConfig);
date_default_timezone_set($app->getConfig()->get('timezone', 'America/New_York'));

$container = $app->getContainer();

// =============================================================================
// Load registered hooks
// =============================================================================
$config = $container->get('config');
if ($config->has('hooks')) {
load_registered_hooks($config->get('hooks'), false);
}

if ($config->get('filters')) {
// set seconds parameter "true" to add as filters
load_registered_hooks($config->get('filters'), true);
}
load_registered_hooks($app);

$app->getContainer()->get('hook_emitter')->run('application.boot', $app);

Expand Down
11 changes: 11 additions & 0 deletions api/bootstrap/application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$configFilePath = API_PATH . '/config.php';

// Creates a simple endpoint to test the server rewriting
// If the server responds "pong" it means the rewriting works
if (!file_exists($configFilePath)) {
return create_ping_server();
}

return new \Directus\Application\Application(realpath(__DIR__ . '/../../'), require $configFilePath);
209 changes: 85 additions & 124 deletions api/core/Directus/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,30 @@ class Application extends App

];

/**
* @var string
*/
protected $basePath;

/**
* @var \Closure
*/
protected $checkRequirementsFunction;

protected static $instance = null;

/**
* @inheritdoc
* Application constructor.
*
* @param string $basePath
* @param array $container
*/
public function __construct(array $container = [])
public function __construct($basePath, array $container = [])
{
$this->setBasePath($basePath);

if (is_array($container)) {
$container = $this->createConfig($container);

$container = new Container($container);
}
$container = $this->createConfig($container);
$container = new Container($container);

static::$instance = $this;

Expand All @@ -65,6 +76,55 @@ public static function getInstance()
return static::$instance;
}

/**
* Sets the application base path
*
* @param $path
*/
public function setBasePath($path)
{
$this->basePath = rtrim($path, '/');

$this->updatePaths();
}

protected function updatePaths()
{
$container = $this->getContainer();
$container['path_base'] = $this->basePath;
$container['path_api'] = realpath($this->basePath . '/api');
$container['path_log'] = realpath($container['path_api'] . '/logs');

// define('BOOTSTRAP_PATH', __DIR__);
// define('API_PATH', realpath(__DIR__ . '/../'));
// define('ROOT_PATH', realpath(API_PATH . '/../'));
// define('LOG_PATH', API_PATH . '/logs');

// TODO: REMOVE THIS, IT'S TEMPORARY
// BASE_PATH will be base path of directus relative to the host
// define('BASE_PATH', ROOT_PATH);
}

/**
* Application configuration object
*
* @return Config
*/
public function getConfig()
{
return $this->getContainer()->get('config');
}

/**
* Sets the function that checks the requirements
*
* @param \Closure $function
*/
public function setCheckRequirementsFunction(\Closure $function)
{
$this->checkRequirementsFunction = $function;
}

/**
* Creates the user configuration based on its configuration
*
Expand All @@ -77,13 +137,10 @@ public static function getInstance()
protected function createConfig(array $appConfig)
{
return [
'settings' => ArrayUtils::get($appConfig, 'settings', []),
'settings' => ArrayUtils::pull($appConfig, 'settings', []),
'config' => function () use ($appConfig) {
return new Config($appConfig);
},
'api_path' => API_PATH,
'root_path' => ROOT_PATH,
'log_path' => LOG_PATH
}
];
}

Expand All @@ -101,15 +158,13 @@ public function run($silent = false)

public function boot()
{
if ($this->booted) {
return;
}

// foreach ($this->providers as $provider) {
// $provider->boot($this);
// }
if (!$this->booted) {
// foreach ($this->providers as $provider) {
// $provider->boot($this);
// }

$this->booted = true;
$this->booted = true;
}
}

/**
Expand All @@ -122,23 +177,6 @@ public function getVersion()
return static::DIRECTUS_VERSION;
}

public function response()
{
$response = parent::response();

if (func_num_args() > 0) {
$data = ArrayUtils::get(func_get_args(), 0);
$options = ArrayUtils::get(func_get_args(), 1);

$data = $this->triggerResponseFilter($data, (array) $options);

// @TODO: Response will support xml
$response->setBody(json_encode($data));
}

return $response;
}

/**
* Trigger Filter by name with its payload
*
Expand All @@ -149,7 +187,7 @@ public function response()
*/
public function triggerFilter($name, $payload)
{
return $this->getContainer()->get('hookEmitter')->apply($name, $payload);
return $this->getContainer()->get('hook_emitter')->apply($name, $payload);
}

/**
Expand All @@ -168,99 +206,22 @@ public function triggerAction($name, $params = [])

array_unshift($params, $name);

call_user_func_array([$this->getContainer()->get('hookEmitter'), 'run'], $params);
}

public function onMissingRequirements(Callable $callback)
{
$errors = get_missing_requirements();

if ($errors) {
$callback($errors);
exit; // Stop
}
call_user_func_array([$this->getContainer()->get('hook_emitter'), 'run'], $params);
}

/**
* Trigger a response filter
* Calls the given callable if there are missing requirements
*
* @param $data
* @param array $options
*
* @return mixed
* @param callable $callback
*/
protected function triggerResponseFilter($data, array $options)
public function onMissingRequirements(Callable $callback)
{
$uriParts = explode('/', trim($this->request()->getResourceUri(), '/'));
$apiVersion = (float) array_shift($uriParts);

if ($apiVersion > 1) {
$meta = ArrayUtils::get($data, 'meta');
} else {
$meta = [
'type' => array_key_exists('rows', $data) ? 'collection' : 'item',
'table' => ArrayUtils::get($options, 'table')
];
}
$errors = $this->checkRequirementsFunction
? call_user_func($this->checkRequirementsFunction)
: get_missing_requirements();

$attributes = [
'meta' => $meta,
'apiVersion' => $apiVersion,
'request' => [
'path' => $this->request()->getResourceUri(),
'method' => $this->request()->getMethod()
]
];

$payload = new Payload($data, $attributes);

$method = strtolower($this->request()->getMethod());
$payload = $this->triggerFilter('response', $payload);
$payload = $this->triggerFilter('response.' . $method, $payload);
if ($meta['table']) {
$payload = $this->triggerFilter('response.' . $meta['table'], $payload);
$payload = $this->triggerFilter(sprintf('response.%s.%s',
$meta['table'],
$method
), $payload);
}

return $payload->getData();
}

protected function guessOutputFormat()
{
$app = $this;
$outputFormat = 'json';
$requestUri = $app->request->getResourceUri();

if ($this->requestHasOutputFormat()) {
$outputFormat = $this->getOutputFormat();
// TODO: create a replace last/first occurrence
$pos = strrpos($requestUri, '.' . $outputFormat);
$newRequestUri = substr_replace($requestUri, '', $pos, strlen('.' . $outputFormat));
$env = $app->environment();
$env['PATH_INFO'] = $newRequestUri;
if ($errors) {
$callback($errors);
}

return $outputFormat;
}

protected function requestHasOutputFormat()
{
$matches = $this->getOutputFormat();

return $matches ? true : false;
}

protected function getOutputFormat()
{
$requestUri = trim($this->request->getResourceUri(), '/');

// @TODO: create a startsWith and endsWith using regex
$matches = [];
preg_match('#\.[\w]+$#', $requestUri, $matches);

return isset($matches[0]) ? substr($matches[0], 1) : null;
}
}
Loading

0 comments on commit 3d5e2ed

Please sign in to comment.