facades.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php namespace Laravel\Facades;
  2. use Laravel\IoC;
  3. /**
  4. * The Laravel framework makes thorough use of dependency injection assisted by an application
  5. * inversion of control container. This allows for great flexibility, easy testing, and better
  6. * architecture. However, most PHP framework users may be used to accessing classes through
  7. * a variety of static methods. Laravel provides "facades" to simulate this behavior while
  8. * still using heavy dependency injection.
  9. *
  10. * Each class that is commonly used by the developer has a corresponding facade defined in
  11. * this file. All of the various facades inherit from the abstract Facade class, which only
  12. * has a single __callStatic magic method. The facade simply resolves the requested class
  13. * out of the IoC container and calls the appropriate method.
  14. */
  15. abstract class Facade {
  16. /**
  17. * Magic Method for passing methods to a class registered in the IoC container.
  18. * This provides a convenient method of accessing functions on classes that
  19. * could not otherwise be accessed staticly.
  20. *
  21. * Facades allow Laravel to still have a high level of dependency injection
  22. * and testability while still accomodating the common desire to conveniently
  23. * use classes via static methods.
  24. */
  25. public static function __callStatic($method, $parameters)
  26. {
  27. $class = IoC::resolve(static::$resolve);
  28. $count = count($parameters);
  29. if ($count > 5)
  30. {
  31. return call_user_func_array(array($class, $method), $parameters);
  32. }
  33. elseif ($count == 1)
  34. {
  35. return $class->$method($parameters[0]);
  36. }
  37. elseif ($count == 2)
  38. {
  39. return $class->$method($parameters[0], $parameters[1]);
  40. }
  41. elseif ($count == 3)
  42. {
  43. return $class->$method($parameters[0], $parameters[1], $parameters[2]);
  44. }
  45. elseif ($count == 4)
  46. {
  47. return $class->$method($parameters[0], $parameters[1], $parameters[2], $parameters[3]);
  48. }
  49. elseif ($count == 5)
  50. {
  51. return $class->$method($parameters[0], $parameters[1], $parameters[2], $parameters[3], $parameters[4]);
  52. }
  53. }
  54. }
  55. class Session extends Facade { public static $resolve = 'laravel.session'; }