1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\ApplicationDI;
9:
10: use Nette;
11:
12:
13: 14: 15: 16:
17: class PresenterFactoryCallback
18: {
19:
20: private $container;
21:
22:
23: private $invalidLinkMode;
24:
25:
26: private $touchToRefresh;
27:
28:
29: public function __construct(Nette\DI\Container $container, $invalidLinkMode, $touchToRefresh)
30: {
31: $this->container = $container;
32: $this->invalidLinkMode = $invalidLinkMode;
33: $this->touchToRefresh = $touchToRefresh;
34: }
35:
36:
37: 38: 39:
40: public function __invoke($class)
41: {
42: $services = array_keys($this->container->findByTag('nette.presenter'), $class, true);
43: if (count($services) > 1) {
44: throw new Nette\Application\InvalidPresenterException("Multiple services of type $class found: " . implode(', ', $services) . '.');
45:
46: } elseif (!$services) {
47: if ($this->touchToRefresh) {
48: touch($this->touchToRefresh);
49: }
50:
51: $presenter = $this->container->createInstance($class);
52: $this->container->callInjects($presenter);
53: if ($presenter instanceof Nette\Application\UI\Presenter && $presenter->invalidLinkMode === null) {
54: $presenter->invalidLinkMode = $this->invalidLinkMode;
55: }
56: return $presenter;
57: }
58:
59: return $this->container->createService($services[0]);
60: }
61: }
62: