1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Application;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class PresenterFactory implements IPresenterFactory
24: {
25:
26: public $caseSensitive = FALSE;
27:
28:
29: private $baseDir;
30:
31:
32: private $cache = array();
33:
34:
35: private $context;
36:
37:
38:
39: 40: 41:
42: public function __construct($baseDir, Nette\DI\IContainer $context)
43: {
44: $this->baseDir = $baseDir;
45: $this->context = $context;
46: }
47:
48:
49:
50: 51: 52: 53: 54:
55: public function createPresenter($name)
56: {
57: $class = $this->getPresenterClass($name);
58: $presenter = new $class;
59: $presenter->setContext($this->context);
60: return $presenter;
61: }
62:
63:
64:
65: 66: 67: 68: 69:
70: public function getPresenterClass(& $name)
71: {
72: if (isset($this->cache[$name])) {
73: list($class, $name) = $this->cache[$name];
74: return $class;
75: }
76:
77: if (!is_string($name) || !Nette\Utils\Strings::match($name, "#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#")) {
78: throw new InvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
79: }
80:
81: $class = $this->formatPresenterClass($name);
82:
83: if (!class_exists($class)) {
84:
85: $file = $this->formatPresenterFile($name);
86: if (is_file($file) && is_readable($file)) {
87: Nette\Utils\LimitedScope::load($file);
88: }
89:
90: if (!class_exists($class)) {
91: throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
92: }
93: }
94:
95: $reflection = new Nette\Reflection\ClassType($class);
96: $class = $reflection->getName();
97:
98: if (!$reflection->implementsInterface('Nette\Application\IPresenter')) {
99: throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is not Nette\\Application\\IPresenter implementor.");
100: }
101:
102: if ($reflection->isAbstract()) {
103: throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
104: }
105:
106:
107: $realName = $this->unformatPresenterClass($class);
108: if ($name !== $realName) {
109: if ($this->caseSensitive) {
110: throw new InvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
111: } else {
112: $this->cache[$name] = array($class, $realName);
113: $name = $realName;
114: }
115: } else {
116: $this->cache[$name] = array($class, $realName);
117: }
118:
119: return $class;
120: }
121:
122:
123:
124: 125: 126: 127: 128:
129: public function formatPresenterClass($presenter)
130: {
131: return str_replace(':', 'Module\\', $presenter) . 'Presenter';
132: }
133:
134:
135:
136: 137: 138: 139: 140:
141: public function unformatPresenterClass($class)
142: {
143: return str_replace('Module\\', ':', substr($class, 0, -9));
144: }
145:
146:
147:
148: 149: 150: 151: 152:
153: public function formatPresenterFile($presenter)
154: {
155: $path = '/' . str_replace(':', 'Module/', $presenter);
156: return $this->baseDir . substr_replace($path, '/presenters', strrpos($path, '/'), 0) . 'Presenter.php';
157: }
158:
159: }
160: