File: IcingaApi.php

package info (click to toggle)
icinga 1.0.2-2%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 33,964 kB
  • ctags: 13,294
  • sloc: xml: 154,821; ansic: 99,198; sh: 14,761; sql: 5,852; php: 5,126; perl: 2,838; makefile: 1,268
file content (181 lines) | stat: -rw-r--r-- 4,459 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

require_once('objects/IcingaApiConstantsInterface.php');

/**
 * 
 * @author Christian Doebler <christian.doebler@netways.de>
 *
 */
class IcingaApi
	implements IcingaApiConstantsInterface {

	/*
	 * CONSTANTS AND VARIABLES
	 */

	const ObjectsFileSuffix = '.php';

	protected $debug = false;

	private static $objectsFound = false;

	private $searchObject = false;

	protected $icingaType = false;

	/*
	 * METHODS
	 */

	/**
	 * class constructor
	 *
	 * @param	void
	 * @return	void
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function __construct () {
		// TODO: remove
		return $this;
	}

	/**
	 * dynamically loads classes on demand
	 *
	 * @param	string		$className			name of class to load
	 * @return	void
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public static function autoload ($className) {

		$classFile = null;

		if (self::$objectsFound === false) {

			// fetch list of available objects and store them in an array for faster processing
			$objectsFound = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__)), RecursiveIteratorIterator::SELF_FIRST);
			foreach ($objectsFound as $classAbsFileName => $fileInfoObject) {
				if ($fileInfoObject->isFile()) {
					$key = $fileInfoObject->getBaseName(self::ObjectsFileSuffix);
					if ($key == $className) {
						$classFile = $classAbsFileName;
					}
					self::$objectsFound[$key] = $classAbsFileName;
				}
			}

		} else {

			// search array of objects for $className
			if (array_key_exists($className, self::$objectsFound)) {
				$classFile = self::$objectsFound[$className];
			}

		}

		if ($classFile !== null && file_exists($classFile)) {
			require_once($classFile);
		}

	}

	/**
	 * sets the icinga type for further generation of class names
	 * @param	string		$type				connection type
	 * @return	void
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	protected function setIcingaType ($type) {
		$this->icingaType = $type;
	}

	/**
	 * calls initialization method for new connection
	 *
	 * @param	string		$type				type of connection object
	 * @param	mixed		$config				configuration settings of connection object
	 * @return	IcingaApiConnection				connection object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public static function getConnection ($type, $config) {
		$class = 'IcingaApiConnection' . $type;

		try {
			$connectionObject = new $class;
			$connectionObject->setConfig($config);
			$connectionObject->setIcingaType($type);
			$connectionObject->connect();
		} catch (Exception $e) {
			throw new IcingaApiException('getConnection failed: ' . $e->getMessage());
		}

		return $connectionObject;
	}

	/**
	 * calls initialization method for new command
	 *
	 * @param	void
	 * @return	IcingaApiCommand				command object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public static function getCommandObject () {
		$commandObject = new IcingaApiCommand();
		return $commandObject;
	}

	/**
	 * calls initialization method for new command dispatcher
	 *
	 * @param	void
	 * @return	IcingaApiCommandDispatcher		command-dispatcher object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public static function getCommandDispatcher () {
		$commandDispatcher = new IcingaApiCommandDispatcher();
		return $commandDispatcher;
	}

	/**
	 * calls initialization method for new command collection
	 *
	 * @param	void
	 * @return	IcingaApiCommandCollection		command-collection object
	 * @author	Marius Hein <marius.hein@netways.de>
	 */
	public static function getCommandCollection() {
		return IcingaApiCommandCollection::getInstance();
	}

	/**
	 * sets debugging levels
	 * @param	mixed		$options			debugging options (see constants
	 * @return	IcingaAPI						IcingaAPI object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function debug ($options = false) {
		if ($options !== false) {
			$this->debug = array();
			if (!is_array($options)) {
				$options = array($options);
			}
			foreach ($options as $currentOption) {
				$this->debug[$currentOption] = false;
			}
			if (array_key_exists(self::DEBUG_OVERALL_TIME, $this->debug)) {
				$this->debug[self::DEBUG_OVERALL_TIME] = microtime(true);
			}
		}
		return $this;
	}

}

// extend exceptions
class IcingaApiException extends Exception {}

// register autoloader
spl_autoload_register(array('IcingaApi', 'autoload'));

?>