summaryrefslogtreecommitdiffstats
path: root/inc/hook.inc.php
blob: 05078f729c01c67b21cbd108a1209e0a7e4965c9 (plain) (blame)
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
<?php

/**
 * Generic helper for getting and executing hooks.
 */
class Hook
{

	/**
	 * Get list of all known and enabled modules for given hook.
	 * Internally, this scans for "modules/<*>/hooks/$hookName.inc.php"
	 * and optionally checks if the module's dependencies are fulfilled,
	 * then returns a list of all matching modules.
	 * @param string $hookName Name of hook to search for.
	 * @param bool $filterBroken if true, modules that have a hook but have missing deps will not be returned
	 * @return \Hook[] list of modules with requested hooks
	 */
	public static function load($hookName, $filterBroken = true)
	{
		$retval = array();
		foreach (glob('modules/*/hooks/' . $hookName . '.inc.php', GLOB_NOSORT) as $file) {
			preg_match('#^modules/([^/]+)/#', $file, $out);
			if ($filterBroken && !Module::isAvailable($out[1]))
				continue;
			$retval[] = new Hook($out[1], $file);
		}
		return $retval;
	}

	/**
	 * Load given hook for a specific module only.
	 *
	 * @param string $moduleName Module
	 * @param string $hookName Hook
	 * @param bool $filterBroken return false if the module has missing deps
	 * @return Hook|false hook instance, false on error or if module doesn't have given hook
	 */
	public static function loadSingle($moduleName, $hookName, $filterBroken = true)
	{
		if (Module::get($moduleName) === false) // No such module
			return false;
		if ($filterBroken && !Module::isAvailable($moduleName)) // Broken
			return false;
		$file = 'modules/' . $moduleName . '/hooks/' . $hookName . '.inc.php';
		if (!file_exists($file)) // No hook
			return false;
		return new Hook($moduleName, $file);
	}

	/*
	 *
	 */

	public $moduleId;
	public $file;

	private function __construct($module, $hookFile)
	{
		$this->moduleId = $module;
		$this->file = $hookFile;
	}

	/**
	 * Run the hook's code. The include is expected to return a
	 * value, which will in turn be the return value of this
	 * method.
	 *
	 * @return mixed The return value of the include file, or false on error
	 */
	public function run()
	{
		try {
			return (include $this->file);
		} catch (Exception $e) {
			error_log($e);
			return false;
		}
	}

}