Commit | Line | Data |
---|---|---|
fa184193 TO |
1 | <?php |
2 | namespace Civi\Core; | |
46bcf597 | 3 | |
fa184193 TO |
4 | use Doctrine\Common\Annotations\AnnotationReader; |
5 | use Doctrine\Common\Annotations\AnnotationRegistry; | |
6 | use Doctrine\Common\Annotations\FileCacheReader; | |
7 | use Doctrine\Common\Cache\FilesystemCache; | |
8 | use Doctrine\ORM\EntityManager; | |
9 | use Doctrine\ORM\Mapping\Driver\AnnotationDriver; | |
10 | use Doctrine\ORM\Tools\Setup; | |
11 | use Symfony\Component\DependencyInjection\ContainerBuilder; | |
c8074a93 | 12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
fa184193 TO |
13 | use Symfony\Component\DependencyInjection\Definition; |
14 | use Symfony\Component\DependencyInjection\Reference; | |
15 | ||
16 | // TODO use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | |
17 | ||
6550386a EM |
18 | /** |
19 | * Class Container | |
20 | * @package Civi\Core | |
21 | */ | |
fa184193 TO |
22 | class Container { |
23 | ||
24 | const SELF = 'civi_container_factory'; | |
25 | ||
26 | /** | |
27 | * @var ContainerBuilder | |
28 | */ | |
29 | private static $singleton; | |
30 | ||
31 | /** | |
04855556 TO |
32 | * @param bool $reset |
33 | * Whether to forcibly rebuild the entire container. | |
fa184193 TO |
34 | * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface |
35 | */ | |
378e2654 TO |
36 | public static function singleton($reset = FALSE) { |
37 | if ($reset || self::$singleton === NULL) { | |
fa184193 TO |
38 | $c = new self(); |
39 | self::$singleton = $c->createContainer(); | |
40 | } | |
41 | return self::$singleton; | |
42 | } | |
43 | ||
44 | /** | |
45 | * @var ContainerBuilder | |
77b97be7 | 46 | * @return \Symfony\Component\DependencyInjection\ContainerBuilder |
fa184193 TO |
47 | */ |
48 | public function createContainer() { | |
49 | $civicrm_base_path = dirname(dirname(__DIR__)); | |
50 | $container = new ContainerBuilder(); | |
51 | $container->setParameter('civicrm_base_path', $civicrm_base_path); | |
52 | $container->set(self::SELF, $this); | |
53 | ||
505d8b83 TO |
54 | // TODO Move configuration to an external file; define caching structure |
55 | // if (empty($configDirectories)) { | |
56 | // throw new \Exception(__CLASS__ . ': Missing required properties (civicrmRoot, configDirectories)'); | |
57 | // } | |
58 | // $locator = new FileLocator($configDirectories); | |
59 | // $loaderResolver = new LoaderResolver(array( | |
60 | // new YamlFileLoader($container, $locator) | |
61 | // )); | |
62 | // $delegatingLoader = new DelegatingLoader($loaderResolver); | |
63 | // foreach (array('services.yml') as $file) { | |
64 | // $yamlUserFiles = $locator->locate($file, NULL, FALSE); | |
65 | // foreach ($yamlUserFiles as $file) { | |
66 | // $delegatingLoader->load($file); | |
67 | // } | |
68 | // } | |
fa184193 | 69 | |
16072ce1 TO |
70 | $container->setDefinition('angular', new Definition( |
71 | '\Civi\Angular\Manager', | |
72 | array() | |
73 | )) | |
74 | ->setFactoryService(self::SELF)->setFactoryMethod('createAngularManager'); | |
75 | ||
fa184193 TO |
76 | $container->setDefinition('dispatcher', new Definition( |
77 | '\Symfony\Component\EventDispatcher\EventDispatcher', | |
78 | array() | |
79 | )) | |
80 | ->setFactoryService(self::SELF)->setFactoryMethod('createEventDispatcher'); | |
81 | ||
c65db512 TO |
82 | $container->setDefinition('magic_function_provider', new Definition( |
83 | '\Civi\API\Provider\MagicFunctionProvider', | |
84 | array() | |
85 | )); | |
86 | ||
0f643fb2 TO |
87 | $container->setDefinition('civi_api_kernel', new Definition( |
88 | '\Civi\API\Kernel', | |
c65db512 | 89 | array(new Reference('dispatcher'), new Reference('magic_function_provider')) |
0f643fb2 TO |
90 | )) |
91 | ->setFactoryService(self::SELF)->setFactoryMethod('createApiKernel'); | |
92 | ||
c8074a93 TO |
93 | // Expose legacy singletons as services in the container. |
94 | $singletons = array( | |
95 | 'resources' => 'CRM_Core_Resources', | |
96 | 'httpClient' => 'CRM_Utils_HttpClient', | |
97 | // Maybe? 'config' => 'CRM_Core_Config', | |
98 | // Maybe? 'smarty' => 'CRM_Core_Smarty', | |
99 | ); | |
100 | foreach ($singletons as $name => $class) { | |
101 | $container->setDefinition($name, new Definition( | |
102 | $class | |
103 | )) | |
104 | ->setFactoryClass($class)->setFactoryMethod('singleton'); | |
105 | } | |
106 | ||
fa184193 TO |
107 | return $container; |
108 | } | |
109 | ||
16072ce1 TO |
110 | /** |
111 | * @return \Civi\Angular\Manager | |
112 | */ | |
113 | public function createAngularManager() { | |
114 | return new \Civi\Angular\Manager(\CRM_Core_Resources::singleton()); | |
115 | } | |
116 | ||
fa184193 TO |
117 | /** |
118 | * @return \Symfony\Component\EventDispatcher\EventDispatcher | |
119 | */ | |
120 | public function createEventDispatcher() { | |
121 | $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); | |
708d8fa2 | 122 | $dispatcher->addListener('hook_civicrm_post::Activity', array('\Civi\CCase\Events', 'fireCaseChange')); |
753657ed | 123 | $dispatcher->addListener('hook_civicrm_post::Case', array('\Civi\CCase\Events', 'fireCaseChange')); |
708d8fa2 | 124 | $dispatcher->addListener('hook_civicrm_caseChange', array('\Civi\CCase\Events', 'delegateToXmlListeners')); |
b019b130 | 125 | $dispatcher->addListener('hook_civicrm_caseChange', array('\Civi\CCase\SequenceListener', 'onCaseChange_static')); |
8498c2b7 | 126 | $dispatcher->addListener('DAO::post-insert', array('\CRM_Core_BAO_RecurringEntity', 'triggerInsert')); |
127 | $dispatcher->addListener('DAO::post-update', array('\CRM_Core_BAO_RecurringEntity', 'triggerUpdate')); | |
128 | $dispatcher->addListener('DAO::post-delete', array('\CRM_Core_BAO_RecurringEntity', 'triggerDelete')); | |
46bcf597 TO |
129 | $dispatcher->addListener('hook_civicrm_unhandled_exception', array( |
130 | 'CRM_Core_LegacyErrorHandler', | |
7c550ca0 | 131 | 'handleException', |
46bcf597 | 132 | )); |
fa184193 TO |
133 | return $dispatcher; |
134 | } | |
0f643fb2 TO |
135 | |
136 | /** | |
137 | * @param \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher | |
2a6da8d7 EM |
138 | * @param $magicFunctionProvider |
139 | * | |
0f643fb2 TO |
140 | * @return \Civi\API\Kernel |
141 | */ | |
c65db512 | 142 | public function createApiKernel($dispatcher, $magicFunctionProvider) { |
0a946de2 | 143 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\ChainSubscriber()); |
b55bc593 | 144 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\TransactionSubscriber()); |
bace5cd9 | 145 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\I18nSubscriber()); |
c65db512 | 146 | $dispatcher->addSubscriber($magicFunctionProvider); |
d0c9daa4 | 147 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck()); |
dcef11bd | 148 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\APIv3SchemaAdapter()); |
6d3bdc98 TO |
149 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\WrapperAdapter(array( |
150 | \CRM_Utils_API_HTMLInputCoder::singleton(), | |
151 | \CRM_Utils_API_NullOutputCoder::singleton(), | |
152 | \CRM_Utils_API_ReloadOption::singleton(), | |
153 | \CRM_Utils_API_MatchOption::singleton(), | |
154 | ))); | |
0661f62b | 155 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\XDebugSubscriber()); |
82376c19 TO |
156 | $kernel = new \Civi\API\Kernel($dispatcher); |
157 | ||
158 | $reflectionProvider = new \Civi\API\Provider\ReflectionProvider($kernel); | |
159 | $dispatcher->addSubscriber($reflectionProvider); | |
160 | ||
56154d36 TO |
161 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\DynamicFKAuthorization( |
162 | $kernel, | |
163 | 'Attachment', | |
164 | array('create', 'get', 'delete'), | |
2e37a19f | 165 | // Given a file ID, determine the entity+table it's attached to. |
56154d36 TO |
166 | 'SELECT if(cf.id,1,0) as is_valid, cef.entity_table, cef.entity_id |
167 | FROM civicrm_file cf | |
168 | LEFT JOIN civicrm_entity_file cef ON cf.id = cef.file_id | |
169 | WHERE cf.id = %1', | |
29468114 TO |
170 | // Get a list of custom fields (field_name,table_name,extends) |
171 | 'SELECT concat("custom_",fld.id) as field_name, | |
172 | grp.table_name as table_name, | |
173 | grp.extends as extends | |
174 | FROM civicrm_custom_field fld | |
175 | INNER JOIN civicrm_custom_group grp ON fld.custom_group_id = grp.id | |
176 | WHERE fld.data_type = "File" | |
177 | ', | |
e3e66815 | 178 | array('civicrm_activity', 'civicrm_mailing', 'civicrm_contact', 'civicrm_grant') |
56154d36 TO |
179 | )); |
180 | ||
82376c19 TO |
181 | $kernel->setApiProviders(array( |
182 | $reflectionProvider, | |
183 | $magicFunctionProvider, | |
184 | )); | |
185 | ||
0f643fb2 TO |
186 | return $kernel; |
187 | } | |
96025800 | 188 | |
fa184193 | 189 | } |