3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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
44 private static $_singleton = NULL;
51 static function &singleton($force = FALSE) {
52 if ($force || self
::$_singleton === NULL) {
53 self
::$_singleton = new CRM_Core_ClassLoader();
55 return self
::$_singleton;
59 * @var bool TRUE if previously registered
61 protected $_registered;
66 protected function __construct() {
67 $this->_registered
= FALSE;
71 * Registers this instance as an autoloader.
73 * @param Boolean $prepend Whether to prepend the autoloader or not
77 function register($prepend = FALSE) {
78 if ($this->_registered
) {
81 $civicrm_base_path = dirname(dirname(__DIR__
));
83 require_once dirname(dirname(__DIR__
)) . '/packages/vendor/autoload.php';
85 // we do this to prevent a autoloader errors with joomla / 3rd party packages
86 // use absolute path since we dont know the content of include_path as yet
88 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
89 // Extensions rely on include_path-based autoloading
90 spl_autoload_register(array($this, 'loadClass'), TRUE, $prepend);
91 $this->initHtmlPurifier($prepend);
93 $this->_registered
= TRUE;
94 $packages_path = implode(DIRECTORY_SEPARATOR
, array($civicrm_base_path, 'packages'));
95 $include_paths = array(
100 $include_paths = implode(PATH_SEPARATOR
, $include_paths);
101 set_include_path($include_paths . PATH_SEPARATOR
. get_include_path());
102 require_once "$civicrm_base_path/packages/vendor/autoload.php";
105 function initHtmlPurifier($prepend) {
106 if (class_exists('HTMLPurifier_Bootstrap')) {
107 // HTMLPurifier is already initialized, e.g. by the Drupal module.
111 $htmlPurifierPath = $this->getHtmlPurifierPath();
113 if (FALSE === $htmlPurifierPath) {
114 // No HTMLPurifier available, e.g. during installation.
117 require_once $htmlPurifierPath;
118 spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE, $prepend);
122 * @return string|false
123 * Path to the file where the class HTMLPurifier_Bootstrap is defined, or
124 * FALSE, if such a file does not exist.
126 private function getHtmlPurifierPath() {
127 if (function_exists('libraries_get_path')
128 && ($path = libraries_get_path('htmlpurifier'))
129 && file_exists($file = $path . '/library/HTMLPurifier/Bootstrap.php')
131 // We are in Drupal 7, and the HTMLPurifier module is installed.
132 // Use Drupal's HTMLPurifier path, to avoid conflicts.
133 // @todo Verify that we are really in Drupal 7, and not in some other
134 // environment that happens to provide a 'libraries_get_path()' function.
138 // we do this to prevent a autoloader errors with joomla / 3rd party packages
139 // Use absolute path, since we don't know the content of include_path yet.
141 $file = dirname(__FILE__
) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
142 if (file_exists($file)) {
152 function loadClass($class) {
154 // Only load classes that clearly belong to CiviCRM.
155 0 === strncmp($class, 'CRM_', 4) &&
156 // Do not load PHP 5.3 namespaced classes.
157 // (in a future version, maybe)
158 FALSE === strpos($class, '\\')
160 $file = strtr($class, '_', '/') . '.php';
161 // There is some question about the best way to do this.
162 // "require_once" is nice because it's simple and throws
163 // intelligible errors. The down side is that autoloaders
164 // down the chain cannot try to find the file if we fail.
165 require_once ($file);