Via Composer
$ composer require softius/resources-resolver
The following resolvers are made available by this library.
CallableResolver
FilenameResolver
It is possible to resolve the following inputs to the associated callable as it is demonstrated below. This works for static and non-static methods as well and it relies heavily for a Container to be provided.
App\GreetingController::helloAction
to[instance of App\GreetingController, 'helloAction']
::helloAction
to[instance of calling class, 'helloAction']
parent::helloAction
to[parent instance of calling class, 'helloAction']
App\SomeClass::someStaticMethod
to['App\SomeClass', 'someStaticMethod']
use League\Container\Container;
use Softius\ResourcesResolver\CallableResolver;
$container = new Container();
$container->add('App\SomeClass');
$resolver = new CallableResolver($container);
$callable = $resolver->resolve('App\SomeClass::someMethod');;
use League\Container\Container;
use Softius\ResourcesResolver\CallableResolver;
$container = new Container();
$container->add('FooClass', 'App\SomeClass');
$resolver = new CallableResolver($container);
$callable = $resolver->resolve('FooClass::someMethod');;
use Softius\ResourcesResolver\CallableResolver;
$resolver = new CallableResolver();
$callable = $resolver->resolve('App\SomeClass::someStaticMethod');
use Softius\ResourcesResolver\CallableResolver;
class A
{
public function hi()
{
echo 'A: Hi!';
}
}
class B extends A
{
public function hi()
{
echo 'B: Hi!';
}
public function test()
{
$resolver = new CallableResolver();
$callable = $resolver->resolve('::hi'); // returns [B, hi]
$callable = $resolver->resolve('self::hi'); // returns [B, hi]
$callable = $resolver->resolve('parent::hi'); // returns [A, hi]
}
}
define('TEMPLATES_DIR', '...');
use Softius\ResourcesResolver\FilenameResolver;
$resolver = new FilenameResolver(TEMPLATES_DIR);
$filename = $resolver->resolve('path/to/template.php');
It is also possible to omit the extension and specify a global extension for all files to be resolved, like below.
define('TEMPLATES_DIR', '...');
use Softius\ResourcesResolver\FilenameResolver;
$resolver = new FilenameResolver(TEMPLATES_DIR);
$resolver->setExtension('php');
$filename = $resolver->resolve('path/to/template');
Many frameworks don't use the directory separator to provide a consistent look across multiple OS. The following example uses '.' as the directory separator without file extensions
define('TEMPLATES_DIR', '...');
use Softius\ResourcesResolver\FilenameResolver;
$resolver = new FilenameResolver(TEMPLATES_DIR, '.');
$resolver->setExtension('php');
$filename = $resolver->resolve('path.to.template');
define('TEMPLATES_DIR', '...');
use Softius\ResourcesResolver\FilenameResolver;
$resolver = new FilenameResolver(TEMPLATES_DIR);
$resolver->useIncludePath(true);
$filename = $resolver->resolve('path/to/file.php');
$ composer test
The MIT License (MIT). Please see License File for more information.