Failed Conditions
Push — master ( fa2e68...636df0 )
by Arnold
03:02
created

src/Loader/ClassLoader.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
namespace Jasny\Container\Loader;
4
5
use Improved as i;
6
use Jasny\Container\Autowire\AutowireInterface;
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Load entries from class.
11
 */
12
class ClassLoader extends AbstractLoader
13
{
14
    /**
15
     * Logic to create entries
16
     * @var callable
17
     */
18
    protected $apply;
19
20
21
    /**
22
     * MappingGenerator constructor.
23
     *
24
     * @param \Iterator $classes
25
     * @param callable  $apply    Logic to create entries for a class
26
     */
27 8
    public function __construct(\Iterator $classes, callable $apply = null)
28
    {
29 8
        $this->apply = $apply ?? [$this, 'createEntry'];
30
31 8
        parent::__construct($classes);
32 7
    }
33
34
35
    /**
36
     * Create new entries
37
     *
38
     * @return void
39
     */
40 8
    protected function prepareNext(): void
41
    {
42 8
        if (!$this->items->valid()) {
43 2
            return;
44
        }
45
46 6
        $class = $this->items->current();
47
48 6
        $entries = i\type_check(
1 ignored issue
show
The function type_check was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $entries = /** @scrutinizer ignore-call */ i\type_check(
Loading history...
49 6
            call_user_func($this->apply, $class),
50 6
            'array',
51 6
            new \UnexpectedValueException("Failed to load container entries for '$class': "
52 6
                . "Expected array, callback returned %s")
53
        );
54
55 5
        $this->entries = new \ArrayIterator($entries);
56
57 5
        $this->items->next();
58 5
    }
59
60
    /**
61
     * Create a container entry for a class using autowiring.
62
     *
63
     * @param string $class
64
     * @return \Closure[]
65
     */
66 1
    protected function createEntry(string $class)
67
    {
68
        return [
69
            $class => function (ContainerInterface $container) use ($class) {
70 1
                return $container->get(AutowireInterface::class)->instantiate($class);
71 1
            }
72
        ];
73
    }
74
}
75