├── LICENSE ├── README.md ├── composer.json └── src ├── Exceptions └── InvalidWidgetException.php ├── Facades └── Widget.php ├── Twig └── Extensions │ └── Widget.php ├── Widget.php ├── WidgetFactory.php └── WidgetsServiceProvider.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Shea Lewis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This package has been abandoned and is no longer maintained. 2 | 3 | Caffeinated Widgets 4 | ================= 5 | [![Laravel 5.1](https://img.shields.io/badge/Laravel-5.1-orange.svg?style=flat-square)](http://laravel.com) 6 | [![Laravel 5.2](https://img.shields.io/badge/Laravel-5.2-orange.svg?style=flat-square)](http://laravel.com) 7 | [![Source](http://img.shields.io/badge/source-caffeinated/menus-blue.svg?style=flat-square)](https://github.com/caffeinated/menus) 8 | [![License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://tldrlegal.com/license/mit-license) 9 | 10 | Easily create reusable widget components to be used throughout your Laravel application. Widgets are very similar to Laravel's view composers, but in more of a dedicated sense. 11 | 12 | The package follows the FIG standards PSR-1, PSR-2, and PSR-4 to ensure a high level of interoperability between shared PHP code. At the moment the package is not unit tested, but is planned to be covered later down the road. 13 | 14 | Documentation 15 | ------------- 16 | You will find user friendly and updated documentation in the wiki here: [Caffeinated Widgets Wiki](https://github.com/caffeinated/widgets/wiki) 17 | 18 | Quick Installation 19 | ------------------ 20 | Begin by installing the package through Composer. 21 | 22 | ``` 23 | composer require caffeinated/widgets 24 | ``` 25 | 26 | Once this operation is complete, simply add the service provider class and facade alias to your project's `config/app.php` file: 27 | 28 | ##### Service Provider 29 | ```php 30 | Caffeinated\Widgets\WidgetsServiceProvider::class, 31 | ``` 32 | 33 | ##### Facade 34 | ```php 35 | 'Widget' => Caffeinated\Widgets\Facades\Widget::class, 36 | ``` 37 | 38 | And that's it! With your coffee in reach, start building out some awesome widgets! 39 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "caffeinated/widgets", 3 | "description": "Laravel 5 Widgets", 4 | "keywords": ["widgets", "laravel", "caffeinated"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Shea Lewis", 9 | "email": "shea.lewis89@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.5.9", 14 | "illuminate/support": "5.1.*|5.2.*", 15 | "illuminate/config": "5.1.*|5.2.*" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "Caffeinated\\Widgets\\": "src/" 20 | } 21 | }, 22 | "suggest": { 23 | "caffeinated/sapling": "Laravel 5 Twig integration package" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidWidgetException.php: -------------------------------------------------------------------------------- 1 | ['html']] 33 | ), 34 | ]; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Widget.php: -------------------------------------------------------------------------------- 1 | $value) { 21 | if (property_exists($this, $parameter)) { 22 | $this->$parameter = $value; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/WidgetFactory.php: -------------------------------------------------------------------------------- 1 | app = $app; 28 | } 29 | 30 | /** 31 | * Register a new namespace location where widgets may be found. 32 | * 33 | * @param string $namespace 34 | */ 35 | public function register($namespace) 36 | { 37 | if (! array_key_exists($namespace, $this->namespace)) { 38 | $this->namespace[] = $namespace; 39 | } 40 | } 41 | 42 | /** 43 | * Determine the full namespace for the given class. 44 | * 45 | * @param string $className 46 | * @return string 47 | */ 48 | protected function determineNamespace($className) 49 | { 50 | if (count($this->namespace) > 0) { 51 | foreach ($this->namespace as $namespace) { 52 | if (class_exists($namespace.'\\'.$className)) { 53 | return $namespace; 54 | } 55 | } 56 | } 57 | 58 | return 'App\\Widgets'; 59 | } 60 | 61 | /** 62 | * Flattens the given array. 63 | * 64 | * @param array $parameters 65 | * @return array 66 | */ 67 | protected function flattenParameters(array $parameters) 68 | { 69 | $flattened = array(); 70 | 71 | foreach($parameters as $parameter) { 72 | array_walk($parameter, function($value, $key) use (&$flattened) { 73 | $flattened[$key] = $value; 74 | }); 75 | } 76 | 77 | return $flattened; 78 | } 79 | 80 | /** 81 | * Magic method to call widget instances. 82 | * 83 | * @param string $signature 84 | * @param array $parameters 85 | * @return mixed 86 | */ 87 | public function __call($signature, $parameters) 88 | { 89 | $parameters = $this->flattenParameters($parameters); 90 | $className = studly_case($signature); 91 | $namespace = $this->determineNamespace($className); 92 | $widgetClass = $namespace.'\\'.$className; 93 | $widget = $this->app->make($widgetClass); 94 | 95 | if ($widget instanceof Widget === false) { 96 | throw new InvalidWidgetException; 97 | } 98 | 99 | $widget->registerParameters($parameters); 100 | 101 | return $widget->handle(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/WidgetsServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerServices(); 23 | 24 | $this->configureSapling(); 25 | } 26 | 27 | /** 28 | * Register the package services. 29 | * 30 | * @return void 31 | */ 32 | protected function registerServices() 33 | { 34 | $this->app->singleton('widgets', function($app) { 35 | return new WidgetFactory($app['app']); 36 | }); 37 | } 38 | 39 | /** 40 | * Configure Sapling 41 | * 42 | * Configures Sapling (Twig) extensions if the Sapling package 43 | * is found to be installed. 44 | * 45 | * @return void 46 | */ 47 | protected function configureSapling() 48 | { 49 | if ($this->app['config']->has('sapling')) { 50 | $this->app['config']->push( 51 | 'sapling.extensions', 52 | 'Caffeinated\Widgets\Twig\Extensions\Widget' 53 | ); 54 | } 55 | } 56 | 57 | /** 58 | * Get the services provided by the provider. 59 | * 60 | * @return array 61 | */ 62 | public function provides() 63 | { 64 | return ['widgets']; 65 | } 66 | } 67 | --------------------------------------------------------------------------------