mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Implement an autoloader (#4140)
* Implement an autoloader When cleaning up classes for psr2, things got a bit unwieldy, so I implemented a class autoloader. I created a PSR-0 compliant LibreNMS directory and moved all classes there that made sense. Implemented LibreNMS\ClassLoader which supports adding manual class mappings This reduces the file includes needed and only loads classes when needed. * Add teh autoloader to graph.php * Add a small bit of docs Fix incomplete class in includes/discovery/functions.inc.php
This commit is contained in:
committed by
Neil Lathwood
parent
c2f7602cc5
commit
b8e9b2d917
128
LibreNMS/ClassLoader.php
Normal file
128
LibreNMS/ClassLoader.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* ClassLoader.php
|
||||
*
|
||||
* PSR-0 and custom class loader for LibreNMS
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2016 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace LibreNMS;
|
||||
|
||||
|
||||
/**
|
||||
* Class ClassLoader
|
||||
* @package LibreNMS
|
||||
*/
|
||||
class ClassLoader
|
||||
{
|
||||
/**
|
||||
* @var array stores dynamically added class > file mappings
|
||||
*/
|
||||
private $classMap;
|
||||
|
||||
/**
|
||||
* ClassLoader constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->classMap = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads classes conforming to the PSR-0 specificaton
|
||||
*
|
||||
* @param string $name Class name to load
|
||||
*/
|
||||
public static function psrLoad($name)
|
||||
{
|
||||
global $config, $vdebug;
|
||||
$file = str_replace(array('\\', '_'), DIRECTORY_SEPARATOR, $name) . '.php';
|
||||
$fullFile = $config['install_dir'] ? $config['install_dir'] . '/' . $file : $file;
|
||||
|
||||
if($vdebug) {
|
||||
echo __CLASS__ . " [[ $name > $fullFile ]]\n";
|
||||
}
|
||||
|
||||
if (is_readable($fullFile)) {
|
||||
include $fullFile;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads classes defined by mapClass()
|
||||
*
|
||||
* @param string $name Class name to load
|
||||
*/
|
||||
public function customLoad($name)
|
||||
{
|
||||
global $vdebug;
|
||||
if (array_key_exists($name, $this->classMap)) {
|
||||
$file = $this->classMap[$name];
|
||||
|
||||
if($vdebug) {
|
||||
echo __CLASS__ . " (( $name > $file ))\n";
|
||||
}
|
||||
|
||||
if (is_readable($file)) {
|
||||
include $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or set a custom class > file mapping
|
||||
*
|
||||
* @param string $class The full class name
|
||||
* @param string $file The path to the file containing the class, full path is preferred
|
||||
*/
|
||||
public function mapClass($class, $file)
|
||||
{
|
||||
$this->classMap[$class] = $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a class from the list of class > file mappings
|
||||
*
|
||||
* @param string $class The full class name
|
||||
*/
|
||||
public function unMapClass($class)
|
||||
{
|
||||
unset($this->classMap[$class]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this autoloader
|
||||
* Custom mappings will take precedence over PSR-0
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
spl_autoload_register(array($this, 'customLoad'));
|
||||
spl_autoload_register(__NAMESPACE__.'\ClassLoader::psrLoad');
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister this autoloader
|
||||
*/
|
||||
public function unregister()
|
||||
{
|
||||
spl_autoload_unregister(array($this, 'customLoad'));
|
||||
spl_autoload_unregister(__NAMESPACE__.'\ClassLoader::psrLoad');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user