指定したミドルウェアオブジェクト(ミドルウェアクラス)のキュー内の位置を返す

複数存在する場合は、最も先頭(インデックスが小さい)もののみが返される。

@param   MiddlewareInterface|string  $target
検索対象のミドルウェアインスタンスもしくはミドルウェアクラス名

@return  int|null
334941
protected function getIndex($target){
        if(is_string($target)){
            if(!class_exists($target) && !interface_exists($target)){
                throw new \InvalidArgumentException(
                    "Searche target must be specified by class name or"
                    . " interface name."
                );
            }

            foreach($this->queue as $index => $middleware){
                if(
                    get_class($middleware) === $target
                    || is_subclass_of($middleware, $target)
                ){
                    return $index;
                }
            }
        }elseif(is_object($target)){
            if(!$target instanceof MiddlewareInterface){
                $class  = MiddlewareInterface::class;
                throw new \InvalidArgumentException(
                    "Searche target must be an instance of a class that"
                    . " implements {$class}."
                );
            }

            foreach($this->queue as $index => $middleware){
                if($target === $middleware){
                    return $index;
                }
            }
        }else{
            throw new \InvalidArgumentException(
                "The searche target must be specified by object or string."
            );
        }

        return null;
    }
843830
public function middleware(...$middlewares): void
    {
        foreach ($middlewares as $middleware) {
            if (!$middleware instanceof MiddlewareInterface && !\is_string($middleware)) {
                $type = \is_object($middleware) ? \get_class($middleware) : \gettype($middleware);
                
                throw new \InvalidArgumentException(\sprintf('Unsupported middleware type: %s', $type));
            }
            
            $this->middlewares[] = $middleware;
        }
    }
850836
public function getTargetController()
    {
        $target = $this->getTarget();
        if ($target instanceof Closure ||
                !preg_match(Router::TARGET_REGEX, $target)) {
            return null;
        }
        $parts = explode('@', $target);
        return isset($parts[0]) ? $parts[0] : null;
    }
331744
public function target($target, $keyName = null)
    {
        $this->target = $target;
        $this->keyName = $keyName;

        return $this;
    }
592149
public function next(RequestInterface $request)
    {
        if (!count($this->processingStack)) {
            throw new \RuntimeException('No middleware response created for stack.');
        }
        if (!($request instanceof ServerRequestInterface)) {
            throw new \InvalidArgumentException();
        }
        /** @var ServerMiddlewareInterface $middleware */
        $middleware = array_shift($this->processingStack);
        return $middleware->process($request, $this);
    }
860733
protected function hasTargetedBinding(string $interface, string $target = null) : bool
    {
        return isset($this->bindings[$target][$interface]);
    }
382494
private static function validateMiddlewareType(string $type) : string
  {
    if (!in_array($type, self::MIDDLEWARE_TYPES)) {
      throw new \InvalidArgumentException(sprintf(
        'Provided middleware type does not exist. Use one of %s instead.',
        implode(', ', self::MIDDLEWARE_TYPES)
      ));
    }

    return $type;
  }
222708
private function proccessMiddlewareQueue(array $queue, \Closure $controllerClosure)
    {
        $return = null;

        if (!empty($queue)) {
            //Proccess queue
            $previousFn = $controllerClosure;

            foreach ($queue as $middleware) {
                //Middleware instance
                $obj = $this->container->get($middleware);

                if ($obj instanceof Middleware) {
                    $fn = function () use ($obj, $previousFn) {
                        return $obj->handle($previousFn);
                    };
                    $previousFn = $fn;
                } else {
                    throw new \Exception("Invalid middleware: '$middleware'");
                }
            }

            $return = $previousFn();
        } else {
            //Run controller immediately
            $return = $controllerClosure();
        }

        return $return;
    }
1159175
public function getAnyPartFlag($target = null) {
		$anyPart = false;
		if (empty($target)) {
			$anyPart = $this->_modelConfigSearchInfo->getFlagDefaultSearchAnyPart();
		} elseif (is_array($target) && in_array(CAKE_SEARCH_INFO_TARGET_FIELD_ANY_PART, $target)) {
			$anyPart = true;
		}

		return $anyPart;
	}
111553
protected function setAjaxTarget($target)
    {
        if (!empty($this->ajax_cmd)) {
            $this->ajax_cmd->setSelector($target);
        }

        return $this;
    }
