<commit_before><commit_msg>Added new dependency resolver for Phroute controllers<commit_after><?php
/**
 * Created by PhpStorm.
 * User: elham_asmar
 * Date: 01/04/2015
 * Time: 11:54
 */

namespace Wave\Framework\Common;


use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Wave\Framework\Annotation\Inject;

class DependencyResolver {

    protected $parser = null;
    protected $container = null;

    public function __construct($container = null)
    {
        if ($container === null) {
            $container = Container::getInstance();
        }

        $this->container = $container;

        AnnotationRegistry::registerAutoloadNamespace('\\Wave\\Framework\\Annotations\\');
        $this->parser = new AnnotationReader();
    }

    public function verify($class)
    {
        if (is_array($class) || is_callable($class) || is_int($class)) {
            throw new \InvalidArgumentException(
                sprintf('Invalid type provided. Expected string or object, received "%s"', gettype($class))
            );
        }

        if (is_object($class)) {
            $class = get_class($class);
        }

        if (!class_exists($class)) {
            throw new \InvalidArgumentException(
                sprintf('Class "%s" does not exist, check the name and try again', $class)
            );
        }

        return $class;
    }

    public function resolve($dependency)
    {
        try {
            $class = $this->verify($dependency);

            $reflection = new \ReflectionClass($class);
            $annotations = $this->parser->getClassAnnotations($reflection);
            $dependencies = [];

            foreach ($annotations as $annotation) {
                if ($annotation instanceof Inject) {
                    echo 'Is Inject';
                    if ($annotation->type === 'Static' && isset($this->container[$annotation->name])) {
                        array_push($dependencies, $this->container[$annotation->name]);
                        continue;
                    } elseif (!isset($this->container[$annotation->name]) && $annotation->type === 'Static') {
                        throw new \RuntimeException(
                            sprintf('Dependency "%s" was requested as static, but was not in the container', $annotation->name)
                        );
                    } else {
                        if (class_exists($annotation->name)) {
                            $class = $annotation->name;
                            array_push($dependencies, new $class);
                        }
                    }
                }
            }

            return $reflection->newInstanceArgs($dependencies);
        } catch (\InvalidArgumentException $e) {
            throw new \RuntimeException('Cannot resolve dependency.', null, $e);
        }
    }
}
{'fraction_non_alphanumeric': 0.12144344205412907, 'fraction_numerical': 0.004163775156141568, 'mean_word_length': 1.8403940886699508, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3011135', 'n_tokens_mistral': 720, 'n_tokens_neox': 675, 'n_words': 160}