CRM-14370 - API Kernel - Dispatch events
[civicrm-core.git] / Civi / Core / Container.php
1 <?php
2 namespace Civi\Core;
3 use Doctrine\Common\Annotations\AnnotationReader;
4 use Doctrine\Common\Annotations\AnnotationRegistry;
5 use Doctrine\Common\Annotations\FileCacheReader;
6 use Doctrine\Common\Cache\FilesystemCache;
7 use Doctrine\ORM\EntityManager;
8 use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
9 use Doctrine\ORM\Tools\Setup;
10 use Symfony\Component\DependencyInjection\ContainerBuilder;
11 use Symfony\Component\DependencyInjection\Definition;
12 use Symfony\Component\DependencyInjection\Reference;
13
14 // TODO use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
15
16 class Container {
17
18 const SELF = 'civi_container_factory';
19
20 /**
21 * @var ContainerBuilder
22 */
23 private static $singleton;
24
25 /**
26 * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface
27 */
28 public static function singleton() {
29 if (self::$singleton === NULL) {
30 $c = new self();
31 self::$singleton = $c->createContainer();
32 }
33 return self::$singleton;
34 }
35
36 /**
37 * @var ContainerBuilder
38 */
39 public function createContainer() {
40 $civicrm_base_path = dirname(dirname(__DIR__));
41 $container = new ContainerBuilder();
42 $container->setParameter('civicrm_base_path', $civicrm_base_path);
43 $container->set(self::SELF, $this);
44
45 // TODO Move configuration to an external file; define caching structure
46 // if (empty($configDirectories)) {
47 // throw new \Exception(__CLASS__ . ': Missing required properties (civicrmRoot, configDirectories)');
48 // }
49 // $locator = new FileLocator($configDirectories);
50 // $loaderResolver = new LoaderResolver(array(
51 // new YamlFileLoader($container, $locator)
52 // ));
53 // $delegatingLoader = new DelegatingLoader($loaderResolver);
54 // foreach (array('services.yml') as $file) {
55 // $yamlUserFiles = $locator->locate($file, NULL, FALSE);
56 // foreach ($yamlUserFiles as $file) {
57 // $delegatingLoader->load($file);
58 // }
59 // }
60
61 $container->setDefinition('dispatcher', new Definition(
62 '\Symfony\Component\EventDispatcher\EventDispatcher',
63 array()
64 ))
65 ->setFactoryService(self::SELF)->setFactoryMethod('createEventDispatcher');
66
67 $container->setDefinition('civi_api_kernel', new Definition(
68 '\Civi\API\Kernel',
69 array(new Reference('dispatcher'))
70 ))
71 ->setFactoryService(self::SELF)->setFactoryMethod('createApiKernel');
72
73 return $container;
74 }
75
76 /**
77 * @return \Symfony\Component\EventDispatcher\EventDispatcher
78 */
79 public function createEventDispatcher() {
80 $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
81 return $dispatcher;
82 }
83
84 /**
85 * @param \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher
86 * @return \Civi\API\Kernel
87 */
88 public function createApiKernel($dispatcher) {
89 $dispatcher->addListener(\Civi\API\Events::AUTHORIZE, function($event) {
90 // dummy placeholder
91 $event->authorize();
92 });
93 $kernel = new \Civi\API\Kernel($dispatcher, array());
94 return $kernel;
95 }
96 }