1<?php
2
3require_once(dirname(__FILE__).'/../conf.php');
4require_once(dirname(__FILE__).'/tools.php');
5require_once(dirname(__FILE__).'/file_definition.php');
6
7class MakeRule extends Plugin {
8	public function name() { return NULL; }
9	public function can_handle($project, $file) { return false; }
10	protected function recipe($project, $file) { return NULL; }
11	protected function dependency($project, $file) { return $file->dependency(); }
12
13	/**
14	 * returns a target project file that can make $name
15	 */
16	public function handle($project, $file) {
17		$deps = $this->dependency($project, $file);
18		$recipe = $this->recipe($project, $file);
19		$def = new TargetDefinition(array('name' => $file->name(),
20			'type' => TARGET));
21		if (is_array($deps)) foreach ($deps as $dep) $def->add_dependency($dep);
22		$def->add_recipe($recipe);
23		return ProjectFile::create($project, $def);
24	}
25
26}
27
28class Plugin {
29	public function name() { return NULL; }
30	public function can_handle($project, $file) { return false; }
31	public function handle($project, $file) { return $file; }
32
33	protected function replace_extension($name, $from, $to) {
34		return substr($name, 0, -strlen($from)) . $to;
35	}
36}
37
38class Plugins {
39	private $plugins = array();
40
41	// $dir is which dir the plugins reside,
42	// $prefix is the prefix of the class name
43	// a plugin has the name in the form of prefix_filename
44	public function __construct($dir) {
45		$dir = PROJECTS_PLUGINS_DIR . $dir;
46		$dh = opendir($dir);
47		if ($dh == false) return;
48		while (($file = readdir($dh)) != false) {
49			if (!has_extension($file, '.php')) continue;
50			include_once($dir . $file);
51			$name = explode('.', $file);
52			array_pop($name);
53			$class = PROJECTS_PLUGINS_PREFIX . implode('_', $name);
54			if (!class_exists($class)) continue;
55			$plugin = new $class();
56			if ($plugin != NULL) $this->plugins[$plugin->name()] = $plugin;
57		}
58		closedir($dh);
59	}
60
61	public function handlers($project, $file) {
62		$handlers = array();
63		foreach ($this->plugins as $key => $plugin)
64			if ($plugin->can_handle($project, $file))
65				$handlers[$key] = $plugin;
66		return $handlers;
67	}
68}
69
70?>