获取其它组件实例

@param $abstract

@return App|Router|Request|Response|HttpKernel|ConsoleKernel|Logger
184310
static public function make($abstract)
    {
        $instance = isset(static::$instances[$abstract]) ? static::$instances[$abstract] : null;
        if ($instance instanceof Closure) {
            $instance = $instance();
            self::register($abstract, $instance);
        }

        return $instance;
    }
919318
private function regular() {
        /** 获取PATHINFO */
        $pathInfo = $this->pathInfo;//$this->getPathInfo();
        foreach ($this->routingTable as $key => $route) {
            if (!preg_match($route['regx'], $pathInfo, $matches)) {
                continue;
            }
            $this->current = $key;
            /** 载入参数 */
            $params = NULL;
            if (!empty($route['params'])) {
                unset($matches[0]);
                $params = array_combine($route['params'], $matches);
                $req = Request::driver();
                $req->get = array_merge($req->get,$params);
                $req->request = array_merge($req->request,$params);
            }
            $this->pathInfo = '/'.$route['action'];
            return true;
        }
        return false;
    }
715056
protected function getClosure($abstract, $concrete)
    {
        return function ($container, array $parameters = array()) use ($abstract, $concrete) {
            $method = ($abstract == $concrete) ? 'build' : 'make';
            return $container->$method($concrete, $parameters);
        };
    }
914586
protected function resolve($abstract, array $parameters = [])
    {
        if ($this->bound($abstract)) {
            array_unshift($parameters, $this);

            return call_user_func_array($this->bindings[$abstract]['concrete'], $parameters);
        }

        $reflector = new ReflectionClass($abstract);

        if (!$constructor = $reflector->getConstructor()) {
            return new $abstract($parameters);
        }

        $constructorParams = $constructor->getParameters();

        $callParams = [];

        foreach ($constructorParams as $param) {
            if ($param->getClass()) {
                $callParams[] = $this->__invoke($param->getClass()->getName());
            }
        }

        foreach ($parameters as $key => $value) {
            $callParams[] = $value;
        }

        return $reflector->newInstanceArgs($callParams);
    }
730840
public function resolved($abstract)
    {
        if ($this->isAlias($abstract)) {
            $abstract = $this->getAlias($abstract);
        }

        return isset($this->resolved[$abstract]) ||
               isset($this->instances[$abstract]);
    }
663490
public function make($abstract, $args = [])
    {
        $abstract = isset($this->aliases[$abstract]) ? $this->aliases[$abstract] : $abstract;

        if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }

        $concrete = isset($this->bindings[$abstract]) ? $this->bindings[$abstract]['concrete'] : $abstract;

        if ($abstract == $concrete || $concrete instanceof Closure) {
            $object = $this->build($concrete, $args);
        } else {
            $object = $this->make($concrete, $args);
        }

        if ($this->isShared($abstract)) {
            $this->instances[$abstract] = $object;
        }

        return $object;
    }
1119383
protected function _routingTable() {

        $module = $this->module;

        $name = $module.'-router-'.$this->config['name'];

        $file = Config::$o->path_temp.$name;

        //如果存在路由缓存
        if( is_file($file) ) {
            return include($file);
        }

        if($this->routes) {
            /** 解析路由配置 */
            $parser = new Parser($this->routes);
            $routingTable = $parser->parse();
            /** 缓存路由 */
            efile($routingTable,$name);
            return $routingTable;
        }
    }
232998
public function resolve( $abstract, array $parameters = [] ) {

		// Get the true abstract name.
		$abstract = $this->getAbstract( $abstract );

		// If this is being managed as an instance and we already have
		// the instance, return it now.
		if ( isset( $this->instances[ $abstract ] ) ) {

			return $this->instances[ $abstract ];
		}

		// Get the concrete implementation.
		$concrete = $this->getConcrete( $abstract );

		// If we can't build an object, assume we should return the value.
		if ( ! $this->isBuildable( $concrete ) ) {

			// If we don't actually have this, return false.
			if ( ! $this->has( $abstract ) ) {
				return false;
			}

			return $concrete;
		}

		// Build the object.
		$object = $this->build( $concrete, $parameters );

		if ( ! $this->has( $abstract ) ) {
			return $object;
		}

		// If shared instance, make sure to store it in the instances
		// array so that we're not creating new objects later.
		if ( $this->bindings[ $abstract ]['shared'] && ! isset( $this->instances[ $abstract ] ) ) {

			$this->instances[ $abstract ] = $object;
		}

		// Run through each of the extensions for the object.
		foreach ( $this->extensions[ $abstract ] as $extension ) {

			$object = new $extension( $object, $this );
		}

		// Return the object.
		return $object;
	}
