Skip to content

Commit

Permalink
ClassLoader: Introduce additional ClassLoader
Browse files Browse the repository at this point in the history
The Loader replaces TableLoader in a generic manner. FormLoader is handled by Icinga\Application\ClassLoader for every module.

refs #13064
  • Loading branch information
lazyfrosch committed Nov 28, 2016
1 parent 4bf26b4 commit 038d777
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 76 deletions.
100 changes: 100 additions & 0 deletions library/Director/ClassLoader.php
@@ -0,0 +1,100 @@
<?php

namespace Icinga\Module\Director;

/**
* PSR-4 ClassLoader for namespace extensions inside Icinga Director
*
* e.g. \Icinga\Module\Director\Forms\SomeTestForm -> ./application/forms/SomeTestForm.php
*
* @package Icinga\Module\Director
*/
class ClassLoader
{
/**
* Prefix to support here
*/
const NAMESPACE_PREFIX = 'Icinga\\Module\\Director\\';

/**
* Hard-coded length of NAMESPACE_PREFIX
*/
const PREFIX_LENGTH = 23;

/**
* Namespaces below ./application that get loaded
* @var array
*/
protected static $APPLICATION_NAMESPACES = array('tables');

/**
* Absolute path of the installed module
*
* @var string
*/
protected $moduleHome;

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

/**
* Loader function for spl_autoload
*
* @param $class
*/
public function loadClass($class)
{
if (strncmp(self::NAMESPACE_PREFIX, $class, self::PREFIX_LENGTH) !== 0) {
return;
}

// get the relative class name
$relativeClass = substr($class, self::PREFIX_LENGTH);
$relativeClassA = explode('\\', $relativeClass);

if (! empty($relativeClassA)) {
$subPath = strtolower(array_shift($relativeClassA));

if (in_array($subPath, self::$APPLICATION_NAMESPACES)) {
$file = $this->moduleHome
. DIRECTORY_SEPARATOR
. 'application'
. DIRECTORY_SEPARATOR
. $subPath
. DIRECTORY_SEPARATOR
. implode(DIRECTORY_SEPARATOR, $relativeClassA)
. '.php';

if (file_exists($file)) {
require $file;
}
}
}
}

/**
* Register {@link loadClass()} as an autoloader
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
}

/**
* Unregister {@link loadClass()} as an autoloader
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}

/**
* Unregister this as an autoloader
*/
public function __destruct()
{
$this->unregister();
}
}
15 changes: 11 additions & 4 deletions library/Director/Web/Controller/ActionController.php
Expand Up @@ -11,10 +11,9 @@
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Monitoring;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use Icinga\Module\Director\Web\Form\FormLoader;
use Icinga\Module\Director\Web\Form\QuickBaseForm;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Director\Web\Table\QuickTable;
use Icinga\Module\Director\Web\Table\TableLoader;
use Icinga\Security\SecurityException;
use Icinga\Web\Controller;
use Icinga\Web\Widget;
Expand Down Expand Up @@ -98,9 +97,16 @@ protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25,
*/
public function loadForm($name)
{
$form = FormLoader::load($name, $this->Module());
$class = '\\Icinga\\Module\\Director\\Forms\\' . ucfirst($name) . 'Form';

$options = array();
$options['icingaModule'] = $this->Module();

/** @var QuickBaseForm $form */
$form = new $class($options);
if ($this->getRequest()->isApiRequest()) {
// TODO: Ask form for API support?
/** @var QuickForm $form */
$form->setApiRequest();
}

Expand All @@ -114,7 +120,8 @@ public function loadForm($name)
*/
public function loadTable($name)
{
return TableLoader::load($name, $this->Module());
$class = '\\Icinga\\Module\\Director\\Tables\\' . ucfirst($name) . 'Table';
return new $class();
}

protected function sendJson($object)
Expand Down
37 changes: 0 additions & 37 deletions library/Director/Web/Form/FormLoader.php

This file was deleted.

34 changes: 0 additions & 34 deletions library/Director/Web/Table/TableLoader.php

This file was deleted.

7 changes: 6 additions & 1 deletion run.php
@@ -1,6 +1,8 @@
<?php

use Icinga\Application\Icinga;
use Icinga\Module\Director\ClassLoader;

/** @var \Icinga\Application\Modules\Module $this */

$prefix = '\\Icinga\\Module\\Director\\';

Expand Down Expand Up @@ -44,3 +46,6 @@
$this->provideHook('director/Job', $prefix . 'Job\\SyncJob');

$this->provideHook('cube/ActionLinks', 'CubeLinks');

$loader = new ClassLoader(__DIR__);
$loader->register();

0 comments on commit 038d777

Please sign in to comment.