File: FormLoader.php

package info (click to toggle)
icingaweb2-module-businessprocess 2.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: php: 10,971; javascript: 2,019; sh: 115; xml: 49; makefile: 18
file content (39 lines) | stat: -rw-r--r-- 1,269 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

namespace Icinga\Module\Businessprocess\Web\Form;

use Icinga\Application\Icinga;
use Icinga\Application\Modules\Module;
use Icinga\Exception\ProgrammingError;

class FormLoader
{
    public static function load($name, Module $module = null)
    {
        if ($module === null) {
            $basedir = Icinga::app()->getApplicationDir('forms');
            $ns = '\\Icinga\\Web\\Forms\\';
        } else {
            $basedir = $module->getFormDir();
            $ns = '\\Icinga\\Module\\' . ucfirst($module->getName()) . '\\Forms\\';
        }

        $file = null;
        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;
                $options = array();
                if ($module !== null) {
                    $options['icingaModule'] = $module;
                }

                return new $class($options);
            }
        }
        throw new ProgrammingError(sprintf('Cannot load %s (%s), no such form', $name, $file));
    }
}