Burgomaster::createAutoloader PHP Méthode

createAutoloader() public méthode

Creates a class-map autoloader to the staging directory in a file named autoloader.php
public createAutoloader ( array $files = [], string $filename = 'autoloader.php' )
$files array Files to explicitly require in the autoloader. This is similar to Composer's "files" "autoload" section.
$filename string Name of the autoloader file.
    function createAutoloader($files = array(), $filename = 'autoloader.php')
    {
        $sourceDir = realpath($this->stageDir);
        $iter = new \RecursiveDirectoryIterator($sourceDir);
        $iter = new \RecursiveIteratorIterator($iter);
        $this->startSection('autoloader');
        $this->debug('Creating classmap autoloader');
        $this->debug("Collecting valid PHP files from {$this->stageDir}");
        $classMap = array();
        foreach ($iter as $file) {
            if ($file->getExtension() == 'php') {
                $location = str_replace($this->stageDir . '/', '', (string) $file);
                $className = str_replace('/', '\\', $location);
                $className = substr($className, 0, -4);
                // Remove "src\" or "lib\"
                if (strpos($className, 'src\\') === 0 || strpos($className, 'lib\\') === 0) {
                    $className = substr($className, 4);
                }
                $classMap[$className] = "__DIR__ . '/{$location}'";
                $this->debug("Found {$className}");
            }
        }
        $destFile = $this->stageDir . '/' . $filename;
        $this->debug("Writing autoloader to {$destFile}");
        if (!($h = fopen($destFile, 'w'))) {
            throw new \RuntimeException('Unable to open file for writing');
        }
        $this->debug('Writing classmap files');
        fwrite($h, "<?php\n\n");
        fwrite($h, "\$mapping = array(\n");
        foreach ($classMap as $c => $f) {
            fwrite($h, "    '{$c}' => {$f},\n");
        }
        fwrite($h, ");\n\n");
        fwrite($h, <<<EOT
spl_autoload_register(function (\$class) use (\$mapping) {
    if (isset(\$mapping[\$class])) {
        require \$mapping[\$class];
    }
}, true);

EOT
);
        fwrite($h, "\n");
        $this->debug('Writing automatically included files');
        foreach ($files as $file) {
            fwrite($h, "require __DIR__ . '/{$file}';\n");
        }
        fclose($h);
        $this->endSection();
    }

Usage Example

Exemple #1
0
<?php

require __DIR__ . '/../vendor/autoload.php';
$packager = new \Burgomaster(__DIR__ . '/artifacts/staging', __DIR__ . '/../');
$packager->exec('rm -rf ' . __DIR__ . '/artifacts/xsolla.*');
$packager->recursiveCopy('src', 'Xsolla/SDK');
$packager->recursiveCopy('vendor/guzzle/guzzle/src/Guzzle', 'Guzzle', array('php', 'pem'));
$packager->recursiveCopy('vendor/symfony/event-dispatcher', 'Symfony/Component/EventDispatcher');
$packager->recursiveCopy('vendor/symfony/http-foundation', 'Symfony/Component/HttpFoundation');
$packager->createAutoloader(array(), 'xsolla-autoloader.php');
$packager->createPhar(__DIR__ . '/artifacts/xsolla.phar', null, 'xsolla-autoloader.php');
$packager->createZip(__DIR__ . '/artifacts/xsolla.zip');
$packager->startSection('test-phar');
$packager->exec('php ' . __DIR__ . '/test-phar.php');
$packager->endSection();
$packager->exec('rm -rf ' . __DIR__ . '/artifacts/staging');
All Usage Examples Of Burgomaster::createAutoloader