2015-04-24 14:26:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Form;
|
|
|
|
|
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
use Icinga\Application\Modules\Module;
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
2018-06-14 14:08:04 +02:00
|
|
|
use RuntimeException;
|
2015-04-24 14:26:44 +02:00
|
|
|
|
|
|
|
class FormLoader
|
|
|
|
{
|
|
|
|
public static function load($name, Module $module = null)
|
|
|
|
{
|
|
|
|
if ($module === null) {
|
2018-06-14 14:08:04 +02:00
|
|
|
try {
|
|
|
|
$basedir = Icinga::app()->getApplicationDir('forms');
|
|
|
|
} catch (ProgrammingError $e) {
|
|
|
|
throw new RuntimeException($e->getMessage(), 0, $e);
|
|
|
|
}
|
2015-04-24 14:26:44 +02:00
|
|
|
$ns = '\\Icinga\\Web\\Forms\\';
|
|
|
|
} else {
|
|
|
|
$basedir = $module->getFormDir();
|
|
|
|
$ns = '\\Icinga\\Module\\' . ucfirst($module->getName()) . '\\Forms\\';
|
|
|
|
}
|
|
|
|
if (preg_match('~^[a-z0-9/]+$~i', $name)) {
|
|
|
|
$parts = preg_split('~/~', $name);
|
|
|
|
$class = ucfirst(array_pop($parts)) . 'Form';
|
|
|
|
$file = sprintf('%s/%s/%s.php', rtrim($basedir, '/'), implode('/', $parts), $class);
|
|
|
|
if (file_exists($file)) {
|
|
|
|
require_once($file);
|
|
|
|
$class = $ns . $class;
|
2015-07-02 15:30:02 +02:00
|
|
|
$options = array();
|
|
|
|
if ($module !== null) {
|
|
|
|
$options['icingaModule'] = $module;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $class($options);
|
2015-04-24 14:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-14 14:08:04 +02:00
|
|
|
|
|
|
|
throw new RuntimeException(sprintf('Cannot load %s (%s), no such form', $name, $file));
|
2015-04-24 14:26:44 +02:00
|
|
|
}
|
|
|
|
}
|