Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: model/interceptor.php
- ===================================================================
- --- model/interceptor.php (revision 0)
- +++ model/interceptor.php (revision 0)
- @@ -0,0 +1,27 @@
- +<?php
- +/**
- + * Interface for api_model_factory interceptors
- + */
- + interface api_model_interceptor {
- +
- + /**
- + * Used by the interceptor to instruct the factory
- + * whether it wants to intercept the instantiation
- + * of the object
- + * @param string classname the factory should create
- + * @param boolean true if the interceptor wants to intercept
- + */
- + public function intercepts($classname);
- +
- + /**
- + * Used by the interceptor to create and return an object from the factory.
- + * Called only if the intercepts method returns true. Has the same signature
- + * as api_model_factory::get()
- + * @param $name string: Model name.
- + * @param $params array: Parameters in order of their appearance in the constructor.
- + * @param $namespace string: Namespace, default "api"
- + * @return api_model_common
- + */
- + public function get($name, $params = array(), $namespace = 'api');
- + }
- +
- \ No newline at end of file
- Index: model/factory.php
- ===================================================================
- --- model/factory.php (revision 11512)
- +++ model/factory.php (working copy)
- @@ -11,6 +11,9 @@
- * The result will be an object of api_model with the given named params.
- */
- class api_model_factory {
- +
- + private static $interceptors = array();
- +
- /**
- * Model Factory
- *
- @@ -23,6 +26,13 @@
- if (class_exists($namespace . '_model_' . $name)) {
- $name = $namespace . '_model_' . $name;
- }
- +
- + foreach ( self::$interceptors as $interceptor ) {
- + if ( $interceptor->intercepts($name) ) {
- + return $interceptor->get($name, $params, $namespace);
- + }
- + }
- +
- if (count($params) == 0) {
- return new $name;
- } else {
- @@ -30,4 +40,16 @@
- return $class->newInstanceArgs($params);
- }
- }
- +
- + /**
- + * Register an object that can intercept the request made to
- + * api_model_factory::get() and return a different object.
- + * An interceptor should provide two methods;
- + * - an "intercepts" method takes a class name and returns boolean
- + * (true if the interceptor want to intercept)
- + * - a "get" method which returns the object, given it's class name
- + */
- + public static function registerInterceptor($interceptor) {
- + self::$interceptors[] = $interceptor;
- + }
- }
Add Comment
Please, Sign In to add comment