Skip to content

Commit

Permalink
Merge pull request #4 from limewell/1.x
Browse files Browse the repository at this point in the history
1.x
  • Loading branch information
dipeshsukhia authored Sep 28, 2021
2 parents c1e3e1b + 2f959cb commit 0ac0bb7
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ All notable changes to `laravel-make-extender` will be documented in this file

1.0.1 - 2021-06-08
- invokable service, cast, collection macro

1.0.2 - 2021-09-88
- View composers
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
4. Generate Global Scope class for Model
5. Generate Custom Casts
6. Generate Collections Macros
7. Generate View Composers

[![Latest Version on Packagist](https://img.shields.io/packagist/v/limewell/laravel-make-extender.svg?style=flat-square)](https://packagist.org/packages/limewell/laravel-make-extender)
[![Total Downloads](https://img.shields.io/packagist/dt/limewell/laravel-make-extender.svg?style=flat-square)](https://packagist.org/packages/limewell/laravel-make-extender)
Expand Down Expand Up @@ -79,6 +80,31 @@ php artisan make:macro toUpper
```
see document [here](https://laravel.com/docs/8.x/collections#extending-collections) for how to use Macro


## Generate View composers
Generate config file for register view composers
```php
php artisan vendor:publish --provider="Limewell\LaravelMakeExtender\LaravelMakeExtenderServiceProvider" --tag="config"
```

Generate view composers class
```php
php artisan make:composer MovieComposer
```
Register view composers Edit config (config/viewcomposers.php)

```php
use App\ViewComposers\MovieComposer;

return [
MovieComposer::class => [
'view1','view2'
],
];
```

see document [here](https://laravel.com/docs/8.x/views#view-composers) for how to use View Composers

## Customize Stubs
```php
php artisan vendor:publish --provider="Limewell\LaravelMakeExtender\LaravelMakeExtenderServiceProvider" --tag="stubs"
Expand Down
2 changes: 1 addition & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
*/
return [

];
];
22 changes: 22 additions & 0 deletions config/viewcomposers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
|--------------------------------------------------------------------------
| View Composers
|--------------------------------------------------------------------------
|
| Particular bald files
| App\Http\ViewComposers\MenuComposers::class => [
| 'welcome',
| 'layout.main'
| ]
|
| All bald files
| App\Http\ViewComposers\MenuComposers::class => [
| '*'
| ]
|
*/

return [
];
60 changes: 60 additions & 0 deletions src/Console/Commands/MakeViewComposerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Limewell\LaravelMakeExtender\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class MakeViewComposerCommand extends GeneratorCommand
{
protected $name = 'make:composer';

protected $description = 'Create a new view composer';

protected $type = 'Composer';

/**
* @return string
*/
protected function getStub(): string
{
return $this->resolveStubPath('viewcomposer.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath(string $stub): string
{
return file_exists($customPath = $this->laravel->basePath("stubs/vendor/laravel-make-extender/".$stub))
? $customPath
: __DIR__. "/../../../stubs/".$stub;
}

/**
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace . '\ViewComposers';
}

/**
* @return bool
* @throws FileNotFoundException
*/
public function handle(): bool
{
$handle = parent::handle();

if ($handle === false) {
return false;
}

return true;
}
}
26 changes: 24 additions & 2 deletions src/LaravelMakeExtenderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
MakeTraitCommand,
MakeScopeCommand,
MakeCastCommand,
MakeMacroCommand
MakeMacroCommand,
MakeViewComposerCommand
};
use Illuminate\Support\ServiceProvider;

Expand All @@ -34,6 +35,21 @@ private function getIncludes($dir, array &$files = []): array
return $files;
}

/**
* Register the application view composer.
*
* @return void
*/
public function ViewComposer()
{
$composers = config('viewcomposers', []);
if(count($composers)) {
foreach ($composers as $composer => $views) {
$this->app->make('view')->composer($views, $composer);
}
}
}

/**
* Bootstrap the application services.
*/
Expand All @@ -59,14 +75,19 @@ public function boot()
__DIR__ . '/../stubs' => base_path('stubs/vendor/laravel-make-extender'),
], 'stubs');

$this->publishes([
__DIR__ . '/../config/viewcomposers.php' => base_path('config/viewcomposers.php'),
], 'config');

// Registering package commands.
$this->commands([
MakeHelperCommand::class,
MakeServiceCommand::class,
MakeTraitCommand::class,
MakeScopeCommand::class,
MakeMacroCommand::class,
MakeCastCommand::class
MakeCastCommand::class,
MakeViewComposerCommand::class
]);
}
}
Expand All @@ -76,6 +97,7 @@ public function boot()
*/
public function register()
{
self::ViewComposer();
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'laravel-make-extender');

Expand Down
25 changes: 25 additions & 0 deletions stubs/viewcomposer.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace DummyNamespace;

use Illuminate\View\View;

class DummyClass
{
public function __construct()
{

}

/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$data = ['view','compose'];
$view->with('data',$data);
}
}

0 comments on commit 0ac0bb7

Please sign in to comment.