Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the Grav classes load and process methods to follow clean coding standards #745

Merged
merged 7 commits into from
Apr 13, 2016
398 changes: 149 additions & 249 deletions system/src/Grav/Common/Grav.php

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions system/src/Grav/Common/Processors/AssetsProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Grav\Common\Processors;

class AssetsProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'assets';
public $title = 'Assets';

public function process() {
$this->container['assets']->init();
$this->container->fireEvent('onAssetsInitialized');
}

}
14 changes: 14 additions & 0 deletions system/src/Grav/Common/Processors/ConfigurationProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Grav\Common\Processors;

class ConfigurationProcessor extends ProcessorBase implements ProcessorInterface {

public $id = '_config';
public $title = 'Configuration';

public function process() {
$this->container['config']->init();
return $this->container['plugins']->setup();
}

}
13 changes: 13 additions & 0 deletions system/src/Grav/Common/Processors/DebuggerAssetsProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Processors;

class DebuggerAssetsProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'debugger_assets';
public $title = 'Debugger Assets';

public function process() {
$this->container['debugger']->addAssets();
}

}
13 changes: 13 additions & 0 deletions system/src/Grav/Common/Processors/DebuggerInitProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Processors;

class DebuggerInitProcessor extends ProcessorBase implements ProcessorInterface {

public $id = '_debugger';
public $title = 'Init Debugger';

public function process() {
$this->container['debugger']->init();
}

}
13 changes: 13 additions & 0 deletions system/src/Grav/Common/Processors/ErrorsProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Processors;

class ErrorsProcessor extends ProcessorBase implements ProcessorInterface {

public $id = '_errors';
public $title = 'Error Handlers Reset';

public function process() {
$this->container['errors']->resetHandlers();
}

}
33 changes: 33 additions & 0 deletions system/src/Grav/Common/Processors/InitializeProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Grav\Common\Processors;

class InitializeProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'init';
public $title = 'Initialize';

public function process() {
$this->container['config']->debug();

// Use output buffering to prevent headers from being sent too early.
ob_start();
if ($this->container['config']->get('system.cache.gzip')) {
// Enable zip/deflate with a fallback in case of if browser does not support compressing.
if (!ob_start("ob_gzhandler")) {
ob_start();
}
}

// Initialize the timezone.
if ($this->container['config']->get('system.timezone')) {
date_default_timezone_set($this->container['config']->get('system.timezone'));
}

// Initialize uri, session.
$this->container['uri']->init();
$this->container['session']->init();

$this->container->setLocale();
}

}
15 changes: 15 additions & 0 deletions system/src/Grav/Common/Processors/PagesProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Grav\Common\Processors;

class PagesProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'pages';
public $title = 'Pages';

public function process() {
$this->container['pages']->init();
$this->container->fireEvent('onPagesInitialized');
$this->container->fireEvent('onPageInitialized');
}

}
14 changes: 14 additions & 0 deletions system/src/Grav/Common/Processors/PluginsProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Grav\Common\Processors;

class PluginsProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'plugins';
public $title = 'Plugins';

public function process() {
$this->container['plugins']->init();
$this->container->fireEvent('onPluginsInitialized');
}

}
10 changes: 10 additions & 0 deletions system/src/Grav/Common/Processors/ProcessorBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Grav\Common\Processors;

class ProcessorBase {

public function __construct($container) {
$this->container = $container;
}

}
6 changes: 6 additions & 0 deletions system/src/Grav/Common/Processors/ProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Grav\Common\Processors;

interface ProcessorInterface {
public function process();
}
14 changes: 14 additions & 0 deletions system/src/Grav/Common/Processors/RenderProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Grav\Common\Processors;

class RenderProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'render';
public $title = 'Render';

public function process() {
$this->container->output = $this->container['output'];
$this->container->fireEvent('onOutputGenerated');
}

}
14 changes: 14 additions & 0 deletions system/src/Grav/Common/Processors/SiteSetupProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Grav\Common\Processors;

class SiteSetupProcessor extends ProcessorBase implements ProcessorInterface {

public $id = '_setup';
public $title = 'Site Setup';

public function process() {
$this->container['setup']->init();
$this->container['streams'];
}

}
16 changes: 16 additions & 0 deletions system/src/Grav/Common/Processors/TasksProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Grav\Common\Processors;

class TasksProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'tasks';
public $title = 'Tasks';

public function process() {
$task = $this->container['task'];
if ($task) {
$this->container->fireEvent('onTask.' . $task);
}
}

}
13 changes: 13 additions & 0 deletions system/src/Grav/Common/Processors/ThemesProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Processors;

class ThemesProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'themes';
public $title = 'Themes';

public function process() {
$this->container['themes']->init();
}

}
13 changes: 13 additions & 0 deletions system/src/Grav/Common/Processors/TwigProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Processors;

class TwigProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'twig';
public $title = 'Twig';

public function process() {
$this->container['twig']->init();
}

}
13 changes: 13 additions & 0 deletions system/src/Grav/Common/Service/AssetsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Service;

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Grav\Common\Assets;

class AssetsServiceProvider implements ServiceProviderInterface
{
public function register(Container $container) {
$container['assets'] = new Assets();
}
}
15 changes: 15 additions & 0 deletions system/src/Grav/Common/Service/OutputServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Grav\Common\Service;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

class OutputServiceProvider implements ServiceProviderInterface
{
public function register(Container $container) {
$container['output'] = function ($c) {
/** @var Grav $c */
return $c['twig']->processSite($c['uri']->extension());
};
}
}
63 changes: 63 additions & 0 deletions system/src/Grav/Common/Service/PageServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace Grav\Common\Service;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

class PageServiceProvider implements ServiceProviderInterface
{
public function register(Container $container) {

$container['page'] = function ($c) {
/** @var Grav $c */

/** @var Pages $pages */
$pages = $c['pages'];
/** @var Language $language */
$language = $c['language'];

/** @var Uri $uri */
$uri = $c['uri'];

$path = $uri->path(); // Don't trim to support trailing slash default routes
$path = $path ?: '/';

$page = $pages->dispatch($path);

// Redirection tests
if ($page) {
// Language-specific redirection scenarios
if ($language->enabled()) {
if ($language->isLanguageInUrl() && !$language->isIncludeDefaultLanguage()) {
$c->redirect($page->route());
}
if (!$language->isLanguageInUrl() && $language->isIncludeDefaultLanguage()) {
$c->redirectLangSafe($page->route());
}
}
// Default route test and redirect
if ($c['config']->get('system.pages.redirect_default_route') && $page->route() != $path) {
$c->redirectLangSafe($page->route());
}
}

// if page is not found, try some fallback stuff
if (!$page || !$page->routable()) {

// Try fallback URL stuff...
$c->fallbackUrl($path);

// If no page found, fire event
$event = $c->fireEvent('onPageNotFound');

if (isset($event->page)) {
$page = $event->page;
} else {
throw new \RuntimeException('Page Not Found', 404);
}
}

return $page;
};
}
}
15 changes: 15 additions & 0 deletions system/src/Grav/Common/Service/TaskServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Grav\Common\Service;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

class TaskServiceProvider implements ServiceProviderInterface
{
public function register(Container $container) {
$container['task'] = function ($c) {
/** @var Grav $c */
return !empty($_POST['task']) ? $_POST['task'] : $c['uri']->param('task');
};
}
}