Skip to content

Instantly share code, notes, and snippets.

@webmozart
Last active August 29, 2015 13:56
Show Gist options
  • Save webmozart/8832773 to your computer and use it in GitHub Desktop.
Save webmozart/8832773 to your computer and use it in GitHub Desktop.
Using Puli for PSR-4 style autoloading
<?php
// Put this file in your vendor/ directory
// The classes needed to make the autoloader work
require_once __DIR__.'/webmozart/puli/src/Locator/ResourceNotFoundException.php';
require_once __DIR__.'/webmozart/puli/src/Locator/DataStorageInterface.php';
require_once __DIR__.'/webmozart/puli/src/Locator/ResourceLocatorInterface.php';
require_once __DIR__.'/webmozart/puli/src/Locator/AbstractResourceLocator.php';
require_once __DIR__.'/webmozart/puli/src/Locator/PhpResourceLocator.php';
require_once __DIR__.'/webmozart/puli/src/LocatorDumper/ResourceLocatorDumperInterface.php';
require_once __DIR__.'/webmozart/puli/src/LocatorDumper/PhpResourceLocatorDumper.php';
require_once __DIR__.'/webmozart/puli/src/Pattern/PatternFactoryInterface.php';
require_once __DIR__.'/webmozart/puli/src/PatternLocator/PatternLocatorInterface.php';
require_once __DIR__.'/webmozart/puli/src/PatternLocator/GlobPatternLocator.php';
require_once __DIR__.'/webmozart/puli/src/Resource/ResourceInterface.php';
require_once __DIR__.'/webmozart/puli/src/Resource/LazyFileResource.php';
$locator = require_once __DIR__.'/resource-locator.php';
spl_autoload_register(function ($class) use ($locator) {
try {
require $locator->get('/'.ltrim(strtr($class, array('\\' => '/')), '/').'.php')->getPath();
} catch (\Webmozart\Puli\Locator\ResourceNotFoundException $e) {
}
});
return $locator;
{
"require": {
"webmozart/composer-puli-plugin": "~1.0@alpha"
},
"extra": {
"resources": {
"override": {
"/Webmozart/Puli": "vendor/webmozart/puli/src",
"/Webmozart/Puli/Tests": "vendor/webmozart/puli/tests"
}
}
},
"minimum-stability": "alpha"
}
<?php
use Webmozart\Puli\Puli;
use Webmozart\Puli\StreamWrapper\ResourceStreamWrapper;
$locator = require __DIR__.'/vendor/autoload-puli.php';
// This class is autoloaded!
echo Puli::VERSION."\n";
// Let's see where it was loaded from
echo $locator->get('/Webmozart/Puli/Puli.php')->getPath()."\n";
// This class is also autoloaded
ResourceStreamWrapper::register('namespace', $locator);
// We can access files relative to PHP namespaces
echo file_get_contents('namespace:///Webmozart/Puli/Tests/Fixtures/dir1/file1')."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment