Merge pull request #21798 from demeritcowboy/sid-notice
[civicrm-core.git] / CRM / Core / ClassLoader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18 class CRM_Core_ClassLoader {
19
20 /**
21 * We only need one instance of this object. So we use the singleton
22 * pattern and cache the instance in this variable
23 * @var object
24 */
25 private static $_singleton = NULL;
26
27 /**
28 * The classes in CiviTest have ucky, non-standard naming.
29 *
30 * @var array
31 * Array(string $className => string $filePath).
32 */
33 private $civiTestClasses;
34
35 /**
36 * @param bool $force
37 *
38 * @return object
39 */
40 public static function &singleton($force = FALSE) {
41 if ($force || self::$_singleton === NULL) {
42 self::$_singleton = new CRM_Core_ClassLoader();
43 }
44 return self::$_singleton;
45 }
46
47 /**
48 * Has this been registered already.
49 *
50 * @var bool
51 */
52 protected $_registered;
53
54 /**
55 * Class constructor.
56 */
57 protected function __construct() {
58 $this->_registered = FALSE;
59 $this->civiTestClasses = [
60 'CiviCaseTestCase',
61 'CiviDBAssert',
62 'CiviMailUtils',
63 'CiviReportTestCase',
64 'CiviSeleniumTestCase',
65 'CiviTestSuite',
66 'CiviUnitTestCase',
67 'CiviEndToEndTestCase',
68 'Contact',
69 'ContributionPage',
70 'Custom',
71 'Event',
72 'Membership',
73 'Participant',
74 'PaypalPro',
75 ];
76 }
77
78 /**
79 * Requires the autoload.php generated by composer
80 *
81 * @return void
82 */
83 protected function requireComposerAutoload() {
84 // We are trying to locate 'vendor/autoload.php'. When installing CiviCRM
85 // manually from the built tarball, that will be two directories up in the
86 // civicrm-core directory. However, if civicrm-core was installed via
87 // composer as a library, that'll be 5 directories up where composer was
88 // run (ex. the Drupal root on a Drupal 8 site).
89 $civicrm_base_path = dirname(dirname(__DIR__));
90 $top_path = dirname(dirname(dirname(dirname(dirname(__DIR__)))));
91
92 if (file_exists($civicrm_base_path . '/vendor/autoload.php')) {
93 require_once $civicrm_base_path . '/vendor/autoload.php';
94 }
95 elseif (file_exists($top_path . '/vendor/autoload.php')) {
96 require_once $top_path . '/vendor/autoload.php';
97 }
98 }
99
100 /**
101 * Registers this instance as an autoloader.
102 *
103 * @param bool $prepend
104 * Whether to prepend the autoloader or not.
105 *
106 * @api
107 */
108 public function register($prepend = FALSE) {
109 if ($this->_registered) {
110 return;
111 }
112 $civicrm_base_path = dirname(dirname(__DIR__));
113
114 $this->requireComposerAutoload();
115
116 // we do this to prevent a autoloader errors with joomla / 3rd party packages
117 // use absolute path since we dont know the content of include_path as yet
118 // CRM-11304
119 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
120 // Extensions rely on include_path-based autoloading
121 spl_autoload_register([$this, 'loadClass'], TRUE, $prepend);
122
123 $this->_registered = TRUE;
124 // The ClassLoader runs before the classes are available. Approximate Civi::paths()->get('[civicrm.packages]').
125 if (isset($GLOBALS['civicrm_paths']['civicrm.packages']['path'])) {
126 $packages_path = rtrim($GLOBALS['civicrm_paths']['civicrm.packages']['path'], DIRECTORY_SEPARATOR);
127 }
128 else {
129 $packages_path = implode(DIRECTORY_SEPARATOR, [$civicrm_base_path, 'packages']);
130 }
131 $include_paths = [
132 '.',
133 $civicrm_base_path,
134 $packages_path,
135 ];
136 $include_paths = implode(PATH_SEPARATOR, $include_paths);
137 set_include_path($include_paths . PATH_SEPARATOR . get_include_path());
138 // @todo Why do we need to load this again?
139 $this->requireComposerAutoload();
140 }
141
142 /**
143 * @param $class
144 */
145 public function loadClass($class) {
146 if ($class === 'CiviCRM_API3_Exception' || $class === 'API_Exception') {
147 //call internal error class api/Exception first
148 // allow api/Exception class call external error class
149 // CiviCRM_API3_Exception
150 require_once 'api/Exception.php';
151 }
152 if (
153 // Only load classes that clearly belong to CiviCRM.
154 // Note: api/v3 does not use classes, but api_v3's test-suite does
155 (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)) &&
156 // Do not load PHP 5.3 namespaced classes.
157 // (in a future version, maybe)
158 FALSE === strpos($class, '\\')
159 ) {
160 $file = strtr($class, '_', '/') . '.php';
161 // There is some question about the best way to do this.
162 // "require_once" is nice because it's simple and throws
163 // intelligible errors.
164 if (FALSE != stream_resolve_include_path($file)) {
165 require_once $file;
166 }
167 }
168 elseif (in_array($class, $this->civiTestClasses)) {
169 $file = "tests/phpunit/CiviTest/{$class}.php";
170 if (FALSE != stream_resolve_include_path($file)) {
171 require_once $file;
172 }
173 }
174 elseif ($class === 'CiviSeleniumSettings') {
175 if (!empty($GLOBALS['_CV'])) {
176 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.auto.php';
177 }
178 elseif (CRM_Utils_File::isIncludable('tests/phpunit/CiviTest/CiviSeleniumSettings.php')) {
179 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.php';
180 }
181 }
182 }
183
184 }