Laravel\Lumen\Application::getNamespace PHP Method

getNamespace() public method

Get the application namespace.
public getNamespace ( ) : string
return string
    public function getNamespace()
    {
        if (!is_null($this->namespace)) {
            return $this->namespace;
        }
        $composer = json_decode(file_get_contents(base_path('composer.json')), true);
        foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {
            foreach ((array) $path as $pathChoice) {
                if (realpath(app()->path()) == realpath(base_path() . '/' . $pathChoice)) {
                    return $this->namespace = $namespace;
                }
            }
        }
        throw new RuntimeException('Unable to detect application namespace.');
    }

Usage Example

 public function testNamespaceDetection()
 {
     $app = new Application();
     $this->setExpectedException('RuntimeException');
     $app->getNamespace();
 }