Textpattern | PHP Cross Reference | Content Management Systems |
Description: Autoloader.
1 <?php 2 3 /* 4 * Textpattern Content Management System 5 * http://textpattern.com 6 * 7 * Copyright (C) 2016 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 <http://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 return spl_autoload_register(array($this, 'load')); 84 } 85 86 return false; 87 } 88 89 /** 90 * Unregisters a loader. 91 * 92 * @return bool FALSE on error 93 */ 94 95 public function unregister() 96 { 97 return spl_autoload_unregister(array($this, 'load')); 98 } 99 100 /** 101 * Constructor. 102 * 103 * @param string $directory Registered vendors directory 104 * @param string $namespace Limits the loader to a specific namespace 105 * @param string $separator Namespace separator 106 * @param string $extension File extension 107 */ 108 109 public function __construct($directory, $namespace = null, $separator = '\\', $extension = '.php') 110 { 111 if (file_exists($directory) && is_dir($directory)) { 112 $this->directory = $directory; 113 $this->namespace = $namespace; 114 $this->separator = $separator; 115 $this->extension = $extension; 116 } 117 } 118 119 /** 120 * Loads a class. 121 * 122 * @param string $class The class 123 * @return bool 124 */ 125 126 public function load($class) 127 { 128 global $trace; 129 130 $request = $class; 131 132 if ($this->namespace !== null && strpos($class, $this->namespace.$this->separator) !== 0 || 133 !preg_match('/^[\\a-zA-Z_\x7f-\xff][a-zA-Z0-9_\\\x7f-\xff]*$/', $class) 134 ) { 135 return false; 136 } 137 138 $file = $this->directory.'/'; 139 $divide = strripos($class, $this->separator); 140 141 if ($divide !== false) { 142 $namespace = substr($class, 0, $divide); 143 $class = substr($class, $divide + 1); 144 $file .= str_replace($this->separator, '/', $namespace).'/'; 145 } 146 147 $file .= $class.$this->extension; 148 149 if (is_readable($file)) { 150 $trace->start("[Load: '".str_replace(txpath.'/', '', $file)."']"); 151 require_once $file; 152 153 if (class_exists($request, false)) { 154 $trace->log("[Class loaded: '$class']"); 155 $trace->stop(); 156 return true; 157 } 158 159 $trace->stop(); 160 } 161 162 return false; 163 } 164 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title