Commit | Line | Data |
---|---|---|
4025c773 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
4025c773 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
4025c773 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
016d95d3 | 13 | * Class CRM_Extension_ClassLoader |
4025c773 TO |
14 | */ |
15 | class CRM_Extension_ClassLoader { | |
16 | ||
17 | /** | |
18 | * @var CRM_Extension_Mapper | |
19 | */ | |
20 | protected $mapper; | |
21 | ||
22 | /** | |
23 | * @var CRM_Extension_Container_Interface | |
24 | */ | |
25 | protected $container; | |
26 | ||
27 | /** | |
28 | * @var CRM_Extension_Manager | |
29 | */ | |
30 | protected $manager; | |
31 | ||
32 | /** | |
33 | * @var \Composer\Autoload\ClassLoader | |
34 | */ | |
35 | protected $loader; | |
36 | ||
37 | /** | |
38 | * CRM_Extension_ClassLoader constructor. | |
39 | * @param \CRM_Extension_Mapper $mapper | |
40 | * @param \CRM_Extension_Container_Interface $container | |
41 | * @param \CRM_Extension_Manager $manager | |
42 | */ | |
43 | public function __construct(\CRM_Extension_Mapper $mapper, \CRM_Extension_Container_Interface $container, \CRM_Extension_Manager $manager) { | |
44 | $this->mapper = $mapper; | |
45 | $this->container = $container; | |
46 | $this->manager = $manager; | |
47 | } | |
48 | ||
49 | public function __destruct() { | |
85c7eb67 | 50 | $this->unregister(); |
4025c773 TO |
51 | } |
52 | ||
53 | /** | |
54 | * Registers this instance as an autoloader. | |
14069c56 | 55 | * @return CRM_Extension_ClassLoader |
4025c773 TO |
56 | */ |
57 | public function register() { | |
58 | // In pre-installation environments, don't bother with caching. | |
6f50d29c | 59 | if (!defined('CIVICRM_DSN') || defined('CIVICRM_TEST') || \CRM_Utils_System::isInUpgradeMode()) { |
4025c773 TO |
60 | return $this->buildClassLoader()->register(); |
61 | } | |
62 | ||
85c7eb67 | 63 | $file = $this->getCacheFile(); |
4025c773 TO |
64 | if (file_exists($file)) { |
65 | $loader = require $file; | |
66 | } | |
67 | else { | |
68 | $loader = $this->buildClassLoader(); | |
69 | $ser = serialize($loader); | |
70 | file_put_contents($file, | |
71 | sprintf("<?php\nreturn unserialize(%s);", var_export($ser, 1)) | |
72 | ); | |
73 | } | |
74 | return $loader->register(); | |
75 | } | |
76 | ||
77 | /** | |
78 | * @return \Composer\Autoload\ClassLoader | |
79 | * @throws \CRM_Extension_Exception | |
80 | * @throws \Exception | |
81 | */ | |
82 | public function buildClassLoader() { | |
83 | $loader = new \Composer\Autoload\ClassLoader(); | |
84 | ||
85 | $statuses = $this->manager->getStatuses(); | |
86 | foreach ($statuses as $key => $status) { | |
87 | if ($status !== CRM_Extension_Manager::STATUS_INSTALLED) { | |
88 | continue; | |
89 | } | |
90 | $path = $this->mapper->keyToBasePath($key); | |
91 | $info = $this->mapper->keyToInfo($key); | |
92 | if (!empty($info->classloader)) { | |
93 | foreach ($info->classloader as $mapping) { | |
94 | switch ($mapping['type']) { | |
95 | case 'psr4': | |
46d3fc4a | 96 | $loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']); |
4025c773 TO |
97 | break; |
98 | } | |
99 | $result[] = $mapping; | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | return $loader; | |
105 | } | |
106 | ||
85c7eb67 TO |
107 | public function unregister() { |
108 | if ($this->loader) { | |
109 | $this->loader->unregister(); | |
110 | $this->loader = NULL; | |
111 | } | |
112 | } | |
113 | ||
114 | public function refresh() { | |
115 | $this->unregister(); | |
116 | $file = $this->getCacheFile(); | |
117 | if (file_exists($file)) { | |
118 | unlink($file); | |
119 | } | |
120 | $this->register(); | |
121 | } | |
122 | ||
123 | /** | |
124 | * @return string | |
125 | */ | |
126 | protected function getCacheFile() { | |
127 | $envId = \CRM_Core_Config_Runtime::getId(); | |
6f50d29c | 128 | $file = \Civi::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php"); |
85c7eb67 TO |
129 | return $file; |
130 | } | |
131 | ||
4025c773 | 132 | } |