public function declareClassNamespace($class, $namespace = '', $alias = false)
    {
        $namespace = trim($namespace, '\\');

        // check if the class is already declared
        if (isset($this->declaredClasses[$namespace])
            && isset($this->declaredClasses[$namespace][$class])) {
            return $this->declaredClasses[$namespace][$class];
        }

        $forcedAlias = $this->needAliasForClassName($class, $namespace);

        if (false === $alias || true === $alias || null === $alias) {
            $aliasWanted = $class;
            $alias = $alias || $forcedAlias;
        } else {
            $aliasWanted = $alias;
            $forcedAlias = false;
        }

        if (!$forcedAlias && !isset($this->declaredShortClassesOrAlias[$aliasWanted])) {
            if (!isset($this->declaredClasses[$namespace])) {
                $this->declaredClasses[$namespace] = [];
            }

            $this->declaredClasses[$namespace][$class] = $aliasWanted;
            $this->declaredShortClassesOrAlias[$aliasWanted] = $namespace . '\\' . $class;

            return $aliasWanted;
        }

        // we have a duplicate class and asked for an automatic Alias
        if (false !== $alias) {
            if ('\\Base' == substr($namespace, -5) || 'Base' == $namespace) {
                return $this->declareClassNamespace($class, $namespace, 'Base' . $class);
            }

            if ('Child' == substr($alias, 0, 5)) {
                //we already requested Child.$class and its in use too,
                //so use the fqcn
                return ($namespace ? '\\' . $namespace : '') .  '\\' . $class;
            } else {
                $autoAliasName = 'Child' . $class;
            }

            return $this->declareClassNamespace($class, $namespace, $autoAliasName);
        }

        throw new LogicException(sprintf(
            'The class %s duplicates the class %s and can\'t be used without alias',
            $namespace . '\\' . $class,
            $this->declaredShortClassesOrAlias[$aliasWanted]
        ));
    }
public function declareClassNamespace($class, $namespace = '', $alias = false)
    {
        $namespace = trim($namespace, '\\');

        // check if the class is already declared
        if (isset($this->declaredClasses[$namespace])
            && isset($this->declaredClasses[$namespace][$class])) {
            return $this->declaredClasses[$namespace][$class];
        }

        $forcedAlias = $this->needAliasForClassName($class, $namespace);

        if (false === $alias || true === $alias || null === $alias) {
            $aliasWanted = $class;
            $alias = $alias || $forcedAlias;
        } else {
            $aliasWanted = $alias;
            $forcedAlias = false;
        }

        if (!$forcedAlias && !isset($this->declaredShortClassesOrAlias[$aliasWanted])) {
            if (!isset($this->declaredClasses[$namespace])) {
                $this->declaredClasses[$namespace] = [];
            }

            $this->declaredClasses[$namespace][$class] = $aliasWanted;
            $this->declaredShortClassesOrAlias[$aliasWanted] = $namespace . '\\' . $class;

            return $aliasWanted;
        }

        // we have a duplicate class and asked for an automatic Alias
        if (false !== $alias) {
            if ('\\Base' == substr($namespace, -5) || 'Base' == $namespace) {
                return $this->declareClassNamespace($class, $namespace, 'Base' . $class);
            }

            if ('Child' == substr($alias, 0, 5)) {
                //we already requested Child.$class and its in use too,
                //so use the fqcn
                return ($namespace ? '\\' . $namespace : '') .  '\\' . $class;
            } else {
                $autoAliasName = 'Child' . $class;
            }

            return $this->declareClassNamespace($class, $namespace, $autoAliasName);
        }

        throw new LogicException(sprintf(
            'The class %s duplicates the class %s and can\'t be used without alias',
            $namespace . '\\' . $class,
            $this->declaredShortClassesOrAlias[$aliasWanted]
        ));
    }
public function declareClassNamespace ( $ class , $ namespace = '' , $ alias = false ) { $ namespace = trim ( $ namespace , '\\' ) ; // check if the class is already declared if ( isset ( $ this -> declaredClasses [ $ namespace ] ) && isset ( $ this -> declaredClasses [ $ namespace ] [ $ class ] ) ) { return $ this -> declaredClasses [ $ namespace ] [ $ class ] ; } $ forcedAlias = $ this -> needAliasForClassName ( $ class , $ namespace ) ; if ( false === $ alias || true === $ alias || null === $ alias ) { $ aliasWanted = $ class ; $ alias = $ alias || $ forcedAlias ; } else { $ aliasWanted = $ alias ; $ forcedAlias = false ; } if ( ! $ forcedAlias && ! isset ( $ this -> declaredShortClassesOrAlias [ $ aliasWanted ] ) ) { if ( ! isset ( $ this -> declaredClasses [ $ namespace ] ) ) { $ this -> declaredClasses [ $ namespace ] = [ ] ; } $ this -> declaredClasses [ $ namespace ] [ $ class ] = $ aliasWanted ; $ this -> declaredShortClassesOrAlias [ $ aliasWanted ] = $ namespace . '\\' . $ class ; return $ aliasWanted ; } // we have a duplicate class and asked for an automatic Alias if ( false !== $ alias ) { if ( '\\Base' == substr ( $ namespace , - 5 ) || 'Base' == $ namespace ) { return $ this -> declareClassNamespace ( $ class , $ namespace , 'Base' . $ class ) ; } if ( 'Child' == substr ( $ alias , 0 , 5 ) ) { //we already requested Child.$class and its in use too, //so use the fqcn return ( $ namespace ? '\\' . $ namespace : '' ) . '\\' . $ class ; } else { $ autoAliasName = 'Child' . $ class ; } return $ this -> declareClassNamespace ( $ class , $ namespace , $ autoAliasName ) ; } throw new LogicException ( sprintf ( 'The class %s duplicates the class %s and can\'t be used without alias' , $ namespace . '\\' . $ class , $ this -> declaredShortClassesOrAlias [ $ aliasWanted ] ) ) ; }
Declare a class to be use and return it's name or it's alias

@param  string         $class     the class name
@param  string         $namespace the namespace
@param  string|boolean $alias     the alias wanted, if set to True, it automatically adds an alias when needed
@return string         the class name or it's alias
Declare a class to be use and return it s name or it s alias