@param mixed $definition

@throws \RuntimeException If no compiler is able to support the
resource

@return \Yosmanyga\Resource\Compiler\CompilerInterface
169901
protected function pickCompiler($definition)
    {
        foreach ($this->compilers as $i => $compiler) {
            if ($compiler->supports($definition)) {
                if (0 != $i) {
                    // Move compiler to top to improve next pick
                    unset($this->compilers[$i]);
                    array_unshift($this->compilers, $compiler);
                }

                return $this->compilers[0];
            }
        }

        throw new \RuntimeException('No compiler is able to work with the resource');
    }
579109
public function register($nodeName, ProcessorDefinitionInterface $definition)
    {
        if (array_key_exists($nodeName, $this->items)) {
            throw new \RuntimeException(sprintf('Processor definition for node name "%s" already exists in registry.', $nodeName));
        }

        $this->items[$nodeName] = $definition;
    }
249752
public function bind(Definition $definition) {
        $this->arguments = [];
        $this->options = [];
        $this->definition = $definition;

        $this->parse();
    }
1197594
public static function createFromFieldset($definition)
    {
        $definition = self::parseDefinition($definition);
        $root       = new Group();
        $group      = $root;

        /** @var bool|Dropdown $dropdown */
        $dropdown = false;
        $first    = true;

        foreach ($definition as $button) {
            // dont add empty items
            if (self::isInvalidDefinition($button)) {
                continue;
            }

            // encode value
            $button = self::encodeValue($button);

            // finish dropdown
            if (self::hasToCloseDropdown($dropdown, $button)) {
                $dropdown = false;
            }

            if (in_array($button['type'], array('group', 'vgroup'))) {
                // create new group
                $group = self::createNewGroup($root, $button, $dropdown, !$first);
            } elseif ($button['type'] == 'dropdown') {
                // create dropdown

                $dropdown      = static::createDropdown($button['label'], $button['attributes'], true);
                $dropdownGroup = static::createGroup();
                $dropdownGroup->addChild($dropdown);

                $group->addChild($dropdownGroup);
            } elseif ($button['type'] == 'child' || $button['type'] == 'header') {
                // add dropdown child

                static::parseDropdownChild($dropdown, $button);
            } elseif ($dropdown !== false) {
                $child = static::createDropdownItem($button['label'], $button['url'], $button['attributes'], true);
                $dropdown->addChild($child);
            } else {
                $child = static::createButton($button['label'], $button['url'], $button['attributes'], true);
                $group->addChild($child);
            }

            $first = false;
        }

        return $root;
    }
1051904
public function resolve(
        DefinitionInterface $definition, array $parameters = [])
    {
        /** @var CallableDefinition $definition */
        $args = (empty($parameters)) ? $definition->getArgs(): $parameters;
        return call_user_func_array($definition->getCallable(), $args);
    }
854805
public function parseRulesDefinition($definition): RulesCollection
    {
        if (is_callable($definition)) {

            $definition = [$definition];

        }

        if (is_string($definition)) {

            $definition = array_map('trim', explode('|', $definition));

        }

        if (is_array($definition)) {

            $keys = array_keys($definition);

            return array_reduce($keys, function ($rules, $key) use ($definition) {

                $definition = $definition[$key];

                $rule = $this->parseRuleDefinition($key, $definition);

                return $rules->withRule($rule);

            }, new RulesCollection);

        }

        throw new InvalidRuleFormatException($definition);
    }
1313100
public function map($definition)
    {
        $fixed = preg_replace('/\s+/', ' ', $definition);
        if (empty($fixed)) {
            return;
        }
        
        $names = explode(' ', $fixed);
        $newArgs = [];
        foreach ($this->args as $key => $value) {
            if (!is_int($key) && in_array($key, $names)) {
                throw new \Exception("Duplicate key {$key}");
            }

            if (is_int($key) && isset($names[$key])) {
                //Map command
                $newArgs[$names[$key]] = $value;
            } else {
                //Ignore options
                $newArgs[$key] = $value;
            }
        }
        $this->args = $newArgs;
    }
