Skip to content

Commit

Permalink
Continued integration into HTTP Kernel environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Manderson committed Sep 15, 2014
1 parent e289016 commit 832baf2
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 35 deletions.
94 changes: 63 additions & 31 deletions src/Action/ActionKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ActionKernel implements HttpKernelInterface
*/
protected $processor;

protected $init = false;

public function __construct(\Silex\Application $application)
{
$this->application = $application;
Expand Down Expand Up @@ -82,37 +84,65 @@ protected function handleException(\Exception $e, Request $request, $type)
*/
protected function process(Request $request, Response $response)
{
// Get the module config
$this->init();
$this->getRequestProcessor($this->getModuleConfig($request))->process($request, $response);
}


/**
* @param Request $request
* @return \Phruts\Config\ModuleConfig
* Initialise the module configurations
* @throws \Phruts\Exception
*/
protected function getModuleConfig(Request $request)
{
$this->initModulePrefixes();
// Check we have a module prefix/match
// Digest the modules configs
// Obtain the matching module config
// Initialise the module config (e.g. all the datasource/plugins)
// Return module config
protected function init() {
// Initialise once
if($this->init) return;

$prefixes = array();

// Obtain the module config provider (implements caching, etc)
if(empty($this->application[\Phruts\Util\Globals::MODULE_CONFIG_PROVIDER])) {
throw new \Phruts\Exception('Unable to locate the module config provider service');
}
$moduleConfigProvider = $this->application[\Phruts\Util\Globals::MODULE_CONFIG_PROVIDER];

// Get the configured modules
foreach($this->application[\Phruts\Util\Globals::ACTION_KERNEL_CONFIG] as $prefixParam => $config) {
if(strlen($prefixParam) > 7 && substr($prefixParam, 0, 7) == 'config/') continue;
$prefix = substr($prefixParam, 7);

// Get the module config
$moduleConfig = $moduleConfigProvider->getModuleConfig($prefix, $config);
if(empty($moduleConfig)) {
throw new \Phruts\Exception('Unable to locate the module config for ' . $prefix);
}
$this->application[\Phruts\Util\Globals::MODULE_KEY . $prefix] = $moduleConfig;

// Initialise the module config
$this->initModuleMessageResources($moduleConfig);
$this->initModuleDataSources($moduleConfig);
$this->initModulePlugIns($moduleConfig);

$prefixes[] = $prefix;
}
$this->application[\Phruts\Util\Globals::PREFIXES_KEY] = $prefixes;

$this->init = true;
return;
}

/**
* Add the modules (not including the default)
* @return void
* @param Request $request
* @return \Phruts\Config\ModuleConfig
*/
public function initModulePrefixes()
{
$prefixes = array();
foreach($this->application[\Phruts\Util\Globals::CONFIG] as $prefix => $config) {
if(strlen($prefix) > 7) continue;
$prefixes[] = substr($prefix, 7);
protected function getModuleConfig(Request $request) {
$config = $request->attributes->get(\Phruts\Util\Globals::MODULE_KEY);
if (empty($config)) {
if(empty($this->application[\Phruts\Util\Globals::MODULE_KEY])) {
throw new \Phruts\Exception('Can not locate the default module for this request');
}
$config = $this->application[\Phruts\Util\Globals::MODULE_KEY];
}
$this->application['phruts.module_prefixes'] = $prefixes;
return $config;
}


Expand All @@ -132,28 +162,30 @@ protected function getRequestProcessor(\Phruts\Config\ModuleConfig $config)
if (empty($processor)) {
try {
$processorClass = $config->getControllerConfig()->getProcessorClass();
$processor = \Phruts\Util\ClassLoader::newInstance($processorClass, '\Phruts\RequestProcessor');

// TODO: If supports DIC injection...
$processor = \Phruts\Util\ClassLoader::newInstance($processorClass, '\Phruts\Action\RequestProcessor');

$processor->init($this, $config);
$this->application[$key] = $processor;
return $this->application[$key];
} catch (\Exception $e) {
throw new \Exception('Cannot initialize RequestProcessor of class ' . $processorClass . ': ' . $e->getMessage());
}
$processor->init($this, $config);
$this->application[$key] = $processor;
}

return $this->application[$key];
}

protected function initPlugIns()
protected function initModuleMessageResources($moduleConfig)
{

// TODO:
}

protected function initDataSources()
protected function initModuleDataSources($moduleConfig)
{
// TODO:
}

protected function initModulePlugIns($moduleConfig)
{
// TODO:
}

/**
Expand All @@ -171,7 +203,7 @@ public function getInternal() {
*/
public function getDataSource(Request $request, $key)
{

// TODO:
}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/PhrutsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Phruts;

use Phruts\Util\ModuleProvider\FileCacheModuleProvider;

/**
* Class PhrutsServiceProvider
* @package Phruts
Expand All @@ -17,14 +19,21 @@ class PhrutsServiceProvider implements \Silex\ServiceProviderInterface
*/
public function register(\Silex\Application $app)
{




// Register our action server
$app[\Phruts\Util\Globals::ACTION_KERNEL] = $app->share(function() use ($app) {
return new \Phruts\Action\ActionKernel($app);
});

// Register our digester for when we need it
$app[\Phruts\Util\Globals::DIGESTER] = $app->share(function() use ($app) {
return new \Phigester\Digester();
});

$app[\Phruts\Util\Globals::MODULE_CONFIG_PROVIDER] = $app->share(function() use ($app) {
$provider = new FileCacheModuleProvider($app);
$provider->setCachePath(getcwd() . '/../app/cache/');
return $provider;
});
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Util/Globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,21 @@ class Globals
* Action Kernel
*/
const ACTION_KERNEL = 'phruts.action_kernel';

/**
* Module Config Paths
*/
const ACTION_KERNEL_CONFIG = 'phruts.action_kernel_config';

/**
* The provider for the module config
* Allows the service to be replaced with alternative module config
* strategies (e.g. non-file)
*/
const MODULE_CONFIG_PROVIDER = 'phruts.module_config_provider';

/**
*
*/
const DIGESTER = 'phruts.digester';
}
81 changes: 81 additions & 0 deletions src/Util/ModuleProvider/FileCacheModuleProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
namespace Phruts\Util\ModuleProvider;

use Phruts\Config\ModuleConfig;
use Phruts\Utils\ModuleProviderInterface;
use Phigester\Digester;

class FileCacheModuleProvider implements ModuleProviderInterface
{
/**
* @var Digester
*/
protected $application;

protected $cachePath;

public function __construct(\Silex\Application $application)
{
$this->application = $application;
}

public function getModuleConfig($prefix = '', $config)
{
$rebuild = false;

// Determine if a cache file is present
$cacheFile = $this->cachePath . '/' . $prefix . '.data';
$mtime = filemtime($cacheFile);

// Check the ages of the config paths against the age of the cache file
if($mtime !== false) {
foreach($configPaths as $path) {
$cmtime = filemtime($path);
if($cmtime == false) {
throw new \Phruts\Exception('Unable to locate the specified Phruts configuration file');
}
if($cmtime > $mtime) {
$rebuild = true;
break;
}
}
} else {
// We couldn't find the modified time on the cache file
$rebuild = true;
}

// Get the module config
if($rebuild == true) {
// (re)Build the cache
if(!is_writable($cacheFile)) {
throw new \Phruts\Exception('Unable to write to the cache');
}

// Digest the config
$moduleConfig = new ModuleConfig($prefix);
$digester = $this->application[\Phruts\Util\Globals::DIGESTER];
if(empty($digester)) {
throw new \Phruts\Exception('Digester is not present in the application, unable to process the configruation file');
}
$digester->push($moduleConfig);
foreach($configPaths as $path) {
$digester->parse($path);
}

// Write out the cache
file_put_contents($cacheFile, serialize($moduleConfig));
} else {
// Obtain from the cache
$serialised = file_get_contents($cacheFile);
$moduleConfig = unserialize($serialised);
}

return $moduleConfig;
}

public function setCachePath($path)
{
$this->cachePath = $path;
}
}

7 changes: 7 additions & 0 deletions src/Util/ModuleProvider/ModuleProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Phruts\Utils;

interface ModuleProviderInterface
{
public function getModuleConfig($prefix = '', $config);
}

0 comments on commit 832baf2

Please sign in to comment.