├── .gitattributes ├── README.md ├── composer.json └── src ├── Share.php ├── ShareFacade.php ├── ShareServiceProvider.php └── helpers.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Share 2 | Laravel Share is an easy way to share any data through a request. 3 | 4 | ## Introduction 5 | With Laravel Share you can share any data through a request to use later. You should consider it as an array that are available everywhere. I mean EVERYWHERE! From Service Provider to routes, middlewares, controllers and views. 6 | 7 | ## Installation 8 | #### Install using [Composer](https://getcomposer.org/doc/00-intro.md) 9 | ``` 10 | $ composer require peyman3d/laravel-share 11 | ``` 12 | When installation completes you should add provider and alias to `config/app.php` file. If you are using Laravel 5.5 or 5.6 you can skip this step as Laravel can discover this package automatically. 13 | 14 | - Add to providers array: 15 | ```php 16 | Peyman3d\Share::class, 17 | ``` 18 | - Add to alias array: 19 | ```php 20 | 'Share' => Peyman3d\Share\ShareFacade::class, 21 | ``` 22 | 23 | ## Usage 24 | Using Laravel Share is easy as pie. Just think of a person and you can share data like this: 25 | ```php 26 | Share::make('Person') 27 | ->add('name', 'Peyman') 28 | ->add('email', 'salam@peyman.me') 29 | ->add('job', 'Web developer') 30 | ->edit('title', 'Mr'); 31 | ``` 32 | And when you want that data just use `Share::get('Person');`; 33 | 34 | As you see you can create an item with `make()` method. then you can add parameters to it by using `add($key, $value)` or `edit($key, $value)`. You can also use `share()` helper instead of `Share` Facade. 35 | ```php 36 | share()->make('asset.js')->add('react', 'https://cdnjs.cloudflare.com/ajax/libs/react/16.4.0/umd/react.production.min.js') 37 | ``` 38 | 39 | There are some basic methods for working with data array: 40 | ```php 41 | // Change key 42 | share()->key('menu'); 43 | 44 | // Make new item with key and value 45 | share()->make($key, $value, $single); 46 | 47 | // Create new item to current key 48 | share()->make('menu')->item('dashboard'); 49 | 50 | // Prepend an item to current key 51 | share()->make('menu')->prepend('posts'); 52 | 53 | // Add a child to array 54 | share()->make('menu')->item('users')->child('users-list'); 55 | 56 | // Check if key exists 57 | share()->key('menu')->has('dashboard'); 58 | 59 | // Get from array with key 60 | // Second parameter is for get result as single value or collection 61 | share()->key('menu')->get('users', true); 62 | 63 | // Pull data from array and delete it 64 | share()->key('menu')->pull('users', true); 65 | 66 | // Get all data 67 | share()->all(); 68 | 69 | // Delete data for current key 70 | share()->key('menu.users')->delete(); 71 | 72 | // Delete all data 73 | share()->reset(); 74 | 75 | ``` 76 | 77 | ## What data can be shared? 78 | Almost anything. You can share any string, numbers, objects and closures. 79 | ```php 80 | share()->make('user')->add('profile', auth()->user()); 81 | share()->menu('posts')->add('filter', function($value){ 82 | return '

'.$value.'

