lithium\util\collection\Filters::apply PHP Method

apply() public static method

This method is useful if you want to apply a filter inside a global bootstrap file to a static class which may or may not be loaded during every request, or which may be loaded lazily elsewhere in your application. If the class is already loaded, the filter will be applied immediately. However, if the class has not been loaded, the filter will be stored and applied to the class the first time the method specified in $method is called. This works for any class which extends StaticObject.
See also: lithium\core\StaticObject
public static apply ( string $class, string $method, Closure $filter ) : void
$class string The fully namespaced name of a **static** class to which the filter will be applied. The class name specified in `$class` **must** extend `StaticObject`, or else statically implement the `applyFilter()` method.
$method string The method to which the filter will be applied.
$filter Closure The filter to apply to the class method.
return void
    public static function apply($class, $method, $filter)
    {
        if (class_exists($class, false)) {
            return $class::applyFilter($method, $filter);
        }
        static::$_lazyFilters[$class][$method][] = $filter;
    }

Usage Example

Beispiel #1
0
 public function testLazyApply()
 {
     $class = 'lithium\\tests\\mocks\\util\\MockFilters';
     Filters::apply($class, 'filteredMethod', function ($self, $params, $chain) {
         return md5($chain->next($self, $params, $chain));
     });
     $expected = md5('Working?');
     $result = $class::filteredMethod();
     $this->assertEqual($expected, $result);
     Filters::apply($class, 'filteredMethod', function ($self, $params, $chain) {
         return sha1($chain->next($self, $params, $chain));
     });
     $expected = md5(sha1('Working?'));
     $result = $class::filteredMethod();
     $this->assertEqual($expected, $result);
 }
All Usage Examples Of lithium\util\collection\Filters::apply