816549
protected function _factory($definition, $params)
    {
        if (is_string($definition)) {
            if ($params) {
                $refl = new ReflectionClass($definition);
                return $refl->newInstanceArgs($params);
            } else {
                return new $definition();
            }
        }
        return call_user_func_array($definition, $params);
    }
911187
protected function parseDefinition($definition = null)
    {
        if (is_null($definition)) {
            if (!is_null($this->definition)) {
                throw new \InvalidArgumentException(
                    'Cannot parse rule definitions for rule "'. $this->name. '", attrib "'
                    . $this->attrib->getName(). '" without definition!'
                );
            }
            $definition = $this->definition;
            $this->definition = null;
        }

        // required, simple
        if (is_string($definition) || is_callable($definition)) {
            $definition = array('constraint' => $definition);
        }

        // init empty to reduce isset checks..
        $definition = array_merge(self::$DEFAULT_ATTRIBS, $definition);

        // set attribs
        $this->sufficient = $definition['sufficient'];
        $this->skipEmpty  = $definition['skipEmpty'];
        $this->error      = $definition['error'];

        // having old style callable constraint
        if (is_callable($definition['constraint']) && is_array($definition['constraint'])) { // !($definition['constraint'] instanceof \Closure)) {
            $cb = $definition['constraint'];
            $definition['constraint'] = function () use ($cb) {
                $args = func_get_args();
                return call_user_func_array($cb, $args);
            };
        }

        // from string -> check predefined
        elseif (is_string($definition['constraint'])) {
            $args = preg_split('/:/', $definition['constraint']);
            $method = 'rule'. array_shift($args);
            $found = false;
            foreach ($this->dataFilter->getPredefinedRuleClasses() as $className) {
                if (is_callable(array($className, $method)) && method_exists($className, $method)) {
                    $definition['constraint'] = call_user_func_array(array($className, $method), $args);
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                throw new \InvalidArgumentException(
                    'Could not use constraint "'. $definition['constraint']. '" for rule "'
                    . $this->name. '", attrib "'. $this->attrib->getName(). '" because no '
                    . 'predefined rule class found implementing "'. $method. '()"'
                );
            }
        }

        // determine class
        $constraintClass = is_object($definition['constraint'])
            ? get_class($definition['constraint'])
            : '(Scalar)';

        // at this point: it has to be a closure!
        if ($constraintClass !== 'Closure') {
            throw new \InvalidArgumentException(
                'Definition for rule "'. $this->name. '", attrib "'. $this->attrib->getName(). '"'
                . ' has an invalid constraint of class '. $constraintClass
            );
        }
        $this->constraint = $definition['constraint'];
    }
1286242
public function compile($definition)
    {
        $validator = $this->createValidator($definition);
        $validator->validate($definition);

        return new ScalarValidator($definition->export());
    }
705143
public function addDefinition(Definition $definition)
    {
        if ($this->hasDefinition($definition)) {
            throw new \InvalidArgumentException(sprintf('Definition "%s" allready exists.', $definition->getName()));
        }

        $this->definitions[$definition->getName()] = $definition;

        return $this;
    }
215824
public function setDefinition($id, Definition $definition)
    {
        if ($this->isCompiled()) {
            throw new BadMethodCallException('Adding definition to a compiled container is not allowed');
        }

        $id = (string) $id;

        if ('' === $id || '\\' === $id[-1] || \strlen($id) !== strcspn($id, "\0\r\n'")) {
            throw new InvalidArgumentException(sprintf('Invalid service id: "%s"', $id));
        }

        unset($this->aliasDefinitions[$id], $this->removedIds[$id]);

        return $this->definitions[$id] = $definition;
    }
410785
public static function create($definition)
    {
        if ($definition instanceof FabricateDefinition) {
            return new FabricateDefinitionFactory($definition);
        }
        if ($definition instanceof FabricateModel) {
            return new FabricateModelFactory($definition);
        }
        throw new \InvalidArgumentException("FabricateFactory is not support instance");
    }
280141
public function create(\stdClass $definition = null): Schema
    {
        if (!$definition) {
            $definition       = (object)[];
            $definition->type = Schema::TYPE_ANY;
        }
        if (!isset($definition->type)) {
            $definition = clone $definition;
            if (isset($definition->allOf)) {
                foreach ($definition->allOf as $nested) {
                    if (isset($nested->type)) {
                        $definition->type = $nested->type;
                    }
                }
            }
            if (!isset($definition->type)) {
                $definition->type = Schema::TYPE_STRING;
            }
        }
        if (isset($definition->properties)) {
            $definition->type = 'object';
        }

        $index = array_search($definition, $this->definitions);

        if (false === $index) {
            if ($definition->type == Schema::TYPE_OBJECT) {
                $propertySchemas = (object)[];

                if (isset($definition->properties)) {
                    foreach ($definition->properties as $attributeName => $propertyDefinition) {
                        $propertySchemas->$attributeName = $this->create($propertyDefinition);
                    }
                }

                if (isset($definition->allOf)) {
                    foreach ($definition->allOf as $nested) {
                        if (isset($nested->properties)) {
                            foreach ($nested->properties as $attributeName => $propertyDefinition) {
                                $propertySchemas->$attributeName = $this->create($propertyDefinition);
                            }
                        }
                    }
                    unset($definition->type);
                }

                $schema = new ObjectSchema($definition, $propertySchemas);
            } elseif ($definition->type == Schema::TYPE_ARRAY) {
                $itemsSchema = isset($definition->items) ? $this->create($definition->items) : null;
                $schema      = new ArraySchema($definition, $itemsSchema);
            } elseif ($definition->type == Schema::TYPE_ANY) {
                $schema = new AnySchema($definition);
            } else {
                $schema = new ScalarSchema($definition);
            }

            $this->definitions[] = $definition;
            $this->schemas[]     = $schema;

            return $schema;
        }

        return $this->schemas[$index];
    }
79673
public function add(DefinitionInterface $definition)
    {
        $index = count($this->_names);
        $this->_definitionSource[$index] = $definition;
        $this->_names[$definition->getName()] = $index;
        return $this;
    }
970146
protected function getReflectionMethod(Definition $definition, $method)
    {
        if ('__construct' === $method) {
            return $this->getConstructor($definition, true);
        }

        if (!$class = $definition->getClass()) {
            throw new RuntimeException(sprintf('Invalid service "%s": the class is not set.', $this->currentId));
        }

        if (!$r = $this->container->getReflectionClass($class)) {
            throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
        }

        if (!$r->hasMethod($method)) {
            throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" does not exist.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
        }

        $r = $r->getMethod($method);
        if (!$r->isPublic()) {
            throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
        }

        return $r;
    }
1013657
protected function getConstructor(Definition $definition, $required)
    {
        if (\is_string($factory = $definition->getFactory())) {
            if (!\function_exists($factory)) {
                throw new RuntimeException(sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));
            }
            $r = new \ReflectionFunction($factory);
            if (false !== $r->getFileName() && file_exists($r->getFileName())) {
                $this->container->fileExists($r->getFileName());
            }

            return $r;
        }

        if ($factory) {
            list($class, $method) = $factory;
            if ($class instanceof Reference) {
                $class = $this->container->findDefinition((string) $class)->getClass();
            } elseif (null === $class) {
                $class = $definition->getClass();
            }
            if ('__construct' === $method) {
                throw new RuntimeException(sprintf('Invalid service "%s": "__construct()" cannot be used as a factory method.', $this->currentId));
            }

            return $this->getReflectionMethod(new Definition($class), $method);
        }

        $class = $definition->getClass();

        try {
            if (!$r = $this->container->getReflectionClass($class)) {
                throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
            }
        } catch (\ReflectionException $e) {
            throw new RuntimeException(sprintf('Invalid service "%s": %s.', $this->currentId, lcfirst(rtrim($e->getMessage(), '.'))));
        }
        if (!$r = $r->getConstructor()) {
            if ($required) {
                throw new RuntimeException(sprintf('Invalid service "%s": class%s has no constructor.', $this->currentId, sprintf($class !== $this->currentId ? ' "%s"' : '', $class)));
            }
        } elseif (!$r->isPublic()) {
            throw new RuntimeException(sprintf('Invalid service "%s": %s must be public.', $this->currentId, sprintf($class !== $this->currentId ? 'constructor of class "%s"' : 'its constructor', $class)));
        }

        return $r;
    }