Merge pull request #10532 from monishdeb/CRM-20488_soft_credit_organization
[civicrm-core.git] / CRM / Core / ClassLoader.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 *
31 * @package CRM
0f03f337 32 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
33 * $Id$
34 *
35 */
36class 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
6a488035
TO
42 */
43 private static $_singleton = NULL;
44
649d6dd0
TO
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
a0ee3941
EM
53 /**
54 * @param bool $force
55 *
56 * @return object
57 */
00be9182 58 public static function &singleton($force = FALSE) {
6a488035
TO
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
a0ee3941 70 /**
a0ee3941 71 */
6a488035
TO
72 protected function __construct() {
73 $this->_registered = FALSE;
649d6dd0
TO
74 $this->civiTestClasses = array(
75 'CiviCaseTestCase',
76 'CiviDBAssert',
77 'CiviMailUtils',
78 'CiviReportTestCase',
79 'CiviSeleniumTestCase',
649d6dd0
TO
80 'CiviTestSuite',
81 'CiviUnitTestCase',
9e80c052 82 'CiviEndToEndTestCase',
649d6dd0
TO
83 'Contact',
84 'ContributionPage',
85 'Custom',
86 'Event',
87 'Membership',
88 'Participant',
89 'PaypalPro',
90 );
6a488035
TO
91 }
92
93 /**
94 * Registers this instance as an autoloader.
95 *
6a0b768e
TO
96 * @param bool $prepend
97 * Whether to prepend the autoloader or not.
6a488035
TO
98 *
99 * @api
100 */
00be9182 101 public function register($prepend = FALSE) {
6a488035
TO
102 if ($this->_registered) {
103 return;
104 }
fa184193
TO
105 $civicrm_base_path = dirname(dirname(__DIR__));
106
ac0b2c9c 107 require_once dirname(dirname(__DIR__)) . '/vendor/autoload.php';
6a488035
TO
108
109 // we do this to prevent a autoloader errors with joomla / 3rd party packages
110 // use absolute path since we dont know the content of include_path as yet
111 // CRM-11304
fa184193
TO
112 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
113 // Extensions rely on include_path-based autoloading
114 spl_autoload_register(array($this, 'loadClass'), TRUE, $prepend);
c6af44d7 115 $this->initHtmlPurifier($prepend);
6a488035
TO
116
117 $this->_registered = TRUE;
fa184193
TO
118 $packages_path = implode(DIRECTORY_SEPARATOR, array($civicrm_base_path, 'packages'));
119 $include_paths = array(
120 '.',
121 $civicrm_base_path,
21dfd5f5 122 $packages_path,
fa184193
TO
123 );
124 $include_paths = implode(PATH_SEPARATOR, $include_paths);
125 set_include_path($include_paths . PATH_SEPARATOR . get_include_path());
ac0b2c9c 126 require_once "$civicrm_base_path/vendor/autoload.php";
6a488035
TO
127 }
128
7a9ab499
EM
129 /**
130 * Initialize HTML purifier class.
131 *
132 * @param string $prepend
133 */
00be9182 134 public function initHtmlPurifier($prepend) {
c6af44d7
C
135 if (class_exists('HTMLPurifier_Bootstrap')) {
136 // HTMLPurifier is already initialized, e.g. by the Drupal module.
137 return;
138 }
139
140 $htmlPurifierPath = $this->getHtmlPurifierPath();
141
142 if (FALSE === $htmlPurifierPath) {
143 // No HTMLPurifier available, e.g. during installation.
144 return;
145 }
146 require_once $htmlPurifierPath;
147 spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE, $prepend);
148 }
149
150 /**
151 * @return string|false
152 * Path to the file where the class HTMLPurifier_Bootstrap is defined, or
153 * FALSE, if such a file does not exist.
154 */
155 private function getHtmlPurifierPath() {
156 if (function_exists('libraries_get_path')
157 && ($path = libraries_get_path('htmlpurifier'))
158 && file_exists($file = $path . '/library/HTMLPurifier/Bootstrap.php')
159 ) {
160 // We are in Drupal 7, and the HTMLPurifier module is installed.
161 // Use Drupal's HTMLPurifier path, to avoid conflicts.
162 // @todo Verify that we are really in Drupal 7, and not in some other
163 // environment that happens to provide a 'libraries_get_path()' function.
164 return $file;
165 }
166
167 // we do this to prevent a autoloader errors with joomla / 3rd party packages
168 // Use absolute path, since we don't know the content of include_path yet.
169 // CRM-11304
170 $file = dirname(__FILE__) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
171 if (file_exists($file)) {
172 return $file;
173 }
174
175 return FALSE;
176 }
177
a0ee3941
EM
178 /**
179 * @param $class
180 */
00be9182 181 public function loadClass($class) {
6a488035
TO
182 if (
183 // Only load classes that clearly belong to CiviCRM.
8581f9ae 184 // Note: api/v3 does not use classes, but api_v3's test-suite does
c5d26c27 185 (0 === strncmp($class, 'CRM_', 4) || 0 === strncmp($class, 'api_v3_', 7) || 0 === strncmp($class, 'WebTest_', 8) || 0 === strncmp($class, 'E2E_', 4)) &&
6a488035
TO
186 // Do not load PHP 5.3 namespaced classes.
187 // (in a future version, maybe)
188 FALSE === strpos($class, '\\')
189 ) {
190 $file = strtr($class, '_', '/') . '.php';
191 // There is some question about the best way to do this.
192 // "require_once" is nice because it's simple and throws
a03a3680
TO
193 // intelligible errors.
194 if (FALSE != stream_resolve_include_path($file)) {
195 require_once $file;
196 }
6a488035 197 }
649d6dd0
TO
198 elseif (in_array($class, $this->civiTestClasses)) {
199 $file = "tests/phpunit/CiviTest/{$class}.php";
200 if (FALSE != stream_resolve_include_path($file)) {
201 require_once $file;
202 }
203 }
204 elseif ($class === 'CiviSeleniumSettings') {
205 if (!empty($GLOBALS['_CV'])) {
206 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.auto.php';
207 }
208 elseif (CRM_Utils_File::isIncludable('tests/phpunit/CiviTest/CiviSeleniumSettings.php')) {
209 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.php';
210 }
211 }
6a488035 212 }
a03a3680 213
6a488035 214}