softius / resources-resolver
Resolve file paths and methods
Requires
- php: >=5.6.0
- container-interop/container-interop: ^1.1
Requires (Dev)
- league/container: ^2.2
- phpunit/phpunit: ^5.0
This package is auto-updated.
Last update: 2024-12-14 04:59:29 UTC
README
Install
Via Composer
$ composer require softius/resources-resolver
Usage
The following resolvers are made available by this library.
CallableResolver
FilenameResolver
Usage: CallableResolver
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']
Resolve a non - static method
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');;
Resolve a method using alias for class
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');;
Resolve a static method
use Softius\ResourcesResolver\CallableResolver; $resolver = new CallableResolver(); $callable = $resolver->resolve('App\SomeClass::someStaticMethod');
Resolve using parent or self
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] } }
Usage: FilenameResolver
Resolve a filename from templates directory
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');
Resolve a filename from include path
define('TEMPLATES_DIR', '...'); use Softius\ResourcesResolver\FilenameResolver; $resolver = new FilenameResolver(TEMPLATES_DIR); $resolver->useIncludePath(true); $filename = $resolver->resolve('path/to/file.php');
Testing
$ composer test
License
The MIT License (MIT). Please see License File for more information.