Gear::moduleHandler PHP Method

moduleHandler() private method

Hendle the module calling process.
private moduleHandler ( )
    private function moduleHandler()
    {
        if (!is_dir($moduleFolder = $this->config['main']['module']['path'] . 'Modules/' . $this->firstUriPath . '/')) {
            if (isset($this->config['main']['alias']['controller']['class'])) {
                $controllerClass = $this->config['main']['alias']['controller']['class'];
                if (!file_exists(APP . 'Controllers/' . $controllerClass . '.php')) {
                    throw new Resources\HttpException('Controller, sub-controller or module ' . $this->firstUriPath . ' does not exists');
                }
                $controllerNamespace = 'Controllers\\' . $controllerClass;
                $method = $this->config['main']['alias']['controller']['method'];
                $instance = new $controllerNamespace();
                $request = $this->uriObj->path();
                $this->run($instance, $method, $request);
                return;
            }
        }
        if (!($controllerClass = $this->uriObj->path(1))) {
            $controllerClass = $this->config['main']['defaultController'];
        }
        $controllerClass = ucwords($controllerClass);
        // Does this class's file exists?
        if (!file_exists($classFile = $moduleFolder . 'Controllers/' . $controllerClass . '.php')) {
            if (!isset($this->config['main']['alias']['controller']['class'])) {
                throw new Resources\HttpException('Controller ' . $controllerClass . ' does not exists in module ' . $this->firstUriPath);
            }
            $controllerClass = $this->config['main']['alias']['controller']['class'];
            $method = $this->config['main']['alias']['controller']['method'];
            $request = array_slice($this->uriObj->path(), 1);
            // Does class for alias file exists?
            if (!file_exists($classFile = $moduleFolder . 'Controllers/' . $controllerClass . '.php')) {
                throw new Resources\HttpException('Controller ' . $controllerClass . ' does not exists in module ' . $this->firstUriPath);
            }
            goto createNamespace;
        }
        $request = array_slice($this->uriObj->path(), 3);
        if (!($method = $this->uriObj->path(2))) {
            $method = 'index';
        }
        createNamespace:
        $controllerNamespace = 'Modules\\' . $this->firstUriPath . '\\Controllers\\' . $controllerClass;
        if (!class_exists($controllerNamespace)) {
            throw new Resources\RunException('Class ' . $controllerNamespace . '  not found in ' . $classFile);
        }
        $instance = new $controllerNamespace();
        if (!method_exists($instance, $method)) {
            $request = array_slice($this->uriObj->path(), 2);
            $method = $this->config['main']['alias']['method'];
            if (!method_exists($instance, $method)) {
                throw new Resources\HttpException('Method ' . $method . ' does not exists in controller ' . $moduleFolder . $controllerClass);
            }
        }
        $this->run($instance, $method, $request);
    }