647776
private function tryToEncodeTargetsAsSwitch(array $targets): ?array
    {
        $result = count($targets) > 1;

        $contextKey  = null;
        $valueRuleIdMap = [];

        foreach ($targets as $ruleId => $nullableTarget) {
            if ($result === true &&
                $nullableTarget !== null &&
                count($allOfs = $nullableTarget->getAnyOf()->getAllOfs()) === 1 &&
                count($pairs = reset($allOfs)->getPairs()) === 1
            ) {
                $value = reset($pairs);
                $key   = key($pairs);
                assert($key !== null);
                if ($contextKey === null) {
                    $contextKey = $key;
                }
                if ($key !== $contextKey) {
                    $result = false;
                    break;
                }
                $valueRuleIdMap[$value] = $ruleId;
            } else {
                $result = false;
                break;
            }
        }

        $result = $result === true && count($valueRuleIdMap) === count($targets);

        // if result === true we know the following
        // - we have more than one target
        // - every target is a really one key, value pair
        // - in every such pair key is identical and every value is unique
        //
        // if so
        // $contextKey - will have that identical key and $valueRuleIdMap will be an array of [unique_value => true]
        // so later we can check if target matched by simply taking value from context by $contextKey and
        // checking that value in $valueRuleIdMap (key existence).

        return $result === true ? [$contextKey, $valueRuleIdMap] : null;
    }
932560
public function mapTarget(PersistenceTarget $target): Target
    {
        try {
            $targetType = $this->targetTypeRegistry->getTargetType(
                $target->type
            );
        } catch (TargetTypeException $e) {
            $targetType = new NullTargetType();
        }

        $targetData = [
            'id' => $target->id,
            'status' => $target->status,
            'ruleId' => $target->ruleId,
            'targetType' => $targetType,
            'value' => $target->value,
        ];

        return Target::fromArray($targetData);
    }
1046547
protected static function makeWriter($target): WriterInterface
    {
        if (is_array($target) || $target instanceof \ArrayAccess) {
            return new ArrayWriter();
        }

        if (is_object($target)) {
            return new ObjectWriter();
        }

        throw new InvalidTargetException(
            sprintf('Cannot find suitable writer for target type "%s"', gettype($target))
        );
    }
97494
private static function collectMiddleware(string $className, string $methodName, Middleware $middlewareAnnotation)
    {
        $middlewares = [
            $middlewareAnnotation->getClass(),
        ];

        if (! empty($methodName)) {
            $scanMiddlewares = self::$middlewares[$className]['middlewares']['actions'][$methodName] ?? [];
            self::$middlewares[$className]['middlewares']['actions'][$methodName] = array_unique(array_merge($middlewares, $scanMiddlewares));
        } else {
            $scanMiddlewares = self::$middlewares[$className]['middlewares']['group'] ?? [];
            self::$middlewares[$className]['middlewares']['group'] = array_unique(array_merge($middlewares, $scanMiddlewares));
        }
    }
698359
private static function collectMiddlewares(
        string $className,
        string $methodName,
        Middlewares $middlewaresAnnotation
    ) {
        $classMiddlewares = [];
        foreach ($middlewaresAnnotation->getMiddlewares() as $middleware) {
            if ($middleware instanceof Middleware) {
                $classMiddlewares[] = $middleware->getClass();
            }
        }
        $classMiddlewares = array_unique($classMiddlewares);

        if (! empty($methodName)) {
            $scanMiddlewares = self::$middlewares[$className]['middlewares']['actions'][$methodName] ?? [];
            self::$middlewares[$className]['middlewares']['actions'][$methodName] = array_unique(array_merge($classMiddlewares,$scanMiddlewares));
        } else {
            $scanMiddlewares = self::$middlewares[$className]['middlewares']['group'] ?? [];
            self::$middlewares[$className]['middlewares']['group'] = array_unique(array_merge($classMiddlewares, $scanMiddlewares));
        }
    }
302530
public function getTargetClass($target): int
    {
        if (!in_array($target, $this->classes, true)) {
            throw new InvalidArgumentException(
                sprintf('Target with value "%s" is not part of the accepted classes', $target)
            );
        }

        return array_search($target, $this->classes, true);
    }
139139
protected function _getClassName($target)
    {
        $className = $target;
        if (is_object($target)) {
            $className = get_class($target);
        }

        return $this->_normalizeName($className);
    }