Merge pull request #14577 from mattwire/type_consolidation
[civicrm-core.git] / CRM / Core / ClassLoader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36 class CRM_Core_ClassLoader {
37
38 /**
39 * We only need one instance of this object. So we use the singleton
40 * pattern and cache the instance in this variable
41 * @var object
42 */
43 private static $_singleton = NULL;
44
45 /**
46 * The classes in CiviTest have ucky, non-standard naming.
47 *
48 * @var array
49 * Array(string $className => string $filePath).
50 */
51 private $civiTestClasses;
52
53 /**
54 * @param bool $force
55 *
56 * @return object
57 */
58 public static function &singleton($force = FALSE) {
59 if ($force || self::$_singleton === NULL) {
60 self::$_singleton = new CRM_Core_ClassLoader();
61 }
62 return self::$_singleton;
63 }
64
65 /**
66 * Has this been registered already.
67 *
68 * @var bool
69 */
70 protected $_registered;
71
72 /**
73 * Class constructor.
74 */
75 protected function __construct() {
76 $this->_registered = FALSE;
77 $this->civiTestClasses = [
78 'CiviCaseTestCase',
79 'CiviDBAssert',
80 'CiviMailUtils',
81 'CiviReportTestCase',
82 'CiviSeleniumTestCase',
83 'CiviTestSuite',
84 'CiviUnitTestCase',
85 'CiviEndToEndTestCase',
86 'Contact',
87 'ContributionPage',
88 'Custom',
89 'Event',
90 'Membership',
91 'Participant',
92 'PaypalPro',
93 ];
94 }
95
96 /**
97 * Requires the autoload.php generated by composer
98 *
99 * @return void
100 */
101 protected function requireComposerAutoload() {
102 // We are trying to locate 'vendor/autoload.php'. When installing CiviCRM
103 // manually from the built tarball, that will be two directories up in the
104 // civicrm-core directory. However, if civicrm-core was installed via
105 // composer as a library, that'll be 5 directories up where composer was
106 // run (ex. the Drupal root on a Drupal 8 site).
107 $civicrm_base_path = dirname(dirname(__DIR__));
108 $top_path = dirname(dirname(dirname(dirname(dirname(__DIR__)))));
109
110 if (file_exists($civicrm_base_path . '/vendor/autoload.php')) {
111 require_once $civicrm_base_path . '/vendor/autoload.php';
112 }
113 elseif (file_exists($top_path . '/vendor/autoload.php')) {
114 require_once $top_path . '/vendor/autoload.php';
115 }
116 }
117
118 /**
119 * Registers this instance as an autoloader.
120 *
121 * @param bool $prepend
122 * Whether to prepend the autoloader or not.
123 *
124 * @api
125 */
126 public function register($prepend = FALSE) {
127 if ($this->_registered) {
128 return;
129 }
130 $civicrm_base_path = dirname(dirname(__DIR__));
131
132 $this->requireComposerAutoload();
133
134 // we do this to prevent a autoloader errors with joomla / 3rd party packages
135 // use absolute path since we dont know the content of include_path as yet
136 // CRM-11304
137 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
138 // Extensions rely on include_path-based autoloading
139 spl_autoload_register([$this, 'loadClass'], TRUE, $prepend);
140 $this->initHtmlPurifier($prepend);
141
142 $this->_registered = TRUE;
143 $packages_path = implode(DIRECTORY_SEPARATOR, [$civicrm_base_path, 'packages']);
144 $include_paths = [
145 '.',
146 $civicrm_base_path,
147 $packages_path,
148 ];
149 $include_paths = implode(PATH_SEPARATOR, $include_paths);
150 set_include_path($include_paths . PATH_SEPARATOR . get_include_path());
151 // @todo Why do we need to load this again?
152 $this->requireComposerAutoload();
153 }
154
155 /**
156 * Initialize HTML purifier class.
157 *
158 * @param string $prepend
159 */
160 public function initHtmlPurifier($prepend) {
161 if (class_exists('HTMLPurifier_Bootstrap')) {
162 // HTMLPurifier is already initialized, e.g. by the Drupal module.
163 return;
164 }
165
166 $htmlPurifierPath = $this->getHtmlPurifierPath();
167
168 if (FALSE === $htmlPurifierPath) {
169 // No HTMLPurifier available, e.g. during installation.
170 return;
171 }
172 require_once $htmlPurifierPath;
173 spl_autoload_register(['HTMLPurifier_Bootstrap', 'autoload'], TRUE, $prepend);
174 }
175
176 /**
177 * @return string|false
178 * Path to the file where the class HTMLPurifier_Bootstrap is defined, or
179 * FALSE, if such a file does not exist.
180 */
181 private function getHtmlPurifierPath() {
182 if (function_exists('libraries_get_path')
183 && ($path = libraries_get_path('htmlpurifier'))
184 && file_exists($file = $path . '/library/HTMLPurifier/Bootstrap.php')
185 ) {
186 // We are in Drupal 7, and the HTMLPurifier module is installed.
187 // Use Drupal's HTMLPurifier path, to avoid conflicts.
188 // @todo Verify that we are really in Drupal 7, and not in some other
189 // environment that happens to provide a 'libraries_get_path()' function.
190 return $file;
191 }
192
193 // we do this to prevent a autoloader errors with joomla / 3rd party packages
194 // Use absolute path, since we don't know the content of include_path yet.
195 // CRM-11304
196 $file = dirname(__FILE__) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
197 if (file_exists($file)) {
198 return $file;
199 }
200
201 return FALSE;
202 }
203
204 /**
205 * @param $class
206 */
207 public function loadClass($class) {
208 if ($class === 'CiviCRM_API3_Exception') {
209 //call internal error class api/Exception first
210 // allow api/Exception class call external error class
211 // CiviCRM_API3_Exception
212 require_once 'api/Exception.php';
213 }
214 if (
215 // Only load classes that clearly belong to CiviCRM.
216 // Note: api/v3 does not use classes, but api_v3's test-suite does
217 (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)) &&
218 // Do not load PHP 5.3 namespaced classes.
219 // (in a future version, maybe)
220 FALSE === strpos($class, '\\')
221 ) {
222 $file = strtr($class, '_', '/') . '.php';
223 // There is some question about the best way to do this.
224 // "require_once" is nice because it's simple and throws
225 // intelligible errors.
226 if (FALSE != stream_resolve_include_path($file)) {
227 require_once $file;
228 }
229 }
230 elseif (in_array($class, $this->civiTestClasses)) {
231 $file = "tests/phpunit/CiviTest/{$class}.php";
232 if (FALSE != stream_resolve_include_path($file)) {
233 require_once $file;
234 }
235 }
236 elseif ($class === 'CiviSeleniumSettings') {
237 if (!empty($GLOBALS['_CV'])) {
238 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.auto.php';
239 }
240 elseif (CRM_Utils_File::isIncludable('tests/phpunit/CiviTest/CiviSeleniumSettings.php')) {
241 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.php';
242 }
243 }
244 }
245
246 }