Exemple #1
0
 /**
  * A very basic yet useful autoloader, not compatible with PSR-0.
  * It is used to autoload classes by namespaces with suffixes.
  *
  * Example:
  *
  * ``` php
  * <?php
  * // loads UserHelper in 'helpers/UserHelper.php'
  * Autoload::register('app\Codeception\Helper','Helper', __DIR__.'/helpers/');
  * // loads UserHelper in 'helpers/UserHelper.php'
  * Autoload::register('app\tests','Page', __DIR__.'/pageobjects/');
  * Autoload::register('app\tests','Controller', __DIR__.'/controllers/');
  * ?>
  * ```
  *
  * @param $namespace
  * @param $suffix
  * @param $path
  */
 public static function register($namespace, $suffix, $path)
 {
     self::$map[self::regex($namespace, $suffix)] = $path;
     if (!self::$registered) {
         spl_autoload_register(array(__CLASS__, 'autoload'));
         self::$registered = true;
     }
 }
Exemple #2
0
 /**
  * Adds a base directory for a namespace prefix.
  *
  * Example:
  *
  * ```php
  * <?php
  * // app\Codeception\UserHelper will be loaded from '/path/to/helpers/UserHelper.php'
  * Autoload::addNamespace('app\Codeception', '/path/to/helpers');
  *
  * // LoginPage will be loaded from '/path/to/pageobjects/LoginPage.php'
  * Autoload::addNamespace('', '/path/to/pageobjects');
  *
  * Autoload::addNamespace('app\Codeception', '/path/to/controllers');
  * ?>
  * ```
  *
  * @param string $prefix The namespace prefix.
  * @param string $base_dir A base directory for class files in the namespace.
  * @param bool $prepend If true, prepend the base directory to the stack instead of appending it; this causes it to be searched first rather than last.
  * @return void
  */
 public static function addNamespace($prefix, $base_dir, $prepend = false)
 {
     if (!self::$registered) {
         spl_autoload_register([__CLASS__, 'load']);
         self::$registered = true;
     }
     // normalize namespace prefix
     $prefix = trim($prefix, '\\') . '\\';
     // normalize the base directory with a trailing separator
     $base_dir = rtrim($base_dir, '/') . DIRECTORY_SEPARATOR;
     $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/';
     // initialize the namespace prefix array
     if (isset(self::$map[$prefix]) === false) {
         self::$map[$prefix] = [];
     }
     // retain the base directory for the namespace prefix
     if ($prepend) {
         array_unshift(self::$map[$prefix], $base_dir);
     } else {
         array_push(self::$map[$prefix], $base_dir);
     }
 }