Get a class's namespace
@param  string $classPath Path to the class php file
@return string            Namespace or null
546765
public static function getNamespace($classPath)
    {
        $src = file_get_contents($classPath);

        if (preg_match('/namespace\s+(.+?);/', $src, $m)) {
            return $m[1];
        }

        return null;
    }
917747
private static function classBelongsToNamespace($className, $namespaces)
    {
        $belongsToNamespace = false;
        $check = array_values($namespaces);
        $lcClassName = strtolower(ltrim($className, '\\'));

        foreach ($check as $namespace) {
            if (false !== strpos($lcClassName, strtolower($namespace))) {
                $belongsToNamespace = true;
                continue;
            }
        }
        return $belongsToNamespace;
    }
949632
public static function getNamespace($namespaceClass)
    {
        if (is_object($namespaceClass) === true) {
            $namespaceClass = get_class($namespaceClass);
        }
    
        $lastSlashPosition = mb_strrpos($namespaceClass, '\\');
    
        if ($lastSlashPosition !== false) {
            return mb_substr($namespaceClass, 0, $lastSlashPosition);
        }
    
        return "";
    }
1019116
public static function getNamespace($className)
        {
            $className = trim($className, '\\');

            if ($lastSeparator = strrpos($className, '\\')) {
                return substr($className, 0, $lastSeparator + 1);
            }

            return '';
        }
1113030
public function loadClass($className)
    {
        $found = false;
        $nsSep = $this->namespace.$this->namespaceSeparator;
        if (null === $this->namespace || $nsSep === substr($className, 0, strlen($nsSep))) {
            $fileName = '';
            if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
                $namespace = substr($className, 0, $lastNsPos);
                $className = substr($className, $lastNsPos + 1);
                $fileName  = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $namespace);
                $fileName .= DIRECTORY_SEPARATOR;
            }
            $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
            $filePath  = stream_resolve_include_path($this->includePath . DIRECTORY_SEPARATOR . $fileName);
            if ($filePath) {
                /** @noinspection PhpIncludeInspection */
                require $filePath;
                $found = true;
            }
        }
        return $found;
    }
1262375
private function getBasePathForClass($name, $namespace, $path)
    {
        $namespace   = str_replace('\\', '/', $namespace);
        $search      = str_replace('\\', '/', $path);
        $destination = str_replace('/' . $namespace, '', $search, $c);

        if ($c !== 1) {
            throw new RuntimeException(sprintf('Can\'t find base path for "%s" (path: "%s", destination: "%s").', $name, $path, $destination));
        }

        return $destination;
    }
1256445
function from($namespacePath)
    {
        if ($namespacePath === '')
            # from recursion calls
            return $this;

        // ..

        $cNamespace = $this->_normalizeNamespace($namespacePath);

        if (false === strstr($cNamespace, self::SEPARATOR)) {
            ## recursion fallback here !!
            if (!isset($this->__nestRight[$cNamespace]))
                return false;

            return $this->__nestRight[$cNamespace];
        }

        // ..

        $namespacePath = rtrim($namespacePath, self::SEPARATOR);
        $brkNamespace  = explode(self::SEPARATOR, $namespacePath);


        ## /root/to/nested/namespace
        $cNamespace   = array_shift($brkNamespace);
        $cContainer   = $this;
        if ($cNamespace === '') {
            ## Start with / (separator)
            #- Goto Root Container
            while ($cContainer->__nestLeft)
                $cContainer = $cContainer->__nestLeft;
        }
        else $cContainer = $this->from($cNamespace); ## fallback

        ## fallback recursion from root parent
        return ($cContainer == false) ? $cContainer : $cContainer->from(implode(self::SEPARATOR, $brkNamespace));
    }
46252
public function getClassByPath($file, $namespace)
    {
        $absPath    = realpath($file);
        $namespaces = explode('/', $absPath);
        $start      = false;
        $i          = 0;
        $chunk      = explode('\\', $namespace);
        $namespace  = array_shift($chunk);

        while ($namespaces[$i] != $namespace) {
            unset($namespaces[$i]);

            if (!array_key_exists(++$i, $namespaces)) {
                break;
            }
        }

        $className = str_replace('.php', null, array_pop($namespaces));

        return '\\'. implode('\\', $namespaces) . '\\' . $className;
    }
11261
public static function getClassName($namespaceClass)
    {
        $lastSlashPosition = mb_strrpos($namespaceClass, '\\');
    
        if ($lastSlashPosition !== false) {
            return mb_substr($namespaceClass, $lastSlashPosition + 1);
        }
    
        return $namespaceClass;
    }
1338293
protected function getNamespace(File $phpCsFile): string
    {
        $tokens = $phpCsFile->getTokens();

        $namespaceStart = null;
        foreach ($tokens as $id => $token) {
            if ($token['code'] !== T_NAMESPACE) {
                continue;
            }

            $namespaceStart = $id + 1;
            break;
        }
        if (!$namespaceStart) {
            return '';
        }

        $namespaceEnd = $phpCsFile->findNext(
            [
                T_NS_SEPARATOR,
                T_STRING,
                T_WHITESPACE,
            ],
            $namespaceStart,
            null,
            true
        );

        $namespace = trim($phpCsFile->getTokensAsString(($namespaceStart), ($namespaceEnd - $namespaceStart)));

        return $namespace;
    }
