Got bin/cli.php bootstrapping Drupal (or at least not crashing when trying to do so)
[civicrm-core.git] / CRM / Core / ClassLoader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 * @var bool TRUE if previously registered
67 */
68 protected $_registered;
69
70 /**
71 */
72 protected function __construct() {
73 $this->_registered = FALSE;
74 $this->civiTestClasses = array(
75 'CiviCaseTestCase',
76 'CiviDBAssert',
77 'CiviMailUtils',
78 'CiviReportTestCase',
79 'CiviSeleniumTestCase',
80 'CiviTestSuite',
81 'CiviUnitTestCase',
82 'CiviEndToEndTestCase',
83 'Contact',
84 'ContributionPage',
85 'Custom',
86 'Event',
87 'Membership',
88 'Participant',
89 'PaypalPro',
90 );
91 }
92
93 /**
94 * Requires the autoload.php generated by composer
95 *
96 * @return void
97 */
98 protected function requireComposerAutoload() {
99 $civicrm_base_path = dirname(dirname(__DIR__));
100 $top_path = dirname(dirname(dirname(dirname(dirname(__DIR__)))));
101
102 if (file_exists($civicrm_base_path . '/vendor/autoload.php')) {
103 require_once $civicrm_base_path . '/vendor/autoload.php';
104 }
105 elseif (file_exists($top_path . '/vendor/autoload.php')) {
106 require_once $top_path . '/vendor/autoload.php';
107 }
108 }
109
110 /**
111 * Registers this instance as an autoloader.
112 *
113 * @param bool $prepend
114 * Whether to prepend the autoloader or not.
115 *
116 * @api
117 */
118 public function register($prepend = FALSE) {
119 if ($this->_registered) {
120 return;
121 }
122 $civicrm_base_path = dirname(dirname(__DIR__));
123
124 $this->requireComposerAutoload();
125
126 // we do this to prevent a autoloader errors with joomla / 3rd party packages
127 // use absolute path since we dont know the content of include_path as yet
128 // CRM-11304
129 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
130 // Extensions rely on include_path-based autoloading
131 spl_autoload_register(array($this, 'loadClass'), TRUE, $prepend);
132 $this->initHtmlPurifier($prepend);
133
134 $this->_registered = TRUE;
135 $packages_path = implode(DIRECTORY_SEPARATOR, array($civicrm_base_path, 'packages'));
136 $include_paths = array(
137 '.',
138 $civicrm_base_path,
139 $packages_path,
140 );
141 $include_paths = implode(PATH_SEPARATOR, $include_paths);
142 set_include_path($include_paths . PATH_SEPARATOR . get_include_path());
143 // @todo Why do we need to load this again?
144 $this->requireComposerAutoload();
145 }
146
147 /**
148 * Initialize HTML purifier class.
149 *
150 * @param string $prepend
151 */
152 public function initHtmlPurifier($prepend) {
153 if (class_exists('HTMLPurifier_Bootstrap')) {
154 // HTMLPurifier is already initialized, e.g. by the Drupal module.
155 return;
156 }
157
158 $htmlPurifierPath = $this->getHtmlPurifierPath();
159
160 if (FALSE === $htmlPurifierPath) {
161 // No HTMLPurifier available, e.g. during installation.
162 return;
163 }
164 require_once $htmlPurifierPath;
165 spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE, $prepend);
166 }
167
168 /**
169 * @return string|false
170 * Path to the file where the class HTMLPurifier_Bootstrap is defined, or
171 * FALSE, if such a file does not exist.
172 */
173 private function getHtmlPurifierPath() {
174 if (function_exists('libraries_get_path')
175 && ($path = libraries_get_path('htmlpurifier'))
176 && file_exists($file = $path . '/library/HTMLPurifier/Bootstrap.php')
177 ) {
178 // We are in Drupal 7, and the HTMLPurifier module is installed.
179 // Use Drupal's HTMLPurifier path, to avoid conflicts.
180 // @todo Verify that we are really in Drupal 7, and not in some other
181 // environment that happens to provide a 'libraries_get_path()' function.
182 return $file;
183 }
184
185 // we do this to prevent a autoloader errors with joomla / 3rd party packages
186 // Use absolute path, since we don't know the content of include_path yet.
187 // CRM-11304
188 $file = dirname(__FILE__) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
189 if (file_exists($file)) {
190 return $file;
191 }
192
193 return FALSE;
194 }
195
196 /**
197 * @param $class
198 */
199 public function loadClass($class) {
200 if (
201 // Only load classes that clearly belong to CiviCRM.
202 // Note: api/v3 does not use classes, but api_v3's test-suite does
203 (0 === strncmp($class, 'CRM_', 4) || 0 === strncmp($class, 'api_v3_', 7) || 0 === strncmp($class, 'WebTest_', 8) || 0 === strncmp($class, 'E2E_', 4)) &&
204 // Do not load PHP 5.3 namespaced classes.
205 // (in a future version, maybe)
206 FALSE === strpos($class, '\\')
207 ) {
208 $file = strtr($class, '_', '/') . '.php';
209 // There is some question about the best way to do this.
210 // "require_once" is nice because it's simple and throws
211 // intelligible errors.
212 if (FALSE != stream_resolve_include_path($file)) {
213 require_once $file;
214 }
215 }
216 elseif (in_array($class, $this->civiTestClasses)) {
217 $file = "tests/phpunit/CiviTest/{$class}.php";
218 if (FALSE != stream_resolve_include_path($file)) {
219 require_once $file;
220 }
221 }
222 elseif ($class === 'CiviSeleniumSettings') {
223 if (!empty($GLOBALS['_CV'])) {
224 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.auto.php';
225 }
226 elseif (CRM_Utils_File::isIncludable('tests/phpunit/CiviTest/CiviSeleniumSettings.php')) {
227 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.php';
228 }
229 }
230 }
231
232 }