summaryrefslogtreecommitdiff
path: root/lib/ActionFactory.php
blob: 8146e542dfb7c4182496af89b6333cf1c1c6c235 (plain)
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
<?php
/**
 * This file is part of RSS-Bridge, a PHP project capable of generating RSS and
 * Atom feeds for websites that don't have one.
 *
 * For the full license information, please view the UNLICENSE file distributed
 * with this source code.
 *
 * @package	Core
 * @license	http://unlicense.org/ UNLICENSE
 * @link	https://github.com/rss-bridge/rss-bridge
 */

/**
 * Factory for action objects.
 */
class ActionFactory extends FactoryAbstract {
	/**
	 * {@inheritdoc}
	 *
	 * @param string $name {@inheritdoc}
	 */
	public function create($name) {
		$filePath = $this->buildFilePath($name);

		if(!file_exists($filePath)) {
			throw new \Exception('File ' . $filePath . ' does not exist!');
		}

		require_once $filePath;

		$class = $this->buildClassName($name);

		if((new \ReflectionClass($class))->isInstantiable()) {
			return new $class();
		}

		return false;
	}

	/**
	 * Build class name from action name
	 *
	 * The class name consists of the action name with prefix "Action". The first
	 * character of the class name must be uppercase.
	 *
	 * Example: 'display' => 'DisplayAction'
	 *
	 * @param string $name The action name.
	 * @return string The class name.
	 */
	protected function buildClassName($name) {
		return ucfirst(strtolower($name)) . 'Action';
	}

	/**
	 * Build file path to the action class.
	 *
	 * @param string $name The action name.
	 * @return string Path to the action class.
	 */
	protected function buildFilePath($name) {
		return $this->getWorkingDir() . $this->buildClassName($name) . '.php';
	}
}