'; 83 | }); 84 | ``` 85 | 86 | ## Even better helpers 87 | Laravel Share has more helpers to create better syntax. You can use any combination of these helpers. 88 | ```php 89 | share()->make('Job')->title('Senior Developer'); 90 | ``` 91 | As you can see `title()` method accept a value and work just like `add('title', 'Senior Developer')`. 92 | 93 | Check all helpers here: 94 | - `id()` 95 | - `title()` 96 | - `subtitle()` 97 | - `label()` 98 | - `icon()` 99 | - `link()` 100 | - `route()` 101 | - `route_attributes()` 102 | - `href()` 103 | - `fallback()` 104 | - `callback()` 105 | - `order()` 106 | - `class()` 107 | - `desc()` 108 | - `type()` 109 | - `default()` 110 | - `options()` 111 | - `name()` 112 | - `placeholder()` 113 | - `children()` 114 | - `file()` 115 | - `src()` 116 | - `active()` 117 | - `config()` 118 | - `format()` 119 | - `permission()` 120 | - `count()` 121 | - `attributes()` 122 | - `field()` 123 | - `blade()` 124 | 125 | Other than this helpers for parameters, we also have some helpers for sections and types: 126 | 127 | - `menu()` 128 | - `view()` 129 | - `asset()` 130 | - `js()` 131 | - `css()` 132 | - `script()` 133 | - `style()` 134 | 135 | You can check some examples: 136 | ```php 137 | // Create new menu with items 138 | share()->menu()->item('dashboard')->label('Dashboard')->href('/admin/'); 139 | share()->menu()->item('users')->label('Users')->route('admin.users'); 140 | share()->menu('users')->child('users-profile')->label('Profile')->route('admin.users.profile'); 141 | share()->menu('users')->child('users-list')->label('All users')->route('admin.users.index'); 142 | share()->menu('users')->child('users-create')->label('Add new user')->route('admin.users.create'); 143 | 144 | // Manage Assets 145 | share()->js('jquery')->link('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js')->order(500); 146 | share()->js('react')->link('https://cdnjs.cloudflare.com/ajax/libs/react/16.4.0/umd/react.production.min.js')->order(300); 147 | ``` 148 | 149 | ### Custom helpers 150 | I added some basic helpers that works for me, but you can also add yours. You can remove or edit items too. See some examples below: 151 | ```php 152 | // Arguments 153 | share()->addArg('avatar'); 154 | share()->removeArg('icon'); 155 | share()->editArg('subtitle', 'second-title'); 156 | if( share()->hasArg('config') ){ 157 | share()->editArg('config', 'setting'); 158 | } 159 | 160 | // Keys 161 | share()->addKey('table'); 162 | share()->removeKey('view'); 163 | share()->editKey('asset', 'libraries'); 164 | if( share()->hasKey('menu') ){ 165 | share()->editKey('menu', 'menus'); 166 | } 167 | ``` 168 | 169 | 170 | ## Just an example 171 | Imagine you have an admin panel that has a menu on sidebar. This menu has multiple items and each one may have sub-items. 172 | Now, we add some items to the menu: 173 | ```php 174 | // Add dashboard 175 | share()->menu()->item('dashboard')->label('Dashboard')->icon('fa fa-dashboard')->route('admin.dashboard'); 176 | 177 | // Add posts and it's sub items 178 | share()->menu()->item('posts')->label('Posts') 179 | ->icon('fa fa-file-text-o')->route('admin.posts'); 180 | share()->menu('posts')->child('posts-list')->label('All posts') 181 | ->route('admin.posts.index'); 182 | share()->menu('posts')->child('posts-create')->label('Add new post') 183 | ->route('admin.posts.create'); 184 | 185 | // Add setting 186 | share()->menu()->item('settings')->label('Settings')->icon('fa fa-cogs'); 187 | foreach($setting_pages as $page){ 188 | share()->menu('settings')->child('setting-page-'.$page['id'])->label($page['label']) 189 | ->route('admin.settings') 190 | ->route_attributes(['slug'=>$page['slug']]); 191 | } 192 | ``` 193 | These codes can be anywhere: Service Provider, routes, middlewares, controller, model and even a blade view. 194 | If you want you can active a menu item like this: 195 | ```php 196 | // PostController.php 197 | 198 | public function index(){ 199 | share()->key('menu.posts')->activate(); 200 | share()->key('menu.posts.children.posts-list')->activate(); 201 | } 202 | ``` 203 | And you can sort items with `order()` method. It can be done when you add an item or just any other times and places. 204 | ```php 205 | // AdminMiddleware.php 206 | 207 | class AdminMiddleware 208 | { 209 | /** 210 | * Handle an incoming request. 211 | * 212 | * @param \Illuminate\Http\Request $request 213 | * @param \Closure $next 214 | * @return mixed 215 | */ 216 | public function handle($request, Closure $next) 217 | { 218 | share()->key('menu.posts')->order(100); 219 | 220 | return $next($request); 221 | } 222 | } 223 | ``` 224 | If you don't set order, it counts as 100. 225 | 226 | And finally you can use this data in a blade view like `sidebar.blade.php` as simple as this: 227 | ```blade 228 | 256 | ``` 257 | And that's just it :) 258 | Just one thing. I use a helper function called `make_menu_link()` that I wrote for create item link base on what it has. It's not on the package as it may not be useful for you. But you can have it in this [link](https://gist.github.com/peyman3d/97941c94d4b877b4a76075fbc3dce122). -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "peyman3d/laravel-share", 3 | "description": "Laravel shared data package", 4 | "keywords": ["laravel", "share", "package", "peyman3d"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Peyman Eskandari", 9 | "email": "peyman.eskandari@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "dev", 13 | "require": {}, 14 | "autoload": { 15 | "psr-4": { 16 | "Peyman3d\\Share\\": "src/" 17 | } 18 | }, 19 | "extra": { 20 | "laravel": { 21 | "providers": [ 22 | "Peyman3d\\Share\\ShareServiceProvider" 23 | ], 24 | "aliases": { 25 | "Share": "Peyman3d\\Share\\ShareFacade" 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Share.php: -------------------------------------------------------------------------------- 1 | args_types)) { 53 | 54 | $this->edit($method, $arguments[0]); 55 | 56 | return $this; 57 | 58 | } elseif(true === in_array($method, $this->keys_types)) { 59 | 60 | $this->key = array_first($arguments) ? "$method.$arguments[0]" : $method; 61 | 62 | return $this; 63 | 64 | } 65 | } 66 | 67 | /** 68 | * Return $this for helper function 69 | * 70 | * @return $this 71 | */ 72 | public function this(){ 73 | return $this; 74 | } 75 | 76 | public function done(){ 77 | 78 | $this->key = ''; 79 | 80 | return $this; 81 | } 82 | 83 | /** 84 | * Set key 85 | * 86 | * @param $key 87 | * 88 | * @return $this 89 | */ 90 | public function key($key){ 91 | 92 | $this->key = $key; 93 | 94 | return $this; 95 | } 96 | 97 | /** 98 | * Add item to data array 99 | * 100 | * @param $key 101 | * @param array $value 102 | * @param bool $single 103 | * 104 | * @return mixed 105 | */ 106 | public function make($key, $value = [], $single = false){ 107 | 108 | $this->key = $this->key && !$single ? "$this->key.$key" : $key; 109 | 110 | array_set($this->data, $this->key, $value); 111 | 112 | return $this; 113 | 114 | } 115 | 116 | /** 117 | * Make item in key 118 | * 119 | * @param $key 120 | * 121 | * @return mixed 122 | */ 123 | public function item($key){ 124 | 125 | return $this->key("$this->key.$key"); 126 | 127 | } 128 | 129 | /** 130 | * Prepend value to array with key 131 | * 132 | * @param $key 133 | * @param array $value 134 | * 135 | * @return $this 136 | */ 137 | public function prepend($key, $value = []){ 138 | 139 | $this->key .= ".$key"; 140 | 141 | array_prepend($this->data, $this->key, $value); 142 | 143 | return $this; 144 | 145 | } 146 | 147 | /** 148 | * Add child to data array 149 | * 150 | * @param $id 151 | * @param string $child_key 152 | * @return $this 153 | */ 154 | public function child($id, $child_key = 'children'){ 155 | 156 | return $this->item("$child_key.$id"); 157 | 158 | } 159 | 160 | /** 161 | * Add to data array 162 | * 163 | * @param $key 164 | * @param $value 165 | * @return mixed 166 | */ 167 | public function add($key, $value){ 168 | 169 | array_set($this->data, "$this->key.$key", $value); 170 | 171 | return $this; 172 | } 173 | 174 | /** 175 | * Edit data array. Arg for add 176 | * 177 | * @param $key 178 | * @param $value 179 | * @return mixed 180 | */ 181 | public function edit($key, $value){ 182 | 183 | return $this->add($key, $value); 184 | 185 | } 186 | 187 | /** 188 | * Check array has key 189 | * 190 | * @param $key 191 | * 192 | * @return mixed 193 | */ 194 | public function has($key){ 195 | 196 | if( $this->key != '' ){ 197 | $key = "$this->key.$key"; 198 | } 199 | 200 | return array_has($this->data, $key); 201 | 202 | } 203 | 204 | /** 205 | * Get from array 206 | * 207 | * @param $key 208 | * @param bool $single 209 | * 210 | * @return mixed 211 | */ 212 | public function get($key = null, $single = false){ 213 | 214 | if( $this->key != '' && $key ){ 215 | $key = "$this->key.$key"; 216 | } elseif (!$key){ 217 | $key = $this->key; 218 | } 219 | 220 | $result = array_get($this->data, $key, null); 221 | 222 | return $single ? $result : $this->makeOutput($result); 223 | } 224 | 225 | /** 226 | * Get from array and delete from it 227 | * 228 | * @param null $key 229 | * @param bool $single 230 | * 231 | * @return mixed 232 | */ 233 | public function pull($key = null, $single = false){ 234 | 235 | if( $this->key != '' && $key ){ 236 | $key = "$this->key.$key"; 237 | } elseif (!$key){ 238 | $key = $this->key; 239 | } 240 | 241 | $result = array_pull($this->data, $key, null); 242 | 243 | return $single ? $result : $this->makeOutput($result); 244 | 245 | } 246 | 247 | /** 248 | * Get all data from data 249 | * 250 | * @param bool $order 251 | * 252 | * @return mixed 253 | */ 254 | public function all($order = true){ 255 | 256 | return $this->makeOutput($this->data, $order); 257 | 258 | } 259 | 260 | /** 261 | * Delete from data array 262 | * 263 | * @return mixed 264 | */ 265 | public function delete(){ 266 | 267 | array_forget($this->data, $this->key); 268 | 269 | return $this; 270 | } 271 | 272 | /** 273 | * Reset data and key 274 | * 275 | * @return $this 276 | */ 277 | public function reset(){ 278 | 279 | $this->data = []; 280 | $this->key = ''; 281 | 282 | return $this; 283 | } 284 | 285 | /* 286 | * Make output 287 | */ 288 | public function makeOutput($items, $order = true){ 289 | 290 | if( $order ) { 291 | $items = shared_data_sort( $items ); 292 | } 293 | 294 | return collect($items); 295 | 296 | } 297 | 298 | /*----------------------------------------------------------------- 299 | - Helpers 300 | -----------------------------------------------------------------*/ 301 | /** 302 | * Make active key true 303 | * 304 | * @return mixed 305 | */ 306 | public function activate(){ 307 | 308 | return $this->add('active', true); 309 | 310 | } 311 | 312 | /** 313 | * Make active key false 314 | * 315 | * @return mixed 316 | */ 317 | public function deactivate(){ 318 | 319 | return $this->add('active', false); 320 | 321 | } 322 | 323 | /*----------------------------------------------------------------- 324 | - Arg helpers 325 | -----------------------------------------------------------------*/ 326 | /** 327 | * Add arg 328 | * 329 | * @param $arg 330 | * 331 | * @return $this 332 | */ 333 | public function addArg($arg){ 334 | 335 | $this->args_types[] = $arg; 336 | 337 | return $this; 338 | 339 | } 340 | 341 | /** 342 | * Delete arg 343 | * 344 | * @param $arg 345 | * 346 | * @return $this 347 | */ 348 | public function deleteArg($arg){ 349 | 350 | array_splice($this->args_types, array_search($arg, $this->args_types ), 1); 351 | 352 | return $this; 353 | 354 | } 355 | 356 | /** 357 | * Edit arg 358 | * 359 | * @param $old_arg 360 | * @param $new_arg 361 | * 362 | * @return $this 363 | */ 364 | public function editArg($old_arg, $new_arg){ 365 | 366 | $this->deleteArg($old_arg); 367 | 368 | $this->addArg($new_arg); 369 | 370 | return $this; 371 | 372 | } 373 | 374 | /** 375 | * Edit arg 376 | * 377 | * @param $arg 378 | * 379 | * @return $this 380 | */ 381 | public function hasArg($arg){ 382 | 383 | return in_array($arg, $this->args_types); 384 | 385 | } 386 | 387 | /** 388 | * Get available args 389 | * 390 | * @return array 391 | */ 392 | public function getArgs(){ 393 | 394 | return $this->args_types; 395 | 396 | } 397 | 398 | /*----------------------------------------------------------------- 399 | - Key helpers 400 | -----------------------------------------------------------------*/ 401 | /** 402 | * Add key 403 | * 404 | * @param $key 405 | * 406 | * @return $this 407 | */ 408 | public function addKey($key){ 409 | 410 | $this->keys_types[] = $key; 411 | 412 | return $this; 413 | 414 | } 415 | 416 | /** 417 | * Delete key 418 | * 419 | * @param $key 420 | * 421 | * @return $this 422 | */ 423 | public function deleteKey($key){ 424 | 425 | array_splice($this->keys_types, array_search($key, $this->keys_types ), 1); 426 | 427 | return $this; 428 | 429 | } 430 | 431 | /** 432 | * Edit arg 433 | * 434 | * @param $old_key 435 | * @param $new_key 436 | * 437 | * @return $this 438 | */ 439 | public function editKey($old_key, $new_key){ 440 | 441 | $this->deleteKey($old_key); 442 | 443 | $this->addKey($new_key); 444 | 445 | return $this; 446 | 447 | } 448 | 449 | /** 450 | * Edit key 451 | * 452 | * @param $key 453 | * 454 | * @return $this 455 | */ 456 | public function hasKey($key){ 457 | 458 | return in_array($key, $this->keys_types); 459 | 460 | } 461 | 462 | /** 463 | * Get available keys 464 | * 465 | * @return array 466 | */ 467 | public function getKeys(){ 468 | 469 | return $this->keys_types; 470 | 471 | } 472 | } -------------------------------------------------------------------------------- /src/ShareFacade.php: -------------------------------------------------------------------------------- 1 | app->bind('share', '\Peyman3d\Share\Share'); 17 | } 18 | 19 | /** 20 | * Bootstrap any application services. 21 | * 22 | * @return void 23 | */ 24 | public function boot() 25 | { 26 | // Load helpers 27 | require_once('helpers.php'); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 |