Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | ||
a0ee3941 EM |
46 | /** |
47 | * @param bool $force | |
48 | * | |
49 | * @return object | |
50 | */ | |
6a488035 TO |
51 | 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 | ||
a0ee3941 EM |
63 | /** |
64 | * | |
65 | */ | |
6a488035 TO |
66 | protected function __construct() { |
67 | $this->_registered = FALSE; | |
68 | } | |
69 | ||
70 | /** | |
71 | * Registers this instance as an autoloader. | |
72 | * | |
73 | * @param Boolean $prepend Whether to prepend the autoloader or not | |
74 | * | |
75 | * @api | |
76 | */ | |
77 | function register($prepend = FALSE) { | |
78 | if ($this->_registered) { | |
79 | return; | |
80 | } | |
fa184193 TO |
81 | $civicrm_base_path = dirname(dirname(__DIR__)); |
82 | ||
83 | require_once dirname(dirname(__DIR__)) . '/packages/vendor/autoload.php'; | |
6a488035 TO |
84 | |
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 | |
87 | // CRM-11304 | |
fa184193 TO |
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); | |
c6af44d7 | 91 | $this->initHtmlPurifier($prepend); |
6a488035 TO |
92 | |
93 | $this->_registered = TRUE; | |
fa184193 TO |
94 | $packages_path = implode(DIRECTORY_SEPARATOR, array($civicrm_base_path, 'packages')); |
95 | $include_paths = array( | |
96 | '.', | |
97 | $civicrm_base_path, | |
98 | $packages_path | |
99 | ); | |
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"; | |
6a488035 TO |
103 | } |
104 | ||
c6af44d7 C |
105 | function initHtmlPurifier($prepend) { |
106 | if (class_exists('HTMLPurifier_Bootstrap')) { | |
107 | // HTMLPurifier is already initialized, e.g. by the Drupal module. | |
108 | return; | |
109 | } | |
110 | ||
111 | $htmlPurifierPath = $this->getHtmlPurifierPath(); | |
112 | ||
113 | if (FALSE === $htmlPurifierPath) { | |
114 | // No HTMLPurifier available, e.g. during installation. | |
115 | return; | |
116 | } | |
117 | require_once $htmlPurifierPath; | |
118 | spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE, $prepend); | |
119 | } | |
120 | ||
121 | /** | |
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. | |
125 | */ | |
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') | |
130 | ) { | |
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. | |
135 | return $file; | |
136 | } | |
137 | ||
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. | |
140 | // CRM-11304 | |
141 | $file = dirname(__FILE__) . '/../../packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php'; | |
142 | if (file_exists($file)) { | |
143 | return $file; | |
144 | } | |
145 | ||
146 | return FALSE; | |
147 | } | |
148 | ||
a0ee3941 EM |
149 | /** |
150 | * @param $class | |
151 | */ | |
6a488035 TO |
152 | function loadClass($class) { |
153 | if ( | |
154 | // Only load classes that clearly belong to CiviCRM. | |
8581f9ae TO |
155 | // Note: api/v3 does not use classes, but api_v3's test-suite does |
156 | (0 === strncmp($class, 'CRM_', 4) || 0 === strncmp($class, 'api_v3_', 7) || 0 === strncmp($class, 'WebTest_', 8)) && | |
6a488035 TO |
157 | // Do not load PHP 5.3 namespaced classes. |
158 | // (in a future version, maybe) | |
159 | FALSE === strpos($class, '\\') | |
160 | ) { | |
161 | $file = strtr($class, '_', '/') . '.php'; | |
162 | // There is some question about the best way to do this. | |
163 | // "require_once" is nice because it's simple and throws | |
164 | // intelligible errors. The down side is that autoloaders | |
165 | // down the chain cannot try to find the file if we fail. | |
166 | require_once ($file); | |
167 | } | |
168 | } | |
169 | } |