Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Aug 11, 2016
2 parents d5cf44f + eac1fe2 commit b684f43
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 15 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Parable PHP Framework Changelog

### 0.8.4

__Changes__
- Added Init scripts. Check app/Init/Example.php for details on what you'd use these for.
- The Dispatcher now uses output buffering. Any content returned from controllers/closures is _appended_ to it.
- Database now always quotes all. This is due to reserved keywords in both sqlite/mysql. This should not impact any existing scripts.

__Bugfixes__
- Nested config values (such as 'app.title') now return null if they don't exist, rather than the Config freakin' out.
- Toolkit now (as originally intended) ignores Cli\App. It was confusing which App class to use. Cli\App should never be available in a web context.

### 0.8.3

__Changes__
Expand Down Expand Up @@ -33,12 +44,12 @@ __Changes__

### 0.7.2

__Bugs__
__Bugfixes__
- Haste is waste. Fixed properly now. It's a Sunday and I should've had more coffee by now ;)

### 0.7.1

__Bugs__
__Bugfixes__
- null was being passed to Tool->setRoute(), which since very recently requires an array, causing a fatal error instead of a 404. Fixed.

### 0.7.0
Expand Down
32 changes: 31 additions & 1 deletion src/Framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class App {
protected $database;

/** @var string */
protected $version = '0.8.3';
protected $version = '0.8.4';

/**
* @param \Parable\Filesystem\Path $path
Expand Down Expand Up @@ -99,6 +99,11 @@ public function run() {
$this->database->setConfig($this->config->get('database'));
}

/* See if there's an init directory defined in the config */
if ($this->config->get('initLocations')) {
$this->loadInits();
}

/* And try to match the route */
$this->hook->trigger('parable_route_match_before', $currentUrl);
$route = $this->router->matchCurrentRoute();
Expand Down Expand Up @@ -128,6 +133,31 @@ protected function loadRoutes() {
return $this;
}

protected function loadInits() {
$locations = $this->config->get('initLocations');

if (!is_array($locations)) {
return;
}

foreach ($locations as $location) {
$directory = $this->path->getDir($location);

if (!file_exists($directory)) {
continue;
}

$dirIterator = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS);
$iteratorIterator = new \RecursiveIteratorIterator($dirIterator);

foreach ($iteratorIterator as $file) {
/** @var \SplFileInfo $file */
$className = '\\Init\\' . str_replace('.' . $file->getExtension(), '', $file->getFilename());
\Parable\DI\Container::create($className);
}
}
}

/**
* Return the version number
*
Expand Down
6 changes: 5 additions & 1 deletion src/Framework/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public function __construct(

public function getNested(&$data, $keys) {
foreach ($keys as $key) {
$data = &$data[$key];
if (isset($data[$key])) {
$data = &$data[$key];
} else {
$data = null;
}
}
return $data;
}
Expand Down
33 changes: 25 additions & 8 deletions src/Framework/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,40 @@ class Dispatcher {
/** @var \Parable\Framework\View */
protected $view;

/** @var \Parable\Http\Response */
protected $response;

/**
* @param \Parable\Events\Hook $hook
* @param \Parable\Filesystem\Path $path
* @param \Parable\Framework\View $view
* @param \Parable\Http\Response $response
*/
public function __construct(
\Parable\Events\Hook $hook,
\Parable\Filesystem\Path $path,
\Parable\Framework\View $view
\Parable\Framework\View $view,
\Parable\Http\Response $response
) {
$this->hook = $hook;
$this->path = $path;
$this->view = $view;
$this->hook = $hook;
$this->path = $path;
$this->view = $view;
$this->response = $response;
}

/**
* @param \Parable\Routing\Route $route
*
* @return string
* @return $this
*/
public function dispatch(\Parable\Routing\Route $route) {
$this->hook->trigger('parable_dispatch_before', $route);
$content = '';
$controller = null;

/* Start output buffering and set $content to null */
$content = null;
$this->response->startOutputBuffer();

/* Call the relevant code */
if ($route->controller && $route->action) {
$controller = \Parable\DI\Container::get($route->controller);
Expand All @@ -60,7 +69,6 @@ public function dispatch(\Parable\Routing\Route $route) {
} else {
if ($controller) {
$reflection = new \ReflectionClass($controller);

$templateFile = $this->path->getDir('app/View/' . $reflection->getShortName() . '/' . $route->action . '.phtml');
}
}
Expand All @@ -70,8 +78,17 @@ public function dispatch(\Parable\Routing\Route $route) {
$this->view->render();
}

/* Get the output buffer content and check if $content holds anything. If so, append it to the $bufferContent */
$bufferContent = $this->response->returnOutputBuffer();
if ($content) {
$bufferContent .= $content;
}

/* And append the content to the response object */
$this->response->appendContent($bufferContent);

$this->hook->trigger('parable_dispatch_after', $route);
return $content ?: '';
return $this;
}

}
3 changes: 2 additions & 1 deletion src/Framework/Toolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ public function loadResourceMap() {

/*
* Specifically exclude all non-php files and Bootstrap, since it will attempt to register everything again
* and isn't a class anyway. Also exclude anything in the Skeleton folder.
* and isn't a class anyway.
*/
if (
$file->getFilename() === 'Bootstrap.php'
|| $file->getFilename() === 'able.php'
|| $file->getExtension() !== 'php'
|| strpos($file->getRealPath(), '/Cli/') !== false
) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Database {

/** @var bool */
protected $quoteAll = false;
protected $quoteAll = true;

/** @var null|string */
protected $type;
Expand Down
Empty file modified src/parable
100644 → 100755
Empty file.
5 changes: 4 additions & 1 deletion structure/app/Config/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public function getSortOrder() {
public function getValues() {
return [
'app' => [
'title' => 'Parable'
'title' => 'Parable'
],
'initLocations' => [
'app/Init',
],
];
}
Expand Down
38 changes: 38 additions & 0 deletions structure/app/Init/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @package Parable
* @license MIT
* @author Robin de Graaf <[email protected]>
* @copyright 2015-2016, Robin de Graaf, devvoh webdevelopment
*/

namespace Init;

class Example {

public function __construct() {
/*
* Init scripts function very simply: They need to be in Init namespace, their filename should match their
* classname, and the location of these scripts needs to be set in a Config file, using the root key
* 'initLocations'. See app/Config/App.php.
*
* They need to have a __construct() method, which will be called automatically BEFORE routing/dispatching is
* done. Init scripts are the perfect place to hook into events before anything else is done. This allows,
* for example, the following:
*
* $hook->into('parable_dispatch_before', function() use ($response, $view) {
* $response->prependContent($view->partial('app/View/Layout/header.phtml'));
* });
*
* You can do anything at all here. It can be very handy for events you want to be able to capture from the
* absolute beginning (such as 'parable_dispatch_before' mentioned above), but you can also define new events
* that you trigger later on. It's all up to you :)
*
* Init scripts are DI'd, so you can use the same constructor-based DI as in other classes.
*
* If you don't want or plan to use Init scripts, you can safely remove this directory. It won't complain if
* there's no init scripts available. You can also remove the key from the Config.
*/
}

}

0 comments on commit b684f43

Please sign in to comment.