├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── Facades └── Widgetify.php ├── Widget.php ├── WidgetManager.php ├── WidgetifyServiceProvider.php └── configs └── widgetify.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | .idea 3 | composer.lock 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Morteza Parvini 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 | # Widgetify 2 | Laravel widget package for Laravel >= 5.1 3 | 4 | ## Installation 5 | ``` 6 | composer require morilog/widgetify 7 | ``` 8 | #### Register in Laravel 9 | - Add `Morilog\Widgetify\WidgetifyServiceProvider` to `config/app.php` providers array 10 | 11 | - If you need to Facade for rendering widgets, add bellow in `config/app.php` aliases array : 12 | ```php 13 | 'Widgetify' => Morilog\Widgetify\Facades\Widgetify::class 14 | ``` 15 | 16 | - For publish Widgetify config file, run this command: 17 | ``` 18 | php artisan vendor:publish --provider="Morilog\Widgetify\WidgetifyServiceProvider" 19 | ``` 20 | 21 | --- 22 | ## Usage 23 | #### Create new widget 24 | For creating new widget you must create a class that extended `Morilog\Widgetify\Widget` and implement `handle()` method. 25 | ```php 26 | get(); 36 | 37 | return view('path.to.view.file', compact('latestPosts')); 38 | } 39 | } 40 | 41 | ``` 42 | 43 | #### Registering Widget 44 | - Add your widget to `widgets` array in `config/widgetify.php` file: 45 | 46 | ```php 47 | 'widgets' => [ 48 | 'simple_widget' => App\MyWidgets\SimpleWidget::class 49 | ] 50 | ``` 51 | 52 | #### Rendering Widgets 53 | With blade `@widgetify` directive: 54 | ```php 55 | // views/sidebar.blade.php 56 |
57 | @widgetify('simple_widget') 58 |
59 | ``` 60 | 61 | OR with configs: 62 | ```php 63 | // views/sidebar.blade.php 64 |
65 | @widgetify('simple_widget', ['key' => 'value', 'key2' => 'value']) 66 |
67 | ``` 68 | 69 | OR with `Widgetify` Facade: 70 | ```php 71 | // views/sidebar.blade.php 72 |
73 | {!! Widgetify::render('simple_widgets') !!} 74 |
75 | ``` 76 | 77 | #### Using cache 78 | ```php 79 | // views/default.blade.php 80 |
81 | {!! Widgetify::remember('my_widget', 15, [CONFIGS]); !!} 82 |
83 |
84 | @cached_widgetify('my_widget', 15, [CONFIGS]); 85 |
86 | 87 | ``` 88 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "morilog/widgetify", 3 | "description": "A powerful laravel widget package. capsulate ui componenet as widget. similar to Yii widgets", 4 | "keywords": ["laravel", "widgets", "widget", "view-composer", "morilog", "laratalks"], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Morteza Parvini", 10 | "email": "m.parvini@outlook.com" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.5.9", 15 | "illuminate/support": ">=5.1" 16 | }, 17 | "require-dev": { 18 | "phpunit/phpunit": "~4.0" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "Morilog\\Widgetify\\": "src/" 23 | } 24 | }, 25 | "minimum-stability": "dev" 26 | } 27 | -------------------------------------------------------------------------------- /src/Facades/Widgetify.php: -------------------------------------------------------------------------------- 1 | configs = array_merge($this->configs, $configs); 16 | 17 | return $this; 18 | } 19 | 20 | protected function getConfig($key, $default = null) 21 | { 22 | return array_key_exists($key, $this->configs) ? $this->configs[$key] : $default; 23 | } 24 | } -------------------------------------------------------------------------------- /src/WidgetManager.php: -------------------------------------------------------------------------------- 1 | container = $container; 25 | $this->cache = $cache; 26 | } 27 | 28 | public function render($widget) 29 | { 30 | $args = func_get_args(); 31 | $configs = count($args) > 1 && is_array($args[1]) ? $args[1] : []; 32 | 33 | return $this->buildWidget($widget)->withConfig($configs)->handle(); 34 | } 35 | 36 | public function remember($widget, $minutes) 37 | { 38 | $args = func_get_args(); 39 | $configs = count($args) > 2 && is_array($args[2]) ? $args[2] : []; 40 | 41 | $cacheKey = self::CACHE_PREFIX . sha1($widget . json_encode($configs)); 42 | 43 | if (($result = $this->cache->get($cacheKey)) === false) { 44 | $result = $this->buildWidget($widget)->withConfig($configs)->handle(); 45 | $this->cache->put($cacheKey, (string)$result, $minutes); 46 | } 47 | 48 | return $result; 49 | } 50 | 51 | /** 52 | * @param $widget 53 | * @return Widget 54 | */ 55 | protected function buildWidget($widget) 56 | { 57 | if ($widget instanceof Widget) { 58 | return $widget; 59 | } 60 | 61 | if (is_string($widget) === false) { 62 | throw new \InvalidArgumentException(sprintf('Widget name must be string')); 63 | } 64 | 65 | $widget = class_exists($widget) ? $widget : $this->generateContainerTag($widget); 66 | 67 | if (isset(static::$widgets[$widget]) === false) { 68 | throw new \RuntimeException(sprintf('Widget %s is not exists', $widget)); 69 | } 70 | 71 | return $this->container->make(static::$widgets[$widget]); 72 | } 73 | 74 | /** 75 | * @param $abstract 76 | * @param null $instance 77 | * @return $this 78 | */ 79 | public function registerWidget($abstract, $instance = null) 80 | { 81 | if (is_string($abstract) === false) { 82 | throw new \InvalidArgumentException(sprintf('Widget name must be string')); 83 | } 84 | 85 | if (class_exists($abstract)) { 86 | $instance = $instance !== null ? $instance : $abstract; 87 | 88 | static::$widgets[$abstract] = $instance; 89 | 90 | return $this; 91 | } 92 | 93 | if ($instance === null || class_exists($instance) === false) { 94 | throw new \InvalidArgumentException('%s Could not resolved'); 95 | } 96 | 97 | static::$widgets[$this->generateContainerTag($abstract)] = $instance; 98 | 99 | return $this; 100 | } 101 | 102 | 103 | protected function generateContainerTag($widget) 104 | { 105 | return static::WIDGET_CONTAINER_PREFIX . snake_case($widget); 106 | } 107 | 108 | } -------------------------------------------------------------------------------- /src/WidgetifyServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 13 | __DIR__ . '/configs/widgetify.php' => config_path('widgetify.php'), 14 | ], 'config'); 15 | } 16 | 17 | /** 18 | * Register the service provider. 19 | * 20 | * @return void 21 | */ 22 | public function register() 23 | { 24 | $this->app->singleton('morilog.widgetify', function ($app) { 25 | return new WidgetManager($app); 26 | }); 27 | 28 | $this->registerWidgets($this->app['config']->get('widgetify.widgets', [])); 29 | 30 | $this->registerBladeDirectives(); 31 | } 32 | 33 | protected function registerWidgets(array $widgets = []) 34 | { 35 | foreach ($widgets as $key => $value) { 36 | $name = is_numeric($key) ? $value : $key; 37 | 38 | $this->app['morilog.widgetify']->registerWidget($name, $value); 39 | } 40 | } 41 | 42 | protected function registerBladeDirectives() 43 | { 44 | $hasParaenthesis = version_compare($this->app->version(), '5.3', '<'); 45 | 46 | Blade::directive('widgetify', function ($expression) use ($hasParaenthesis) { 47 | $expression = $hasParaenthesis ? $expression : "($expression)"; 48 | 49 | return "render{$expression}; ?>"; 50 | }); 51 | 52 | Blade::directive('cached_widgetify', function ($expression) use ($hasParaenthesis) { 53 | $expression = $hasParaenthesis ? $expression : "($expression)"; 54 | 55 | return "remember{$expression}; ?>"; 56 | }); 57 | } 58 | 59 | public function provides() 60 | { 61 | return ['morilog.widgetify']; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/configs/widgetify.php: -------------------------------------------------------------------------------- 1 | [ 4 | // Your widgets registered here 5 | // ExmapleWidget::class, 6 | // 'widget_name' => MyWidget::class 7 | ], 8 | ]; 9 | --------------------------------------------------------------------------------