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