Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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;
1441 ariadna 20
use DI\Definition;
1 efrain 21
use core\attribute\label;
22
 
23
/**
24
 * Allow for init-time configuration of the Dependency Injection container.
25
 *
26
 * @package    core
27
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
#[label('A hook to allow per-component configuration of the DI container.')]
31
class di_configuration {
32
    /**
33
     * Create the Dependency Injection configuration hook instance.
34
     *
35
     * @param ContainerBuilder $builder
36
     */
37
    public function __construct(
38
        /** @var ContainerBuilder The PHP-DI Builder */
39
        protected ContainerBuilder $builder,
40
    ) {
41
    }
42
 
43
    /**
44
     * Add a definition to the Dependency Injection container.
45
     *
46
     * A definition is a callable that returns an instance of the service.
47
     *
48
     * The callable can take arguments which are resolved using the DI container, for example a definition for the
49
     * following example service requires \moodle_database, and \core\formatting which will be resolved using the DI
50
     * container.
51
     *
52
     * <code>
53
     * $hook->add_definition(
54
     *     id: \mod\example\service::class,
55
     *     definition: function (
56
     *         \moodle_database $db,
57
     *         \core\formatting $formatter,
58
     *     ): \mod\example\service {
59
     *         return new \mod\example\service(
60
     *             $database,
61
     *             $formatter,
62
     *             $some,
63
     *             $other,
64
     *             $args,
65
     *         )'
66
     *     },
67
     *  );
68
     * </code>
69
     *
70
     * @param string $id The identifier of the container entry
1441 ariadna 71
     * @param callable|Definition\Definition|Definition\SelfResolvingDefinition|Definition\Helper\DefinitionHelper $definition
72
     *     The definition of the container entry
1 efrain 73
     * @return self
74
     * @example
75
     */
76
    public function add_definition(
77
        string $id,
1441 ariadna 78
        callable|Definition\Definition|Definition\SelfResolvingDefinition|Definition\Helper\DefinitionHelper $definition,
1 efrain 79
    ): self {
80
        $this->builder->addDefinitions([
81
            $id => $definition,
82
        ]);
83
 
84
        return $this;
85
    }
86
}