Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\hook;
18
 
19
use DI\ContainerBuilder;
20
use core\attribute\label;
21
 
22
/**
23
 * Allow for init-time configuration of the Dependency Injection container.
24
 *
25
 * @package    core
26
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
#[label('A hook to allow per-component configuration of the DI container.')]
30
class di_configuration {
31
    /**
32
     * Create the Dependency Injection configuration hook instance.
33
     *
34
     * @param ContainerBuilder $builder
35
     */
36
    public function __construct(
37
        /** @var ContainerBuilder The PHP-DI Builder */
38
        protected ContainerBuilder $builder,
39
    ) {
40
    }
41
 
42
    /**
43
     * Add a definition to the Dependency Injection container.
44
     *
45
     * A definition is a callable that returns an instance of the service.
46
     *
47
     * The callable can take arguments which are resolved using the DI container, for example a definition for the
48
     * following example service requires \moodle_database, and \core\formatting which will be resolved using the DI
49
     * container.
50
     *
51
     * <code>
52
     * $hook->add_definition(
53
     *     id: \mod\example\service::class,
54
     *     definition: function (
55
     *         \moodle_database $db,
56
     *         \core\formatting $formatter,
57
     *     ): \mod\example\service {
58
     *         return new \mod\example\service(
59
     *             $database,
60
     *             $formatter,
61
     *             $some,
62
     *             $other,
63
     *             $args,
64
     *         )'
65
     *     },
66
     *  );
67
     * </code>
68
     *
69
     * @param string $id The identifier of the container entry
70
     * @param callable $definition The definition of the container entry
71
     * @return self
72
     * @example
73
     */
74
    public function add_definition(
75
        string $id,
76
        callable $definition,
77
    ): self {
78
        $this->builder->addDefinitions([
79
            $id => $definition,
80
        ]);
81
 
82
        return $this;
83
    }
84
}