3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 class CRM_Core_ClassLoader
{
23 * We only need one instance of this object. So we use the singleton
24 * pattern and cache the instance in this variable
27 private static $_singleton = NULL;
30 * The classes in CiviTest have ucky, non-standard naming.
33 * Array(string $className => string $filePath).
35 private $civiTestClasses;
42 public static function &singleton($force = FALSE) {
43 if ($force || self
::$_singleton === NULL) {
44 self
::$_singleton = new CRM_Core_ClassLoader();
46 return self
::$_singleton;
50 * Has this been registered already.
54 protected $_registered;
59 protected function __construct() {
60 $this->_registered
= FALSE;
61 $this->civiTestClasses
= [
66 'CiviSeleniumTestCase',
69 'CiviEndToEndTestCase',
81 * Requires the autoload.php generated by composer
85 protected function requireComposerAutoload() {
86 // We are trying to locate 'vendor/autoload.php'. When installing CiviCRM
87 // manually from the built tarball, that will be two directories up in the
88 // civicrm-core directory. However, if civicrm-core was installed via
89 // composer as a library, that'll be 5 directories up where composer was
90 // run (ex. the Drupal root on a Drupal 8 site).
91 $civicrm_base_path = dirname(dirname(__DIR__
));
92 $top_path = dirname(dirname(dirname(dirname(dirname(__DIR__
)))));
94 if (file_exists($civicrm_base_path . '/vendor/autoload.php')) {
95 require_once $civicrm_base_path . '/vendor/autoload.php';
97 elseif (file_exists($top_path . '/vendor/autoload.php')) {
98 require_once $top_path . '/vendor/autoload.php';
103 * Registers this instance as an autoloader.
105 * @param bool $prepend
106 * Whether to prepend the autoloader or not.
110 public function register($prepend = FALSE) {
111 if ($this->_registered
) {
114 $civicrm_base_path = dirname(dirname(__DIR__
));
116 $this->requireComposerAutoload();
118 // we do this to prevent a autoloader errors with joomla / 3rd party packages
119 // use absolute path since we dont know the content of include_path as yet
121 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
122 // Extensions rely on include_path-based autoloading
123 spl_autoload_register([$this, 'loadClass'], TRUE, $prepend);
124 $this->initHtmlPurifier($prepend);
126 $this->_registered
= TRUE;
127 // The ClassLoader runs before the classes are available. Approximate Civi::paths()->get('[civicrm.packages]').
128 if (isset($GLOBALS['civicrm_paths']['civicrm.packages']['path'])) {
129 $packages_path = rtrim($GLOBALS['civicrm_paths']['civicrm.packages']['path'], DIRECTORY_SEPARATOR
);
132 $packages_path = implode(DIRECTORY_SEPARATOR
, [$civicrm_base_path, 'packages']);
139 $include_paths = implode(PATH_SEPARATOR
, $include_paths);
140 set_include_path($include_paths . PATH_SEPARATOR
. get_include_path());
141 // @todo Why do we need to load this again?
142 $this->requireComposerAutoload();
146 * Initialize HTML purifier class.
148 * @param string $prepend
150 public function initHtmlPurifier($prepend) {
151 if (class_exists('HTMLPurifier_Bootstrap')) {
152 // HTMLPurifier is already initialized, e.g. by the Drupal module.
156 $htmlPurifierPath = $this->getHtmlPurifierPath();
158 if (FALSE === $htmlPurifierPath) {
159 // No HTMLPurifier available, e.g. during installation.
162 require_once $htmlPurifierPath;
163 spl_autoload_register(['HTMLPurifier_Bootstrap', 'autoload'], TRUE, $prepend);
167 * @return string|false
168 * Path to the file where the class HTMLPurifier_Bootstrap is defined, or
169 * FALSE, if such a file does not exist.
171 private function getHtmlPurifierPath() {
172 if (function_exists('libraries_get_path')
173 && ($path = libraries_get_path('htmlpurifier'))
174 && file_exists($file = $path . '/library/HTMLPurifier/Bootstrap.php')
176 // We are in Drupal 7, and the HTMLPurifier module is installed.
177 // Use Drupal's HTMLPurifier path, to avoid conflicts.
178 // @todo Verify that we are really in Drupal 7, and not in some other
179 // environment that happens to provide a 'libraries_get_path()' function.
183 // we do this to prevent a autoloader errors with joomla / 3rd party packages
184 // Use absolute path, since we don't know the content of include_path yet.
186 if (isset($GLOBALS['civicrm_paths']['civicrm.packages']['path'])) {
187 $file = rtrim($GLOBALS['civicrm_paths']['civicrm.packages']['path'], DIRECTORY_SEPARATOR
) . '/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
190 $file = dirname(__FILE__
) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
192 if (file_exists($file)) {
202 public function loadClass($class) {
203 if ($class === 'CiviCRM_API3_Exception') {
204 //call internal error class api/Exception first
205 // allow api/Exception class call external error class
206 // CiviCRM_API3_Exception
207 require_once 'api/Exception.php';
210 // Only load classes that clearly belong to CiviCRM.
211 // Note: api/v3 does not use classes, but api_v3's test-suite does
212 (0 === strncmp($class, 'CRM_', 4) ||
0 === strncmp($class, 'CRMTraits', 9) ||
0 === strncmp($class, 'api_v3_', 7) ||
0 === strncmp($class, 'WebTest_', 8) ||
0 === strncmp($class, 'E2E_', 4)) &&
213 // Do not load PHP 5.3 namespaced classes.
214 // (in a future version, maybe)
215 FALSE === strpos($class, '\\')
217 $file = strtr($class, '_', '/') . '.php';
218 // There is some question about the best way to do this.
219 // "require_once" is nice because it's simple and throws
220 // intelligible errors.
221 if (FALSE != stream_resolve_include_path($file)) {
225 elseif (in_array($class, $this->civiTestClasses
)) {
226 $file = "tests/phpunit/CiviTest/{$class}.php";
227 if (FALSE != stream_resolve_include_path($file)) {
231 elseif ($class === 'CiviSeleniumSettings') {
232 if (!empty($GLOBALS['_CV'])) {
233 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.auto.php';
235 elseif (CRM_Utils_File
::isIncludable('tests/phpunit/CiviTest/CiviSeleniumSettings.php')) {
236 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.php';