325883
public final function index(...$args) {
        $len = count($args);
        switch ($len) {
            case 0:
                return null;
            case 1:
                $subname = array_shift($args);
                $action  = 'index';
                break;
            default:
                $subname = array_shift($args);
                $fParam  = $action = array_shift($args);
        }
        if (empty($subname) || empty($action)) {
            return null;
        }
        $module = $this->module->getDirname();
        $app    = DefaultDispatcher::findApp($module, $action, $args, $this->module->getNamespace(), $subname);
        if ($app) {
            list ($controllerClz, $action, $pms, , $controllerSlag, $actionSlag) = $app;
            if (in_array($action, ['beforerun', 'afterrun', 'geturlprefix'])) {
                return null;
            }
            try {
                $clz = new $controllerClz ($this->module);

                if ($clz instanceof Controller && $clz->slag == $controllerSlag) {
                    $rqMethod = strtolower($_SERVER ['REQUEST_METHOD']);
                    $rm       = ucfirst($rqMethod);
                    // 存在index_get,index_post,add_get add_post这新的方法.
                    $md          = $action . $rm;
                    $actionFound = false;
                    if (method_exists($clz, $md)) {
                        $action      = $md;
                        $actionSlag  = $actionSlag . '-' . $rqMethod;
                        $actionFound = true;
                    } else if (!method_exists($clz, $action)) {
                        array_unshift($pms, $actionSlag);
                        $action     = 'index';
                        $actionSlag = 'index';
                    }
                    if (!$actionFound) {
                        $md = $action . $rm;
                        if (method_exists($clz, $md)) {
                            $action      = $md;
                            $actionSlag  = $actionSlag . '-' . $rqMethod;
                            $actionFound = true;
                        } else if (method_exists($clz, $action)) {
                            $actionFound = true;
                        }
                    }
                    if ($actionFound) {
                        $ref        = $clz->reflectionObj;
                        $method     = $ref->getMethod($action);
                        $methodSlag = Router::addSlash($method->getName());
                        if (!$method->isPublic() || $methodSlag != $actionSlag) {
                            return null;
                        }
                        /* @var \ReflectionParameter[] $params */
                        $params      = $method->getParameters();
                        $paramsCount = count($params);
                        if ($paramsCount < count($pms)) {
                            return null;
                        }
                        $rtn = $clz->beforeRun($action, $method);

                        $module .= '/' . $subname;
                        //beforeRun可以返回view了
                        if ($rtn instanceof View) {
                            DefaultDispatcher::prepareView($rtn, $module, $clz, $action);

                            return $rtn;
                        }
                        $args = [];

                        if ($paramsCount) {
                            $idx = 0;
                            foreach ($params as $p) {
                                $name    = $p->getName();
                                $def     = isset ($pms [ $idx ]) ? $pms [ $idx ] : ($p->isDefaultValueAvailable() ? $p->getDefaultValue() : null);
                                $value   = rqst($name, $def, true);
                                $args [] = is_array($value) ? array_map(function ($v) {
                                    return is_array($v) ? $v : urldecode($v);
                                }, $value) : urldecode($value);
                                $idx++;
                            }
                        }
                        $view = $clz->{$action}(...$args);
                        if ($view !== null) {
                            if (is_array($view)) {
                                $view = new JsonView($view);
                            } else if (!$view instanceof View) {
                                $view = new SimpleView($view);
                            } else {
                                DefaultDispatcher::prepareView($view, $module, $clz, $action);
                            }
                        }

                        return $clz->afterRun($action, $view, $method);
                    }
                }
            } catch (\Exception $e) {
                if (DEBUG == DEBUG_DEBUG) {
                    throw $e;
                }
            }
        }

        //通过插件扩展路由
        $ns = 'router:' . $this->module->getNamespace() . '/' . $subname;
        if (isset($fParam)) {
            array_unshift($args, $fParam);
        }

        return apply_filter($ns, null, $args);
    }
