├── .gitignore ├── LICENSE ├── README.md ├── Widget.php ├── WidgetFacade.php ├── WidgetServiceProvider.php └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | /.idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Pingpong Labs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel 5 Widget 2 | ============== 3 | 4 | Official documentation is located [here](http://sky.pingpong-labs.com/docs/2.0/widget) -------------------------------------------------------------------------------- /Widget.php: -------------------------------------------------------------------------------- 1 | blade = $blade; 41 | $this->container = $container; 42 | } 43 | 44 | /** 45 | * Register new widget. 46 | * 47 | * @param string $name 48 | * @param string|callable $callback 49 | */ 50 | public function register($name, $callback) 51 | { 52 | $this->widgets[$name] = $callback; 53 | 54 | $this->registerBlade($name); 55 | } 56 | 57 | /** 58 | * Register widget using a specified handler class. 59 | * 60 | * @param string $subscriber 61 | */ 62 | public function subscribe($subscriber) 63 | { 64 | list($className, $method) = Str::parseCallback($subscriber, 'subscribe'); 65 | 66 | $instance = $this->container->make($className); 67 | 68 | call_user_func_array([$instance, $method], [$this]); 69 | } 70 | 71 | /** 72 | * Register blade syntax for a specific widget. 73 | * 74 | * @param string $name 75 | */ 76 | protected function registerBlade($name) 77 | { 78 | $this->blade->extend(function ($view, $compiler) use ($name) { 79 | $pattern = $this->createMatcher($name); 80 | 81 | $replace = '$1'; 82 | 83 | return preg_replace($pattern, $replace, $view); 84 | }); 85 | } 86 | 87 | /** 88 | * Get the regular expression for a generic Blade function. 89 | * 90 | * @param string $function 91 | * @return string 92 | */ 93 | protected function createMatcher($function) 94 | { 95 | return '/(?widgets); 108 | } 109 | 110 | /** 111 | * Calling a specific widget. 112 | * 113 | * @param string $name 114 | * @param array $parameters 115 | * 116 | * @return mixed 117 | */ 118 | public function call($name, array $parameters = array()) 119 | { 120 | return $this->get($name, $parameters); 121 | } 122 | 123 | /** 124 | * Calling a specific widget. 125 | * 126 | * @param string $name 127 | * @param array $parameters 128 | * 129 | * @return mixed 130 | */ 131 | public function get($name, array $parameters = array()) 132 | { 133 | if ($this->hasGroup($name)) { 134 | return $this->callGroup($name, $parameters); 135 | } 136 | 137 | if ($this->has($name)) { 138 | $callback = $this->widgets[$name]; 139 | 140 | return $this->getCallback($callback, $parameters); 141 | } 142 | 143 | return null; 144 | } 145 | 146 | /** 147 | * Get a callback from specific widget. 148 | * 149 | * @param mixed $callback 150 | * @param array $parameters 151 | * 152 | * @return mixed 153 | */ 154 | protected function getCallback($callback, array $parameters) 155 | { 156 | if ($callback instanceof Closure) { 157 | return $this->createCallableCallback($callback, $parameters); 158 | } elseif (is_string($callback)) { 159 | return $this->createStringCallback($callback, $parameters); 160 | } else { 161 | return; 162 | } 163 | } 164 | 165 | /** 166 | * Get a result from string callback. 167 | * 168 | * @param string $callback 169 | * @param array $parameters 170 | * 171 | * @return mixed 172 | */ 173 | protected function createStringCallback($callback, array $parameters) 174 | { 175 | if (function_exists($callback)) { 176 | return $this->createCallableCallback($callback, $parameters); 177 | } else { 178 | return $this->createClassesCallback($callback, $parameters); 179 | } 180 | } 181 | 182 | /** 183 | * Get a result from callable callback. 184 | * 185 | * @param callable $callback 186 | * @param array $parameters 187 | * 188 | * @return mixed 189 | */ 190 | protected function createCallableCallback($callback, array $parameters) 191 | { 192 | return call_user_func_array($callback, $parameters); 193 | } 194 | 195 | /** 196 | * Get a result from classes callback. 197 | * 198 | * @param string $callback 199 | * @param array $parameters 200 | * 201 | * @return mixed 202 | */ 203 | protected function createClassesCallback($callback, array $parameters) 204 | { 205 | list($className, $method) = Str::parseCallback($callback, 'register'); 206 | 207 | $instance = $this->container->make($className); 208 | 209 | $callable = array($instance, $method); 210 | 211 | return $this->createCallableCallback($callable, $parameters); 212 | } 213 | 214 | /** 215 | * Group some widgets. 216 | * 217 | * @param string $name 218 | * @param array $widgets 219 | */ 220 | public function group($name, array $widgets) 221 | { 222 | $this->groups[$name] = $widgets; 223 | 224 | $this->registerBlade($name); 225 | } 226 | 227 | /** 228 | * Group some widgets, merging if previously set. 229 | * 230 | * @param string $name 231 | * @param array $newWidgets 232 | */ 233 | public function mergeGroup($name, array $newWidgets) 234 | { 235 | $widgets = $this->hasGroup($name) ? $this->groups[$name] : []; 236 | 237 | $this->groups[$name] = array_merge($widgets, $newWidgets); 238 | 239 | $this->registerBlade($name); 240 | } 241 | 242 | /** 243 | * Determine whether a group of widgets there or not. 244 | * 245 | * @param string $name 246 | * 247 | * @return bool 248 | */ 249 | public function hasGroup($name) 250 | { 251 | return array_key_exists($name, $this->groups); 252 | } 253 | 254 | /** 255 | * Call a specific group of widgets. 256 | * 257 | * @param string $name 258 | * @param array $parameters 259 | * 260 | * @return string 261 | */ 262 | public function callGroup($name, $parameters = array()) 263 | { 264 | if (!$this->hasGroup($name)) { 265 | return; 266 | } 267 | 268 | $result = ''; 269 | 270 | foreach ($this->groups[$name] as $key => $widget) { 271 | $result .= $this->get($widget, array_get($parameters, $key, array())); 272 | } 273 | 274 | return $result; 275 | } 276 | 277 | /** 278 | * Get a group of widgets. 279 | * 280 | * @param string $name 281 | * @return array|null 282 | */ 283 | public function getGroup($name) 284 | { 285 | if (!$this->hasGroup($name)) { 286 | return; 287 | } 288 | 289 | return $this->groups[$name]; 290 | } 291 | 292 | /** 293 | * Get a collection of a group of widgets. 294 | * 295 | * @param string $name 296 | * @return \Illuminate\Support\Collection|null 297 | */ 298 | public function collectGroup($name) 299 | { 300 | if (!$this->hasGroup($name)) { 301 | return; 302 | } 303 | 304 | return collect($this->groups[$name]); 305 | } 306 | 307 | /** 308 | * Reorder widgets. 309 | * 310 | * @param array $widgets 311 | * 312 | * @return array 313 | */ 314 | protected function reorderWidgets($widgets) 315 | { 316 | $formatted = []; 317 | 318 | foreach ($widgets as $key => $widget) { 319 | if (is_array($widget)) { 320 | $formatted[] = [ 321 | 'name' => array_get($widget, 0), 322 | 'order' => array_get($widget, 1), 323 | ]; 324 | } else { 325 | $formatted[] = [ 326 | 'name' => $widget, 327 | 'order' => $key, 328 | ]; 329 | } 330 | } 331 | 332 | return collect($formatted)->sortBy(function ($widget) { 333 | return $widget['order']; 334 | })->all(); 335 | } 336 | 337 | /** 338 | * Handle call to the class. 339 | * 340 | * @param string $method 341 | * @param array $parameters 342 | * 343 | * @return mixed 344 | */ 345 | public function __call($method, $parameters = array()) 346 | { 347 | return $this->get($method, $parameters); 348 | } 349 | } 350 | -------------------------------------------------------------------------------- /WidgetFacade.php: -------------------------------------------------------------------------------- 1 | app['widget'] = $this->app->share(function ($app) { 23 | $blade = $app['view']->getEngineResolver()->resolve('blade')->getCompiler(); 24 | 25 | return new Widget($blade, $app); 26 | }); 27 | 28 | $this->app->booting(function () { 29 | $loader = AliasLoader::getInstance(); 30 | $loader->alias('Widget', 'Pingpong\Widget\WidgetFacade'); 31 | 32 | $file = app_path('widgets.php'); 33 | 34 | if (file_exists($file)) { 35 | include $file; 36 | } 37 | }); 38 | } 39 | 40 | /** 41 | * Get the services provided by the provider. 42 | * 43 | * @return array 44 | */ 45 | public function provides() 46 | { 47 | return array('widget'); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pingpong/widget", 3 | "description": "Simple Widget System for Laravel Framework", 4 | "keywords": [ 5 | "widget", 6 | "laravel", 7 | "pingpong" 8 | ], 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Pingpong Labs", 13 | "email": "pingpong.labs@gmail.com" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.5.9", 18 | "illuminate/support": "5.2.*", 19 | "illuminate/container": "5.2.*", 20 | "illuminate/view": "5.2.*", 21 | "illuminate/routing": "5.2.*" 22 | }, 23 | "require-dev": { 24 | "mockery/mockery": "~0.9", 25 | "phpunit/phpunit": "~4" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Pingpong\\Widget\\": "" 30 | } 31 | }, 32 | "minimum-stability": "stable" 33 | } 34 | --------------------------------------------------------------------------------