Provides lazy loading of data to views. Dot notation can be used.
composer require laasti/lazydata
All PHP callables are supported. To pass arguments to calls, use an array like ['my_callable', [/* args here */]].
Without League\Container:
$data = [
'title' => 'render_title',
'with_arguments' => ['=my_callable', [/* args here */]],
'with_class' => ['=my_class', 'my_function'], //or '=my_class::my_function',
'with_object' => [$object, 'my_function'],
'meta' => function() {
return [
'description' => 'My description'
]
}
];
$viewdata = new Laasti\Lazydata\Data($data);
$viewdata->set('username', function() {return 'George';});
//You can use dot notation within the lazy loaded data
$viewdata->get('meta.description'); //Returns 'My description'
Using filters, you can define your own filter with setFilter
or use native PHP functions that take one string argument.
$data = [
'native_example' => 'strtoupper:test', //I know, it's a stupid example :P
'closure_example' => 'closure:Test',
];
$resolver = new \Laasti\Lazydata\Resolvers\FilterResolver;
$resolver->setFilter('closure', function($value) {
return md5($value.'MYSALT');
});
$viewdata = new Laasti\Lazydata\Data($data, $resolver);
$viewdata->get('native_example'); //Returns 'TEST'
$viewdata->get('closure_example'); //Returns '56e29f03228697ad59822c71eb4d7750'
With league/container:
//We need to setup the ContainerResolver that comes with the package
$container = new League\Container\Container;
$container->add('Laasti\Lazydata\Resolvers\ResolverInterface', 'Laasti\Lazydata\Resolvers\ContainerResolver')->withArgument($container);
$container->add('Laasti\Lazydata\Data')->withArguments([[], 'Laasti\Lazydata\Resolvers\ResolverInterface']);
$viewdata = $container->get('Laasti\Lazydata\Data);;
$container->add('container_key', 'some value');
$viewdata->set('viewdata_key', '=container_key');
$viewdata->get('viewdata_key'); //Returns 'some value'
//Returns the value from SomeClass->myMethod();, SomeClass is resolved with the container
$viewdata->set('viewdata_callable_key', '=SomeClass::myMethod');
$viewdata->get('viewdata_callable_key');
//Returns the value from SomeClass->myMethod('George'); SomeClass is resolved with the container
$viewdata->set('viewdata_callable_args_key', ['=SomeClass::myMethod', ['George']]);
$viewdata->get('viewdata_callable_args_key');
The ContainerResolver falls back on the default resolver if it cannot resolve the call.
Note: Does not work with league/container invokables. It is a limitation due to the way registered callables are stored, there is no way to check if a callable is registered to the container in the public API.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
See CHANGELOG.md for more information.
Author: Sonia Marquette (@nebulousGirl)
Released under the MIT License. See LICENSE.txt file.