605581
protected function getHandler(): AbstractHandler
    {
        if ($this->handler === null
            && (
                is_callable($this->handlerGetter) === false
                || !(($this->handler = ($this->handlerGetter)()) instanceof \SlaxWeb\Output\AbstractHandler)
            )
        ) {
            throw new Exception\MissingOutputHandlerException(
                "Output Manager does not have a valid Output Handler nor an "
                . "Output Handler getter set, or it provided an invalid Handler."
            );
        }
        return $this->handler;
    }
1037804
public function make(string $abstract, array $parameters = [])
    {
        try {
            if (! isset($this->resolving[$abstract])) {
                $this->resolving[$abstract] = $this->construct($abstract);
            }

            return $this->resolving[$abstract]($abstract, $parameters);
        } catch (ReflectionException $e) {
            throw new ContainerException("Fail while attempt to make '$abstract'", 0, $e);
        }
    }
641930
public function singleton(string $abstract, $concrete = null, array $arguments = []): self
    {
        $this->data->singleton($abstract, $concrete, $arguments);
        return $this;
    }
543154
private function routeApi()
    {
        // get protocol class
        if ($this->apiProtocolClass != "framework\\protocol\\Api") {
            try {
                $api_protocol_class = sprintf("main\\%s\\protocol\\%s", $this->currentApp, $this->apiProtocolClass);
                if (!class_exists($api_protocol_class)) {
                    throw new HornetCoreException($api_protocol_class . ' no found', 500);
                }
            } catch (HornetCoreException $exception) {
                $apiProtocol = new Api($this->enableTrace);
                $this->handleApiException($apiProtocol, $exception);
                return;
            }
            $apiProtocol = new $api_protocol_class($this->enableTrace);
        } else {
            $apiProtocol = new Api($this->enableTrace);
        }

        // url route to controller
        try {
            // sql注入检查
            if ($this->enableFilterSqlInject) {
                $filterSqlInject = new FilterSqlInject($this->storagePath . '/log/sql_inject/');
                $filterSqlInject->filterInput();
            }
            if (!strpos($this->cmd, '.')) {
                throw new \Exception('Api invoker error: cmd param error!', 500);
            }
            list ($service, $method) = explode('.', $this->cmd);
            $service = ucfirst($service);
            $service = $this->underlineToUppercase($service);
            $serviceClass = sprintf("main\\%s\\api\\%s", $this->currentApp, $service);
            if (!empty($this->mod) && $this->mod != 'api') {
                $serviceClass = sprintf("main\\%s\\api\\%s\\%s", $this->currentApp, $this->mod, $service);
            }

            $service_obj = new $serviceClass();

            if (!method_exists($service_obj, $method)) {
                $method = $this->underlineToUppercase($method);
                if (!method_exists($service_obj, $method)) {
                    throw new \Exception($this->ctrl . '->' . $method . ' no found;', 404);
                }
            }

            // 安全映射机制
            $this->securityMapCheck($this->modApiName, $service, $method);

            if (($this->appStatus == 'development' || $this->appStatus == 'test') && $this->enableWriteReqLog) {
                $reqLogPath = $this->storagePath . 'tmp/' . date('Y-m-d') . '_request.log';
                $dateTime = date('H:i:s');
                $getStr = var_export($_GET, true);
                $postStr = var_export($_POST, true);
                $cookieStr = var_export($_COOKIE, true);
                $logContent = $dateTime . ': ' . $getStr . $postStr . $cookieStr . "\n\n";
                f($reqLogPath, $logContent, FILE_APPEND);
                unset($dateTime, $getStr, $postStr, $cookieStr, $logContent);
            }
            $reflectMethod = null;
            // 通过反射获取调用方法的参数列表
            if ($this->enableReflectMethod) {
                $reflectMethod = new \ReflectionMethod($service_obj, $method);
                $args = [];
                $defaults = [];
                foreach ($reflectMethod->getParameters() as $param) {
                    $args[] = $param->getName();
                    $defaults[] = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null;
                }
                if (empty($args)) {
                    // 开始执行业务逻辑流程
                    $result = call_user_func_array([$service_obj, $method], []);
                } else {
                    $params = array();
                    foreach ($args as $ak => $arg) {
                        $params[$ak] = isset($_REQUEST[$arg]) ? $_REQUEST[$arg] : $defaults[$ak];
                    }
                    // 开始执行业务逻辑流程
                    $result = call_user_func_array([$service_obj, $method], $params);
                }
            } else {
                $result = call_user_func_array([$service_obj, $method], []);
            }

            // 执行结束检验结果类型
            $apiProtocol->builder('200', $result, '', $this->format);
            $jsonStr = $apiProtocol->getResponse();
            if ($this->format == 'json' && $this->enableReflectMethod) {
                $return_obj = json_decode(json_encode($result));
                $this->validReturnJson($reflectMethod, $apiProtocol, $return_obj, $jsonStr);
            }
            echo $jsonStr;
            if (($this->appStatus == 'development' || $this->appStatus == 'test') && $this->enableWriteReqLog) {
                $reqLogPath = $this->storagePath . 'tmp/' . date('Y-m-d') . '_request.log';
                $datetime = date('H:i:s');
                $getStr = var_export($_GET, true);
                $postStr = var_export($_POST, true);
                $cookieStr = var_export($_COOKIE, true);
                $logContent = $datetime . ': ' . $getStr . $postStr . $cookieStr . "\n\n";
                unset($datetime, $getStr, $postStr, $cookieStr);
                f($reqLogPath, $logContent, FILE_APPEND);
            }
            closeResources();
        } catch (HornetCoreException  $e) {
            $this->handleApiException($apiProtocol, $e);
        } catch (HornetLogicException  $e) {
            $this->handleApiException($apiProtocol, $e);
        } catch (\PDOException $e) {
            $this->handleApiException($apiProtocol, $e);
        } catch (\Exception $e) {
            $this->handleApiException($apiProtocol, $e);
        }
    }
