# cookiebot/3.11.2/addons/inc/Dependencies/DI/Definition/Source/DefinitionFile.php

Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA &amp; Google Consent Mode, version 3.11.2. 75 lines.

- Page: https://pluginprobe.com/plugins/cookiebot/3.11.2/code/addons/inc/Dependencies/DI/Definition/Source/DefinitionFile.php
- Raw: https://pluginprobe.com/plugins/cookiebot/3.11.2/raw/addons/inc/Dependencies/DI/Definition/Source/DefinitionFile.php
- Modified: 2021-03-23T11:49:48+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/cookiebot/3.11.2/code/addons/inc/Dependencies/DI/Definition/Source/DefinitionFile.php#L10-L20`.

```php
<?php
/**
 * PHP-DI
 *
 * @link      http://php-di.org/
 * @copyright Matthieu Napoli (http://mnapoli.fr/)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
 */

namespace Cybot\Dependencies\DI\Definition\Source;

use Cybot\Dependencies\DI\Definition\Definition;
use Cybot\Dependencies\DI\Definition\Exception\DefinitionException;

/**
 * Reads DI definitions from a file returning a PHP array.
 *
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
 */
class DefinitionFile extends DefinitionArray
{
    /**
     * @var bool
     */
    private $initialized = false;

    /**
     * File containing definitions, or null if the definitions are given as a PHP array.
     * @var string|null
     */
    private $file;

    /**
     * @param string $file File in which the definitions are returned as an array.
     */
    public function __construct($file)
    {
        // Lazy-loading to improve performances
        $this->file = $file;

        parent::__construct([]);
    }

    /**
     * {@inheritdoc}
     */
    public function getDefinition($name)
    {
        $this->initialize();

        return parent::getDefinition($name);
    }

    /**
     * Lazy-loading of the definitions.
     * @throws DefinitionException
     */
    private function initialize()
    {
        if ($this->initialized === true) {
            return;
        }

        $definitions = require $this->file;

        if (! is_array($definitions)) {
            throw new DefinitionException("File {$this->file} should return an array of definitions");
        }

        $this->addDefinitions($definitions);

        $this->initialized = true;
    }
}

```
