Contao\CoreBundle\Config\Loader\PhpFileLoader::load PHP Method

load() public method

Reads the contents of a PHP file stripping the opening and closing PHP tags.
public load ( string $file, string | null $type = null ) : string
$file string
$type string | null
return string
    public function load($file, $type = null)
    {
        $code = rtrim(file_get_contents($file));
        // Opening tag
        if (0 === strncmp($code, '<?php', 5)) {
            $code = substr($code, 5);
        }
        // Access check
        $code = str_replace([" if (!defined('TL_ROOT')) die('You cannot access this file directly!');", " if (!defined('TL_ROOT')) die('You can not access this file directly!');"], '', $code);
        // Closing tag
        if (substr($code, -2) == '?>') {
            $code = substr($code, 0, -2);
        }
        return rtrim($code) . "\n";
    }

Usage Example

Example #1
0
    /**
     * Tests the load() method.
     */
    public function testLoad()
    {
        $this->assertEquals("\n\n\$GLOBALS['TL_TEST'] = true;\n", $this->loader->load($this->getRootDir() . '/vendor/contao/test-bundle/Resources/contao/config/config.php'));
        $content = <<<'EOF'


$GLOBALS['TL_DCA']['tl_test'] = [
    'config' => [
        'dataContainer' => 'DC_Table',
        'sql' => [
            'keys' => [
                'id' => 'primary',
            ],
        ],
    ],
    'fields' => [
        'id' => [
            'sql' => "int(10) unsigned NOT NULL auto_increment"
        ],
    ],
];

EOF;
        $this->assertEquals($content, $this->loader->load($this->getRootDir() . '/vendor/contao/test-bundle/Resources/contao/dca/tl_test.php'));
        $this->assertEquals("\n\n\$GLOBALS['TL_TEST'] = true;\n", $this->loader->load($this->getRootDir() . '/vendor/contao/test-bundle/Resources/contao/languages/en/tl_test.php'));
    }
All Usage Examples Of Contao\CoreBundle\Config\Loader\PhpFileLoader::load
PhpFileLoader