527564
protected function getClosure($abstract, $concrete)
    {
        return function (Container $container, $parameters = []) use ($abstract, $concrete) {
            if ($abstract == $concrete) {
                return $container->build($concrete);
            }

            return $container->make($concrete, $parameters);
        };
    }
448594
public static function route(): AbstractRoute
    {
        if (static::$routing === null) {
            $config = static::getConfig('application');
            $routingClassName = $config->get('routing_class');
            $routeOptions = $config->get('routing_options') ?? [];

            if ($routingClassName == null) {
                static::terminateWithError('Can not init routing class when routing_class is not set in application config');
            }

            static::$routing = new $routingClassName($routeOptions);
        }

        return static::$routing;
    }
1273332
public function serialize($abstraction)
    {
        if ($abstraction instanceof File) {
            $baseDir = $this->getRootDirectory($abstraction->getFileSystemId());

            return 'file://'.urlencode($abstraction->getFileSystemId()).'/'.urlencode(
                    $baseDir->getRelPath($abstraction)
                );
        } elseif ($abstraction instanceof Directory) {
            $baseDir = $this->getRootDirectory($abstraction->getFileSystemId());

            return 'dir://'.urlencode($abstraction->getFileSystemId()).'/'.urlencode(
                    $baseDir->getRelPath($abstraction)
                );
        } else {
            throw new FileSerializerException(
                __CLASS__.'::'.__FUNCTION__.' expects parameter to be an instance of Directory or File'
            );
        }
    }
214370
public function instance($abstract, $instance)
    {
        $this->removeAbstractAlias($abstract);

        $isBound = $this->bound($abstract);

        unset($this->aliases[$abstract]);

        // We'll check to determine if this type has been bound before, and if it has
        // we will fire the rebound callbacks registered with the container and it
        // can be updated with consuming classes that have gotten resolved here.
        $this->instances[$abstract] = $instance;

        if ($isBound) {
            $this->rebound($abstract);
        }

        return $instance;
    }