Comments & e-notice fixes
[civicrm-core.git] / CRM / Core / ClassLoader.php
CommitLineData
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 */
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 }
fa184193
TO
73 $civicrm_base_path = dirname(dirname(__DIR__));
74
75 require_once dirname(dirname(__DIR__)) . '/packages/vendor/autoload.php';
6a488035
TO
76
77 // we do this to prevent a autoloader errors with joomla / 3rd party packages
78 // use absolute path since we dont know the content of include_path as yet
79 // CRM-11304
80
81 // since HTML Purifier could potentially be loaded / used by other modules / components
82 // lets check it its already loaded
83 // we also check if the bootstrap file exists since during install of a drupal distro profile
84 // the files might not exists, in which case we skip loading the file
85 // if you change the below, please test on Joomla and also PCP pages
86 $includeHTMLPurifier = TRUE;
fa184193 87 $htmlPurifierPath = "$civicrm_base_path/packages/IDS/vendors/htmlpurifier/HTMLPurifier/Bootstrap.php";
6a488035
TO
88 if (
89 class_exists('HTMLPurifier_Bootstrap') ||
90 !file_exists($htmlPurifierPath)
91 ) {
92 $includeHTMLPurifier = FALSE;
93 }
94 else {
95 require_once $htmlPurifierPath;
96 }
97
fa184193
TO
98 // TODO Remove this autoloader. For civicrm-core and civicrm-packages, the composer autoloader works fine.
99 // Extensions rely on include_path-based autoloading
100 spl_autoload_register(array($this, 'loadClass'), TRUE, $prepend);
101 if ($includeHTMLPurifier) {
102 spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'), TRUE, $prepend);
6a488035
TO
103 }
104
105 $this->_registered = TRUE;
fa184193
TO
106 $packages_path = implode(DIRECTORY_SEPARATOR, array($civicrm_base_path, 'packages'));
107 $include_paths = array(
108 '.',
109 $civicrm_base_path,
110 $packages_path
111 );
112 $include_paths = implode(PATH_SEPARATOR, $include_paths);
113 set_include_path($include_paths . PATH_SEPARATOR . get_include_path());
114 require_once "$civicrm_base_path/packages/vendor/autoload.php";
6a488035
TO
115 }
116
117 function loadClass($class) {
118 if (
119 // Only load classes that clearly belong to CiviCRM.
120 0 === strncmp($class, 'CRM_', 4) &&
121 // Do not load PHP 5.3 namespaced classes.
122 // (in a future version, maybe)
123 FALSE === strpos($class, '\\')
124 ) {
125 $file = strtr($class, '_', '/') . '.php';
126 // There is some question about the best way to do this.
127 // "require_once" is nice because it's simple and throws
128 // intelligible errors. The down side is that autoloaders
129 // down the chain cannot try to find the file if we fail.
130 require_once ($file);
131 }
132 }
133}