3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2016
36 class CRM_Core_ClassLoader
{
39 * We only need one instance of this object. So we use the singleton
40 * pattern and cache the instance in this variable
43 private static $_singleton = NULL;
46 * The classes in CiviTest have ucky, non-standard naming.
49 * Array(string $className => string $filePath).
51 private $civiTestClasses;
58 public static function &singleton($force = FALSE) {
59 if ($force || self
::$_singleton === NULL) {
60 self
::$_singleton = new CRM_Core_ClassLoader();
62 return self
::$_singleton;
66 * @var bool TRUE if previously registered
68 protected $_registered;
72 protected function __construct() {
73 $this->_registered
= FALSE;
74 $this->civiTestClasses
= array(
79 'CiviSeleniumTestCase',
93 * Registers this instance as an autoloader.
95 * @param bool $prepend
96 * Whether to prepend the autoloader or not.
100 public function register($prepend = FALSE) {
101 if ($this->_registered
) {
104 $civicrm_base_path = dirname(dirname(__DIR__
));
106 require_once dirname(dirname(__DIR__
)) . '/vendor/autoload.php';
108 // we do this to prevent a autoloader errors with joomla / 3rd party packages
109 // use absolute path since we dont know the content of include_path as yet
111 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
112 // Extensions rely on include_path-based autoloading
113 spl_autoload_register(array($this, 'loadClass'), TRUE, $prepend);
114 $this->initHtmlPurifier($prepend);
116 $this->_registered
= TRUE;
117 $packages_path = implode(DIRECTORY_SEPARATOR
, array($civicrm_base_path, 'packages'));
118 $include_paths = array(
123 $include_paths = implode(PATH_SEPARATOR
, $include_paths);
124 set_include_path($include_paths . PATH_SEPARATOR
. get_include_path());
125 require_once "$civicrm_base_path/vendor/autoload.php";
129 * Initialize HTML purifier class.
131 * @param string $prepend
133 public function initHtmlPurifier($prepend) {
134 if (class_exists('HTMLPurifier_Bootstrap')) {
135 // HTMLPurifier is already initialized, e.g. by the Drupal module.
139 $htmlPurifierPath = $this->getHtmlPurifierPath();
141 if (FALSE === $htmlPurifierPath) {
142 // No HTMLPurifier available, e.g. during installation.
145 require_once $htmlPurifierPath;
146 spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE, $prepend);
150 * @return string|false
151 * Path to the file where the class HTMLPurifier_Bootstrap is defined, or
152 * FALSE, if such a file does not exist.
154 private function getHtmlPurifierPath() {
155 if (function_exists('libraries_get_path')
156 && ($path = libraries_get_path('htmlpurifier'))
157 && file_exists($file = $path . '/library/HTMLPurifier/Bootstrap.php')
159 // We are in Drupal 7, and the HTMLPurifier module is installed.
160 // Use Drupal's HTMLPurifier path, to avoid conflicts.
161 // @todo Verify that we are really in Drupal 7, and not in some other
162 // environment that happens to provide a 'libraries_get_path()' function.
166 // we do this to prevent a autoloader errors with joomla / 3rd party packages
167 // Use absolute path, since we don't know the content of include_path yet.
169 $file = dirname(__FILE__
) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
170 if (file_exists($file)) {
180 public function loadClass($class) {
182 // Only load classes that clearly belong to CiviCRM.
183 // Note: api/v3 does not use classes, but api_v3's test-suite does
184 (0 === strncmp($class, 'CRM_', 4) ||
0 === strncmp($class, 'api_v3_', 7) ||
0 === strncmp($class, 'WebTest_', 8)) &&
185 // Do not load PHP 5.3 namespaced classes.
186 // (in a future version, maybe)
187 FALSE === strpos($class, '\\')
189 $file = strtr($class, '_', '/') . '.php';
190 // There is some question about the best way to do this.
191 // "require_once" is nice because it's simple and throws
192 // intelligible errors.
193 if (FALSE != stream_resolve_include_path($file)) {
197 elseif (in_array($class, $this->civiTestClasses
)) {
198 $file = "tests/phpunit/CiviTest/{$class}.php";
199 if (FALSE != stream_resolve_include_path($file)) {
203 elseif ($class === 'CiviSeleniumSettings') {
204 if (!empty($GLOBALS['_CV'])) {
205 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.auto.php';
207 elseif (CRM_Utils_File
::isIncludable('tests/phpunit/CiviTest/CiviSeleniumSettings.php')) {
208 require_once 'tests/phpunit/CiviTest/CiviSeleniumSettings.php';