Merge pull request #6006 from aputtu/patch-4
[civicrm-core.git] / CRM / Core / ClassLoader.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 32 * @copyright CiviCRM LLC (c) 2004-2015
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
a0ee3941
EM
45 /**
46 * @param bool $force
47 *
48 * @return object
49 */
00be9182 50 public static function &singleton($force = FALSE) {
6a488035
TO
51 if ($force || self::$_singleton === NULL) {
52 self::$_singleton = new CRM_Core_ClassLoader();
53 }
54 return self::$_singleton;
55 }
56
57 /**
58 * @var bool TRUE if previously registered
59 */
60 protected $_registered;
61
a0ee3941 62 /**
a0ee3941 63 */
6a488035
TO
64 protected function __construct() {
65 $this->_registered = FALSE;
66 }
67
68 /**
69 * Registers this instance as an autoloader.
70 *
6a0b768e
TO
71 * @param bool $prepend
72 * Whether to prepend the autoloader or not.
6a488035
TO
73 *
74 * @api
75 */
00be9182 76 public function register($prepend = FALSE) {
6a488035
TO
77 if ($this->_registered) {
78 return;
79 }
fa184193
TO
80 $civicrm_base_path = dirname(dirname(__DIR__));
81
ac0b2c9c 82 require_once dirname(dirname(__DIR__)) . '/vendor/autoload.php';
6a488035
TO
83
84 // we do this to prevent a autoloader errors with joomla / 3rd party packages
85 // use absolute path since we dont know the content of include_path as yet
86 // CRM-11304
fa184193
TO
87 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
88 // Extensions rely on include_path-based autoloading
89 spl_autoload_register(array($this, 'loadClass'), TRUE, $prepend);
c6af44d7 90 $this->initHtmlPurifier($prepend);
6a488035
TO
91
92 $this->_registered = TRUE;
fa184193
TO
93 $packages_path = implode(DIRECTORY_SEPARATOR, array($civicrm_base_path, 'packages'));
94 $include_paths = array(
95 '.',
96 $civicrm_base_path,
21dfd5f5 97 $packages_path,
fa184193
TO
98 );
99 $include_paths = implode(PATH_SEPARATOR, $include_paths);
100 set_include_path($include_paths . PATH_SEPARATOR . get_include_path());
ac0b2c9c 101 require_once "$civicrm_base_path/vendor/autoload.php";
6a488035
TO
102 }
103
7a9ab499
EM
104 /**
105 * Initialize HTML purifier class.
106 *
107 * @param string $prepend
108 */
00be9182 109 public function initHtmlPurifier($prepend) {
c6af44d7
C
110 if (class_exists('HTMLPurifier_Bootstrap')) {
111 // HTMLPurifier is already initialized, e.g. by the Drupal module.
112 return;
113 }
114
115 $htmlPurifierPath = $this->getHtmlPurifierPath();
116
117 if (FALSE === $htmlPurifierPath) {
118 // No HTMLPurifier available, e.g. during installation.
119 return;
120 }
121 require_once $htmlPurifierPath;
122 spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE, $prepend);
123 }
124
125 /**
126 * @return string|false
127 * Path to the file where the class HTMLPurifier_Bootstrap is defined, or
128 * FALSE, if such a file does not exist.
129 */
130 private function getHtmlPurifierPath() {
131 if (function_exists('libraries_get_path')
132 && ($path = libraries_get_path('htmlpurifier'))
133 && file_exists($file = $path . '/library/HTMLPurifier/Bootstrap.php')
134 ) {
135 // We are in Drupal 7, and the HTMLPurifier module is installed.
136 // Use Drupal's HTMLPurifier path, to avoid conflicts.
137 // @todo Verify that we are really in Drupal 7, and not in some other
138 // environment that happens to provide a 'libraries_get_path()' function.
139 return $file;
140 }
141
142 // we do this to prevent a autoloader errors with joomla / 3rd party packages
143 // Use absolute path, since we don't know the content of include_path yet.
144 // CRM-11304
145 $file = dirname(__FILE__) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
146 if (file_exists($file)) {
147 return $file;
148 }
149
150 return FALSE;
151 }
152
a0ee3941
EM
153 /**
154 * @param $class
155 */
00be9182 156 public function loadClass($class) {
6a488035
TO
157 if (
158 // Only load classes that clearly belong to CiviCRM.
8581f9ae
TO
159 // Note: api/v3 does not use classes, but api_v3's test-suite does
160 (0 === strncmp($class, 'CRM_', 4) || 0 === strncmp($class, 'api_v3_', 7) || 0 === strncmp($class, 'WebTest_', 8)) &&
6a488035
TO
161 // Do not load PHP 5.3 namespaced classes.
162 // (in a future version, maybe)
163 FALSE === strpos($class, '\\')
164 ) {
165 $file = strtr($class, '_', '/') . '.php';
166 // There is some question about the best way to do this.
167 // "require_once" is nice because it's simple and throws
a03a3680
TO
168 // intelligible errors.
169 if (FALSE != stream_resolve_include_path($file)) {
170 require_once $file;
171 }
6a488035
TO
172 }
173 }
a03a3680 174
6a488035 175}