Merge pull request #2157 from kurund/CRM-13896
[civicrm-core.git] / CRM / Core / ClassLoader.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
42 * @static
43 */
44 private static $_singleton = NULL;
45
46 static function &singleton($force = FALSE) {
47 if ($force || self::$_singleton === NULL) {
48 self::$_singleton = new CRM_Core_ClassLoader();
49 }
50 return self::$_singleton;
51 }
52
53 /**
54 * @var bool TRUE if previously registered
55 */
56 protected $_registered;
57
58 protected function __construct() {
59 $this->_registered = FALSE;
60 }
61
62 /**
63 * Registers this instance as an autoloader.
64 *
65 * @param Boolean $prepend Whether to prepend the autoloader or not
66 *
67 * @api
68 */
69 function register($prepend = FALSE) {
70 if ($this->_registered) {
71 return;
72 }
73
74 // we do this to prevent a autoloader errors with joomla / 3rd party packages
75 // use absolute path since we dont know the content of include_path as yet
76 // CRM-11304
77
78 // since HTML Purifier could potentially be loaded / used by other modules / components
79 // lets check it its already loaded
80 // we also check if the bootstrap file exists since during install of a drupal distro profile
81 // the files might not exists, in which case we skip loading the file
82 // if you change the below, please test on Joomla and also PCP pages
83 $includeHTMLPurifier = TRUE;
84 $htmlPurifierPath = dirname(__FILE__) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php';
85 if (
86 class_exists('HTMLPurifier_Bootstrap') ||
87 !file_exists($htmlPurifierPath)
88 ) {
89 $includeHTMLPurifier = FALSE;
90 }
91 else {
92 require_once $htmlPurifierPath;
93 }
94
95 if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
96 spl_autoload_register(array($this, 'loadClass'), TRUE, $prepend);
97 if ($includeHTMLPurifier) {
98 spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE, $prepend);
99 }
100 }
101 else {
102 // http://www.php.net/manual/de/function.spl-autoload-register.php#107362
103 // "when specifying the third parameter (prepend), the function will fail badly in PHP 5.2"
104 spl_autoload_register(array($this, 'loadClass'), TRUE);
105 if ($includeHTMLPurifier) {
106 spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE);
107 }
108 }
109
110 $this->_registered = TRUE;
111 }
112
113 function loadClass($class) {
114 if (
115 // Only load classes that clearly belong to CiviCRM.
116 0 === strncmp($class, 'CRM_', 4) &&
117 // Do not load PHP 5.3 namespaced classes.
118 // (in a future version, maybe)
119 FALSE === strpos($class, '\\')
120 ) {
121 $file = strtr($class, '_', '/') . '.php';
122 // There is some question about the best way to do this.
123 // "require_once" is nice because it's simple and throws
124 // intelligible errors. The down side is that autoloaders
125 // down the chain cannot try to find the file if we fail.
126 require_once ($file);
127 }
128 }
129}
130