Stream Wrappers¶
Puli supports a stream wrapper that lets you access the resources in the
repository transparently through PHP’s file functions. To register the wrapper,
call the register()
method and pass the name of a URI schema and a
ResourceRepository
instance:
use Puli\Repository\StreamWrapper\ResourceStreamWrapper;
ResourceStreamWrapper::register('puli', $repo);
After registering the stream wrapper, you can pass Puli paths to regular PHP functions, prefixed by the registered URI scheme:
$contents = file_get_contents('puli:///acme/blog/css/style.css');
foreach (scandir('puli:///acme/blog') as $entry) {
// ...
}
Performance Tweaks¶
The method
register()
needs
to be called in every request even if the stream wrapper is not used. This means
that also the repository needs to be loaded in every request, which can have
a negative performance impact on your application. To mitigate this performance
impact, you can pass a callable to
register()
that
loads the repository on demand:
class ServiceRegistry
{
private $repo;
public function getRepository()
{
if (!$this->repo) {
$factoryClass = PULI_FACTORY_CLASS;
$factory = new $factoryClass();
$this->repo = $factory->createRepository();
}
return $this->repo;
}
}
$registry = new ServiceRegistry();
ResourceStreamWrapper::register('puli', array($registry, 'getRepository'));