807532
public function resolveNamespace(string $path, bool $asPackage = false): string
    {
        $file       = new File($path);
        $pathInfo   = array_filter(explode(DIRECTORY_SEPARATOR, trim(str_replace([$this->configRepository->getRootPath(), 'src'], '', $file->getRealPath()), DIRECTORY_SEPARATOR)));
        $prefix     = 'antares';
        $namespaces = [];

        foreach ($pathInfo as $name) {
            if ($name === 'core') {
                $namespaces[] = $asPackage ? 'foundation' : $name;
                break;
            }
            if ($name === 'app') {
                $namespaces[] = 'foundation';
                break;
            }
            if ($name === 'src') {
                break;
            }
            if (in_array($name, ['components', 'modules'], true)) {
                continue;
            }
            $namespaces[] = $name;
        }

        return $prefix . '/' . implode('/', $namespaces);
    }
420634
public static function singleton($classPath) {
        if ( !isset(self::$INSTANES[$classPath]) ) {
            $reflect = new \ReflectionClass($classPath);
            self::$INSTANES[$classPath] = $reflect->newInstance();
        }
        return self::$INSTANES[$classPath];
    }
95220
public function applyNamespaceToPath($path)
    {
        /** @var ViewFinderInterface $finder */
        $finder = $this->getContainer()['view.finder'];
        if (!method_exists($finder, 'getHints')) {
            return $path;
        }
        $delimiter = $finder::HINT_PATH_DELIMITER;
        $hints = $finder->getHints();
        $view = array_reduce(array_keys($hints), function ($view, $namespace) use ($delimiter, $hints) {
            return str_replace($hints[$namespace], $namespace.$delimiter, $view);
        }, $path);
        return preg_replace("%{$delimiter}[\\/]*%", $delimiter, $view);
    }
1305002
public function load($className)
    {
        // get namespace of current instance
        $currentNamespace = $this->namespace.$this->namespaceSeparator;

        // get namespace from passed className
        $namespace = substr($className, 0, strlen($currentNamespace));

        // check if requested class must be loaded by this instance of loader
        // this is a good example use case for namespacing which makes sense
        if (
            $this->namespace === null ||
            $currentNamespace === $namespace
        ) {
            $filename = '';
            $lastNamespaceSeparatorPosition = strripos($className, $this->namespaceSeparator);

            if ($lastNamespaceSeparatorPosition !== false) {
                $namespace = substr($className, 0, $lastNamespaceSeparatorPosition);
                $className = substr($className, $lastNamespaceSeparatorPosition + 1);
                $filename  = str_replace(
                    $this->namespaceSeparator,
                    $this->separator,
                    $namespace
                ).$this->separator;
            }

            $filename .= str_replace('_', $this->separator, $className).'.php';
            $path      = $this->getPath();

            if ($path !== null) {
                foreach ($path as $singlePath) {
                    $filename = $singlePath.$this->separator.$filename;
                    if (file_exists($filename)) {
                        include_once $filename;
                        return true;
                    }
                }
            }
        }

        return false;
    }
82869
public function locate($classname, $basepath = null)
    {
        if (substr($classname, 0, 3) === 'Plg')
        {
            /*
             * Exception rule for Exception classes
             *
             * Transform class to lower case to always load the exception class from the /exception/ folder.
             */
            if ($pos = strpos($classname, 'Exception'))
            {
                $filename  = substr($classname, $pos + strlen('Exception'));
                $classname = str_replace($filename, ucfirst(strtolower($filename)), $classname);
            }

            $word  = strtolower(preg_replace('/(?<=\\w)([A-Z])/', ' \\1', $classname));
            $parts = explode(' ', $word);

            array_shift($parts);
            $package   = array_shift($parts);
            $namespace = ucfirst($package);

            if(count($parts)) {
                $file = array_pop($parts);
            } else {
                $file = $package;
            }

            //Switch basepath
            if(!$this->getNamespace($namespace)) {
                $basepath = $this->getNamespace('\\');
            } else {
                $basepath = $this->getNamespace($namespace);
            }

            $path = '';

            if (!empty($parts)) {
                $path = implode('/', $parts) . '/';
            }

            $result = $basepath.'/'.$package.'/'.$path . $file.'.php';

            if(!is_file($result)) {
                $result = $basepath.'/'.$package.'/'.$path . $file.'/'.$file.'.php';
            }

            return $result;
        }

        return false;

    }
1326943
static public function findClass($name, $type=null, $namespaces=null)
	{
	    $name = StringHelper::toClassName($name);
	    if (!$namespaces && strpos($name, '\\') !== false && class_exists($name)) {
	        return $name;
	    }
	    
	    $class_names = [];
	    if (!empty($type)) {
	        $type = StringHelper::toCamelCase($type);
	        if ($pos = strpos($name, '\\')) {
	            $class_names[] = $type . '\\' . $name . $type;
	            $class_names[] = $type . '\\' . $name;
	            
	            $name = substr($name, 0, $pos) . '\\' . $type . '\\' . substr($name, $pos+1);
	        } else {
	            $name = $type . '\\' . $name;
	        }
	        $class_names[] = $name;
	        $class_names[] = $name . $type;
		} else {
		    $class_names[] = $name;
		}
		
		if ($namespaces) {
            $namespaces = (array)static::formatNamespace($namespaces);
		} else {
            //$namespaces = array_unique(array_merge((array)$namespaces, static::getNamespaces()));
		    $namespaces = static::getNamespaces();
		}
		
		foreach ($namespaces as $nss) {
		    foreach ($class_names as $class) { 
		        if (class_exists($nss . $class)) {
		            return $nss . $class;
		        }
		    }
		}
		
		return null;
	}
805702
public static function startClass (string $classpath, string $namespace = "") {
		$class = substr( strrchr( $classpath , '/' ), 1 );
		$class = str_replace(".class", "", $class);
		$classpath = strtolower($classpath);

		if (!file_exists("app/".$classpath.".php"))
			return false;

		require_once "app/".$classpath.".php";

		if ($namespace != "")
			$class = $namespace."\\".$class;
			
		return new $class();
	}