├── README.md ├── composer.json └── src ├── Facade.php └── Widget.php /README.md: -------------------------------------------------------------------------------- 1 | #DA EPIC WIDGET-SYSTEM 2 | laravel widget-system 3 | 4 | composer 5 | 6 | ``` 7 | "greabock/widget-system": "dev-master" 8 | ``` 9 | 10 | 11 | facade 12 | ``` 13 | 'Widget'=> 'Greabock\Widget\Facade' 14 | ``` 15 | 16 | 17 | Example Widget 18 | ```php 19 | something = $repository->getSomething(); 28 | } 29 | 30 | 31 | public function render($param) 32 | { 33 | $data = [ 34 | 'something' => $this->something, 35 | 'someElse' => $param, 36 | ]; 37 | 38 | return view('view', $data); 39 | } 40 | } 41 | 42 | ``` 43 | 44 | 45 | Registration: 46 | ```php 47 | Widget::register('App\Widgets\MyCustomWidget', 'myWidget' ); 48 | ``` 49 | 50 | Template: 51 | ```tpl 52 | {!! Widget::show('myWidget', 'param') !!} 53 | {-- or --} 54 | {!! Widget::myWidget('param') !!} 55 | ``` 56 | 57 | Positions 58 | ```php 59 | Widget::register('App\Widgets\MyCustomWidget', 'myWidget', 'menu_position', 1 ); 60 | Widget::register('App\Widgets\OtherCustomWidget', 'OtherWidget', 'menu_position', 2 ); 61 | ``` 62 | 63 | Template: 64 | 65 | ```php 66 | {!! Widget::position('menu_position') !!} 67 | // same as 68 | {!! Widget::show('myWidget') !!} 69 | {!! Widget::show('OtherWidget') !!} 70 | ``` 71 | 72 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "greabock/widget-system", 3 | "description": "Da epic widget-system for Laravel", 4 | "keywords": ["laravel", "widget"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Greabock", 9 | "email": "greabock@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Greabock\\Widget\\": "src/" 18 | } 19 | }, 20 | "extra": { 21 | "branch-alias": { 22 | "dev-master": "0.1-dev" 23 | } 24 | }, 25 | "minimum-stability": "dev" 26 | } 27 | -------------------------------------------------------------------------------- /src/Facade.php: -------------------------------------------------------------------------------- 1 | app = $app; 32 | } 33 | 34 | /** 35 | * @param string $method 36 | * @param array $arguments 37 | * @return mixed 38 | */ 39 | public function __call($method, array $arguments) 40 | { 41 | array_unshift($arguments, $method); 42 | 43 | return call_user_func_array([$this, 'show'], $arguments); 44 | } 45 | 46 | /** 47 | * @param string $class 48 | * @param string $name 49 | * @return void 50 | */ 51 | public function register($class, $name, $position = null, $order = null) 52 | { 53 | $this->classes[$name] = $class; 54 | 55 | if ($position) 56 | { 57 | $this->positions[$position][] = compact('name', 'order'); 58 | } 59 | 60 | } 61 | 62 | /** 63 | * @param string $name 64 | * @return mixed|null 65 | */ 66 | public function show($name) 67 | { 68 | 69 | if ( ! array_key_exists($name, $this->classes)) return null; 70 | 71 | if ( ! array_key_exists($name, $this->instances)) 72 | { 73 | $widget = $this->app->make($this->classes[$name]); 74 | 75 | $this->addInstance($widget, $name); 76 | } 77 | 78 | return call_user_func_array([$this->instances[$name], 'render'], array_slice(func_get_args(), 1)); 79 | } 80 | 81 | 82 | public function position($position) 83 | { 84 | if ( ! array_key_exists($position, $this->positions)) return null; 85 | 86 | usort($this->positions[$position], function ($a, $b) 87 | { 88 | return ($a['order'] == $b['order']) ? 0 : (($a['order'] > $b['order']) ? -1 : 1); 89 | 90 | }); 91 | 92 | $carpet = ''; 93 | 94 | $arguments = array_slice(func_get_args(), 1); 95 | 96 | 97 | foreach ($this->positions[$position] as $widget) 98 | { 99 | 100 | $parameters = $arguments; 101 | 102 | array_unshift($parameters, $widget['name']); 103 | 104 | $carpet .= call_user_func_array([$this, 'show'], $parameters); 105 | } 106 | 107 | return $carpet; 108 | } 109 | 110 | 111 | public function exists($name) 112 | { 113 | if (array_key_exists($name, $this->classes)) return true; 114 | 115 | return false; 116 | } 117 | 118 | 119 | public function isEmptyPosition($position) 120 | { 121 | if ( ! array_key_exists($position, $this->positions)) return true; 122 | 123 | if ( ! count($this->positions[$position])) return true; 124 | 125 | return false; 126 | } 127 | 128 | /** 129 | * @param $widget 130 | * @param string $name 131 | * @return void 132 | */ 133 | protected function addInstance($widget, $name) 134 | { 135 | $this->instances[$name] = $widget; 136 | } 137 | 138 | } 139 | 140 | --------------------------------------------------------------------------------