App_CLI::normalizeClassName PHP Метод

normalizeClassName() публичный Метод

Class name can have namespaces and they are treated prefectly. If object is passed as $name parameter, then same object is returned. Example: normalizeClassName('User','Model') == 'Model_User';
public normalizeClassName ( string | object $name, string $prefix = null ) : string | object
$name string | object Name of class or object
$prefix string Optional prefix for class name
Результат string | object Full, normalized class name or received object
    public function normalizeClassName($name, $prefix = null)
    {
        if (!is_string($name)) {
            return $name;
        }
        $name = str_replace('/', '\\', $name);
        if ($prefix !== null) {
            $class = ltrim(strrchr($name, '\\'), '\\') ?: $name;
            $prefix = ucfirst($prefix);
            if (strpos($class, $prefix) !== 0) {
                $name = preg_replace('|^(.*\\\\)?(.*)$|', '\\1' . $prefix . '_\\2', $name);
            }
        }
        return $name;
    }

Usage Example

Пример #1
0
 /**
  * Returns relevant exception class. Use this method with "throw".
  *
  * @param string $message Static text of exception.
  * @param string $type    Exception class or class postfix
  * @param string $code    Optional error code
  *
  * @return BaseException
  */
 public function exception($message = 'Undefined Exception', $type = null, $code = null)
 {
     if ($type === null) {
         $type = $this->default_exception;
     } elseif ($type[0] == '_') {
         if ($this->default_exception == 'BaseException') {
             $type = 'Exception_' . substr($type, 1);
         } else {
             $type = $this->default_exception . '_' . substr($type, 1);
         }
     } elseif ($type != 'BaseException') {
         $type = $this->app->normalizeClassName($type, 'Exception');
     }
     // Localization support
     $message = $this->app->_($message);
     if ($type == 'Exception') {
         $type = 'BaseException';
     }
     $e = new $type($message, $code);
     if (!$e instanceof BaseException) {
         throw $e;
     }
     $e->owner = $this;
     $e->app = $this->app;
     $e->api = $this->app;
     // compatibility with ATK 4.2 and lower
     $e->init();
     return $e;
 }