Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Loader.php
Go to the documentation of this file.
1 <?php
31 {
52  public static function loadClass($class, $dirs = null)
53  {
54  if (class_exists($class, false) || interface_exists($class, false)) {
55  return;
56  }
57 
58  if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
59  #require_once 'Zend/Exception.php';
60  throw new Zend_Exception('Directory argument must be a string or an array');
61  }
62 
64 
65  if (!empty($dirs)) {
66  // use the autodiscovered path
67  $dirPath = dirname($file);
68  if (is_string($dirs)) {
69  $dirs = explode(PATH_SEPARATOR, $dirs);
70  }
71  foreach ($dirs as $key => $dir) {
72  if ($dir == '.') {
73  $dirs[$key] = $dirPath;
74  } else {
75  $dir = rtrim($dir, '\\/');
76  $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
77  }
78  }
79  $file = basename($file);
80  self::loadFile($file, $dirs, true);
81  } else {
82  self::loadFile($file, null, true);
83  }
84 
85  if (!class_exists($class, false) && !interface_exists($class, false)) {
86  #require_once 'Zend/Exception.php';
87  throw new Zend_Exception("File \"$file\" does not exist or class \"$class\" was not found in the file");
88  }
89  }
90 
114  public static function loadFile($filename, $dirs = null, $once = false)
115  {
116  self::_securityCheck($filename);
117 
121  $incPath = false;
122  if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
123  if (is_array($dirs)) {
124  $dirs = implode(PATH_SEPARATOR, $dirs);
125  }
126  $incPath = get_include_path();
127  set_include_path($dirs . PATH_SEPARATOR . $incPath);
128  }
129 
133  if ($once) {
134  include_once $filename;
135  } else {
136  include $filename;
137  }
138 
142  if ($incPath) {
143  set_include_path($incPath);
144  }
145 
146  return true;
147  }
148 
162  public static function isReadable($filename)
163  {
164  if (is_readable($filename)) {
165  // Return early if the filename is readable without needing the
166  // include_path
167  return true;
168  }
169 
170  if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
171  && preg_match('/^[a-z]:/i', $filename)
172  ) {
173  // If on windows, and path provided is clearly an absolute path,
174  // return false immediately
175  return false;
176  }
177 
178  foreach (self::explodeIncludePath() as $path) {
179  if ($path == '.') {
180  if (is_readable($filename)) {
181  return true;
182  }
183  continue;
184  }
185  $file = $path . '/' . $filename;
186  if (is_readable($file)) {
187  return true;
188  }
189  }
190  return false;
191  }
192 
202  public static function explodeIncludePath($path = null)
203  {
204  if (null === $path) {
205  $path = get_include_path();
206  }
207 
208  if (PATH_SEPARATOR == ':') {
209  // On *nix systems, include_paths which include paths with a stream
210  // schema cannot be safely explode'd, so we have to be a bit more
211  // intelligent in the approach.
212  $paths = preg_split('#:(?!//)#', $path);
213  } else {
214  $paths = explode(PATH_SEPARATOR, $path);
215  }
216  return $paths;
217  }
218 
231  public static function autoload($class)
232  {
233  trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead', E_USER_NOTICE);
234  try {
236  return $class;
237  } catch (Exception $e) {
238  return false;
239  }
240  }
241 
252  public static function registerAutoload($class = 'Zend_Loader', $enabled = true)
253  {
254  trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead', E_USER_NOTICE);
255  #require_once 'Zend/Loader/Autoloader.php';
256  $autoloader = Zend_Loader_Autoloader::getInstance();
257  $autoloader->setFallbackAutoloader(true);
258 
259  if ('Zend_Loader' != $class) {
261  $methods = get_class_methods($class);
262  if (!in_array('autoload', (array) $methods)) {
263  #require_once 'Zend/Exception.php';
264  throw new Zend_Exception("The class \"$class\" does not have an autoload() method");
265  }
266 
267  $callback = array($class, 'autoload');
268 
269  if ($enabled) {
270  $autoloader->pushAutoloader($callback);
271  } else {
272  $autoloader->removeAutoloader($callback);
273  }
274  }
275  }
276 
284  protected static function _securityCheck($filename)
285  {
289  if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
290  #require_once 'Zend/Exception.php';
291  throw new Zend_Exception('Security check: Illegal character in filename');
292  }
293  }
294 
309  protected static function _includeFile($filespec, $once = false)
310  {
311  if ($once) {
312  return include_once $filespec;
313  } else {
314  return include $filespec ;
315  }
316  }
317 
330  public static function standardiseFile($file)
331  {
332  $fileName = ltrim($file, '\\');
333  $file = '';
334  $namespace = '';
335  if ($lastNsPos = strripos($fileName, '\\')) {
336  $namespace = substr($fileName, 0, $lastNsPos);
337  $fileName = substr($fileName, $lastNsPos + 1);
338  $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
339  }
340  $file .= str_replace('_', DIRECTORY_SEPARATOR, $fileName) . '.php';
341  return $file;
342  }
343 }
static loadClass($class, $dirs=null)
Definition: Loader.php:52
static explodeIncludePath($path=null)
Definition: Loader.php:202
static isReadable($filename)
Definition: Loader.php:162
static _includeFile($filespec, $once=false)
Definition: Loader.php:309
$fileName
Definition: translate.phtml:15
$methods
Definition: billing.phtml:71
$_option $_optionId $class
Definition: date.phtml:13
static _securityCheck($filename)
Definition: Loader.php:284
static registerAutoload($class='Zend_Loader', $enabled=true)
Definition: Loader.php:252
static loadFile($filename, $dirs=null, $once=false)
Definition: Loader.php:114
static autoload($class)
Definition: Loader.php:231
static standardiseFile($file)
Definition: Loader.php:330
$paths
Definition: _bootstrap.php:83