Widget.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Widget;
  3. use Closure;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Support\Str;
  6. use Illuminate\View\Compilers\BladeCompiler;
  7. class Widget
  8. {
  9. protected $container;
  10. protected $blade;
  11. protected $groups = array();
  12. protected $widgets = array();
  13. public function __construct( Container $container, BladeCompiler $blade )
  14. {
  15. $this->container = $container;
  16. $this->blade = $blade;
  17. }
  18. public function register( $name, $callback )
  19. {
  20. $this->widgets[ $name ] = $callback;
  21. $this->registerTag( $name );
  22. }
  23. protected function registerTag( $method )
  24. {
  25. $this->blade->directive( $method, function ( $expression ) use ( $method ) {
  26. return '<?php echo \Widget::' . $method . $expression . '; ?>';
  27. } );
  28. }
  29. public function exists( $name )
  30. {
  31. return array_key_exists( $name, $this->widgets );
  32. }
  33. public function call( $name, array $parameters = array() )
  34. {
  35. if ( $this->groupExists( $name ) ) return $this->callGroup( $name, $parameters );
  36. if ( $this->exists( $name ) ) {
  37. $callback = $this->widgets[ $name ];
  38. return $this->getCallback( $callback, $parameters );
  39. }
  40. return null;
  41. }
  42. public function attribute( $name, $attr )
  43. {
  44. if ( $this->exists( $name ) ) {
  45. $name = $this->widgets[ $name ];
  46. $class = new \ReflectionClass( $name );
  47. if ( $class->hasProperty( $attr ) ) {
  48. $properties = $class->getDefaultProperties();
  49. return $properties[ $attr ];
  50. }
  51. }
  52. return null;
  53. }
  54. public function method( $name, $dataSource )
  55. {
  56. if ( $this->exists( $name ) ) {
  57. $name = $this->widgets[ $name ];
  58. $class = new \ReflectionClass( $name );
  59. $instance = $class->newInstanceArgs();
  60. return $instance->$dataSource();
  61. }
  62. return null;
  63. }
  64. public function view( $name, $dataSourceView )
  65. {
  66. $name = ucfirst( $name );
  67. if ( $this->exists( $name ) ) {
  68. $dataSourceView = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . $dataSourceView;
  69. view()->addNamespace( $name, dirname( $dataSourceView ) );
  70. return View( $name . '::' . basename( $dataSourceView ) );
  71. }
  72. }
  73. protected function getCallback( $callback, array $parameters )
  74. {
  75. if ( $callback instanceof Closure ) {
  76. return $this->createCallableCallback( $callback, $parameters );
  77. } elseif ( is_string( $callback ) ) {
  78. return $this->createStringCallback( $callback, $parameters );
  79. } else {
  80. return null;
  81. }
  82. }
  83. protected function createStringCallback( $callback, array $parameters )
  84. {
  85. if ( function_exists( $callback ) ) {
  86. return $this->createCallableCallback( $callback, $parameters );
  87. } else {
  88. return $this->createClassCallback( $callback, $parameters );
  89. }
  90. }
  91. protected function createCallableCallback( $callback, array $parameters )
  92. {
  93. return call_user_func_array( $callback, $parameters );
  94. }
  95. protected function createClassCallback( $callback, array $parameters )
  96. {
  97. list( $className, $method ) = Str::parseCallback( $callback, 'run' );
  98. $instance = $this->container->make( $className );
  99. $callable = array( $instance, $method );
  100. return $this->createCallableCallback( $callable, $parameters );
  101. }
  102. public function group( $name, array $widgets )
  103. {
  104. $this->groups[ $name ] = $widgets;
  105. $this->registerTag( $name );
  106. }
  107. public function groupExists( $name )
  108. {
  109. return array_key_exists( $name, $this->groups );
  110. }
  111. public function callGroup( $name, $parameters = array() )
  112. {
  113. if ( !$this->groupExists( $name ) ) return null;
  114. $result = '';
  115. foreach ( $this->groups[ $name ] as $key => $plugin ) {
  116. $result .= $this->call( $plugin, array_get( $parameters, $key, array() ) );
  117. }
  118. return $result;
  119. }
  120. public function __call( $method, $parameters = array() )
  121. {
  122. return $this->call( $method, $parameters );
  123. }
  124. }