Guest User

Untitled

a guest
Jun 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. Index: model/interceptor.php
  2. ===================================================================
  3. --- model/interceptor.php (revision 0)
  4. +++ model/interceptor.php (revision 0)
  5. @@ -0,0 +1,27 @@
  6. +<?php
  7. +/**
  8. + * Interface for api_model_factory interceptors
  9. + */
  10. + interface api_model_interceptor {
  11. +
  12. + /**
  13. + * Used by the interceptor to instruct the factory
  14. + * whether it wants to intercept the instantiation
  15. + * of the object
  16. + * @param string classname the factory should create
  17. + * @param boolean true if the interceptor wants to intercept
  18. + */
  19. + public function intercepts($classname);
  20. +
  21. + /**
  22. + * Used by the interceptor to create and return an object from the factory.
  23. + * Called only if the intercepts method returns true. Has the same signature
  24. + * as api_model_factory::get()
  25. + * @param $name string: Model name.
  26. + * @param $params array: Parameters in order of their appearance in the constructor.
  27. + * @param $namespace string: Namespace, default "api"
  28. + * @return api_model_common
  29. + */
  30. + public function get($name, $params = array(), $namespace = 'api');
  31. + }
  32. +
  33. \ No newline at end of file
  34. Index: model/factory.php
  35. ===================================================================
  36. --- model/factory.php (revision 11512)
  37. +++ model/factory.php (working copy)
  38. @@ -11,6 +11,9 @@
  39. * The result will be an object of api_model with the given named params.
  40. */
  41. class api_model_factory {
  42. +
  43. + private static $interceptors = array();
  44. +
  45. /**
  46. * Model Factory
  47. *
  48. @@ -23,6 +26,13 @@
  49. if (class_exists($namespace . '_model_' . $name)) {
  50. $name = $namespace . '_model_' . $name;
  51. }
  52. +
  53. + foreach ( self::$interceptors as $interceptor ) {
  54. + if ( $interceptor->intercepts($name) ) {
  55. + return $interceptor->get($name, $params, $namespace);
  56. + }
  57. + }
  58. +
  59. if (count($params) == 0) {
  60. return new $name;
  61. } else {
  62. @@ -30,4 +40,16 @@
  63. return $class->newInstanceArgs($params);
  64. }
  65. }
  66. +
  67. + /**
  68. + * Register an object that can intercept the request made to
  69. + * api_model_factory::get() and return a different object.
  70. + * An interceptor should provide two methods;
  71. + * - an "intercepts" method takes a class name and returns boolean
  72. + * (true if the interceptor want to intercept)
  73. + * - a "get" method which returns the object, given it's class name
  74. + */
  75. + public static function registerInterceptor($interceptor) {
  76. + self::$interceptors[] = $interceptor;
  77. + }
  78. }
Add Comment
Please, Sign In to add comment