Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Loader.php - 166 lines - 3763 bytes - Summary - Text - Print

Description: Autoloader.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Autoloader.
  26   *
  27   * <code>
  28   * Txp::get('\Textpattern\Loader', '/path/to/directory')->register();
  29   * </code>
  30   *
  31   * @since   4.6.0
  32   * @package Autoloader
  33   */
  34  
  35  namespace Textpattern;
  36  
  37  class Loader
  38  {
  39      /**
  40       * Registered directory.
  41       *
  42       * @var string
  43       */
  44  
  45      protected $directory;
  46  
  47      /**
  48       * Registered namespace.
  49       *
  50       * @var string
  51       */
  52  
  53      protected $namespace;
  54  
  55      /**
  56       * Namespace separator.
  57       *
  58       * @var string
  59       */
  60  
  61      protected $separator;
  62  
  63      /**
  64       * File extension.
  65       *
  66       * @var string
  67       */
  68  
  69      protected $extension;
  70  
  71      /**
  72       * Registers the loader.
  73       *
  74       * @return bool FALSE on error
  75       */
  76  
  77      public function register()
  78      {
  79          global $trace;
  80  
  81          if ($this->directory) {
  82              $trace->log("[Textpattern autoload dir: '".str_replace(txpath.'/', '', $this->directory)."']");
  83  
  84              return spl_autoload_register(array($this, 'load'));
  85          }
  86  
  87          return false;
  88      }
  89  
  90      /**
  91       * Unregisters a loader.
  92       *
  93       * @return bool FALSE on error
  94       */
  95  
  96      public function unregister()
  97      {
  98          return spl_autoload_unregister(array($this, 'load'));
  99      }
 100  
 101      /**
 102       * Constructor.
 103       *
 104       * @param string $directory Registered vendors directory
 105       * @param string $namespace Limits the loader to a specific namespace
 106       * @param string $separator Namespace separator
 107       * @param string $extension File extension
 108       */
 109  
 110      public function __construct($directory, $namespace = null, $separator = '\\', $extension = '.php')
 111      {
 112          if (file_exists($directory) && is_dir($directory)) {
 113              $this->directory = $directory;
 114              $this->namespace = $namespace;
 115              $this->separator = $separator;
 116              $this->extension = $extension;
 117          }
 118      }
 119  
 120      /**
 121       * Loads a class.
 122       *
 123       * @param  string $class The class
 124       * @return bool
 125       */
 126  
 127      public function load($class)
 128      {
 129          global $trace;
 130  
 131          $request = $class;
 132  
 133          if ($this->namespace !== null && strpos($class, $this->namespace.$this->separator) !== 0 ||
 134              !preg_match('/^[\\a-zA-Z_\x7f-\xff][a-zA-Z0-9_\\\x7f-\xff]*$/', $class)
 135          ) {
 136              return false;
 137          }
 138  
 139          $file = $this->directory.'/';
 140          $divide = strripos($class, $this->separator);
 141  
 142          if ($divide !== false) {
 143              $namespace = substr($class, 0, $divide);
 144              $class = substr($class, $divide + 1);
 145              $file .= str_replace($this->separator, '/', $namespace).'/';
 146          }
 147  
 148          $file .= $class.$this->extension;
 149  
 150          if (is_readable($file)) {
 151              $trace->start("[Load: '".str_replace(txpath.'/', '', $file)."']");
 152              require_once $file;
 153  
 154              if (class_exists($request, false)) {
 155                  $trace->log("[Class loaded: '$class']");
 156                  $trace->stop();
 157  
 158                  return true;
 159              }
 160  
 161              $trace->stop();
 162          }
 163  
 164          return false;
 165      }
 166  }

title

Description

title

Description

title

Description

title

title

Body