├── .gitignore ├── README.md ├── composer.json └── src ├── MakeViewCommand.php └── ServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .idea/ 6 | nbproject/ 7 | /nbproject/private/ 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel 5+ View Generator 2 | [![Latest Stable Version](https://poser.pugx.org/maddhatter/laravel-view-generator/v/stable)](https://packagist.org/packages/maddhatter/laravel-view-generator) [![Total Downloads](https://poser.pugx.org/maddhatter/laravel-view-generator/downloads)](https://packagist.org/packages/maddhatter/laravel-view-generator) [![Latest Unstable Version](https://poser.pugx.org/maddhatter/laravel-view-generator/v/unstable)](https://packagist.org/packages/maddhatter/laravel-view-generator) [![License](https://poser.pugx.org/maddhatter/laravel-view-generator/license)](https://packagist.org/packages/maddhatter/laravel-view-generator) 3 | 4 | This is a tiny package to add a `php artisan make:view` command to quickly create blade views. 5 | 6 | ## Installing 7 | 8 | Require the package with composer using the following command: 9 | 10 | composer require maddhatter/laravel-view-generator --dev 11 | 12 | Or add the following to your composer.json's require section and `composer update` 13 | 14 | ```json 15 | "require-dev": { 16 | "maddhatter/laravel-view-generator": "dev-master" 17 | } 18 | ``` 19 | 20 | Or if you always want it included regardless of environment, just add it to the `providers` array in `config/app.php` 21 | 22 | ## Usage 23 | 24 | 25 | ### Create a New View 26 | 27 | ``` 28 | php artisan make:view path.to.your.view 29 | ``` 30 | 31 | Use the same dotted notation to your view that you would pass to the `view()` command. The directory will be created if it doesn't already exist. 32 | 33 | Note: If there are multiple paths defined in your `config/view.php`'s `paths` array, this package will use the first path. 34 | 35 | ### Extend Another View 36 | 37 | ``` 38 | php artisan make:view path.to.your.view -e path.to.parent.view 39 | ``` 40 | 41 | You can optionally extend another view by adding the `-e` parameter and providing the name of the view you want to extend. It will parse the parent view for `@yield()` directives and create the corresponding `@section` / `@endsection` tags. To exclude a section from automatic creation, begin the name with an underscore, e.g.: `_meta` 42 | 43 | #### Example 44 | 45 | Imagine you have the following layout defined: 46 | 47 | resources/views/layouts/master.blade.php 48 | 49 | ``` 50 | 51 | 52 | 53 | 54 | @yield('_meta') 55 | 56 | 57 | 58 |
59 | @yield('content') 60 |
61 | 62 | 63 | @yield('scripts') 64 | 65 | 66 | 67 | ``` 68 | 69 | And you run: 70 | 71 | ``` 72 | php artisan make:view pages.home -e layouts.master 73 | ``` 74 | 75 | The following will be created: 76 | 77 | resources/views/pages/home.blade.php 78 | 79 | ``` 80 | @extends('layouts.master') 81 | 82 | @section('content') 83 | @endsection 84 | 85 | @section('scripts') 86 | @endsection 87 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "maddhatter/laravel-view-generator", 3 | "description": "Tiny package to create a make:view command for Laravel 5+", 4 | "authors": [ 5 | { 6 | "name": "Shawn Tunney", 7 | "email": "shawn.tunney@gmail.com" 8 | } 9 | ], 10 | "require": { 11 | "php": ">=5.4.0", 12 | "illuminate/support": ">5.0", 13 | "illuminate/view": ">5.0", 14 | "illuminate/filesystem": ">5.0", 15 | "illuminate/console": ">5.0" 16 | }, 17 | "license": "MIT", 18 | "autoload": { 19 | "psr-4": { 20 | "MaddHatter\\ViewGenerator\\": "src/" 21 | } 22 | }, 23 | "extra": { 24 | "laravel": { 25 | "providers": [ 26 | "MaddHatter\\ViewGenerator\\ServiceProvider" 27 | ] 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/MakeViewCommand.php: -------------------------------------------------------------------------------- 1 | file = $file; 47 | $this->view = $viewFactory; 48 | 49 | parent::__construct(); 50 | } 51 | 52 | /** 53 | * Execute the console command. 54 | * 55 | * @return mixed 56 | */ 57 | public function handle() 58 | { 59 | $views = $this->viewsToCreate(); 60 | 61 | foreach ($views as $name => $path) { 62 | $directory = dirname($path); 63 | 64 | if ($this->file->exists($path) && ! $this->option('force')) { 65 | $this->error("A view already exists at {$path}!"); 66 | continue; 67 | } 68 | 69 | if ( ! $this->file->exists($directory)) { 70 | $this->file->makeDirectory($directory, 0777, true); 71 | } 72 | 73 | $this->file->put($path, $this->view($name)); 74 | $this->info("Created a new view at {$path}"); 75 | } 76 | } 77 | 78 | protected function viewsToCreate() 79 | { 80 | if ($this->option('resource')) { 81 | $folder = $this->argument('viewname'); 82 | 83 | return [ 84 | "$folder.index" => $this->name2Path("$folder.index"), 85 | "$folder.create" => $this->name2Path("$folder.create"), 86 | "$folder.edit" => $this->name2Path("$folder.edit"), 87 | "$folder.show" => $this->name2Path("$folder.show"), 88 | ]; 89 | } 90 | 91 | return [$this->argument('viewname') => $this->name2Path($this->argument('viewname'))]; 92 | } 93 | 94 | protected function name2Path($name) 95 | { 96 | return config('view.paths')[0] . '/' . str_replace('.', '/', $name) . '.blade.php'; 97 | } 98 | 99 | protected function view($name) 100 | { 101 | if ($extends = $this->extending()) { 102 | return $this->renderView($extends); 103 | } 104 | 105 | return $name; 106 | } 107 | 108 | protected function extending() 109 | { 110 | return empty($this->option('extend')) ? false : $this->option('extend'); 111 | } 112 | 113 | protected function renderView($extends) 114 | { 115 | $content = []; 116 | 117 | $content[] = "@extends('{$extends}')"; 118 | 119 | if ( ! $this->view->exists($extends)) { 120 | $this->warn("Could not find view: {$extends}"); 121 | 122 | return join(PHP_EOL, $content); 123 | } 124 | 125 | $path = $this->view->getFinder()->find($extends); 126 | $parent = $this->file->get($path); 127 | 128 | if (preg_match_all('/\B@(\w+)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $parent, $matches)) { 129 | for ($i = 0; $i < count($matches[1]); $i++) { 130 | // Skip sections starting w/ a _ 131 | if (str_starts_with($matches[4][$i], '\'_')) { 132 | continue; 133 | } 134 | 135 | switch ($matches[1][$i]) { 136 | case 'yield': 137 | case 'section': 138 | $content[] = PHP_EOL . "@section({$matches[4][$i]})" . PHP_EOL . '@endsection'; 139 | break; 140 | case 'stack': 141 | $content[] = PHP_EOL . "@push({$matches[4][$i]})" . PHP_EOL . '@endpush'; 142 | break; 143 | } 144 | } 145 | } 146 | 147 | return join(PHP_EOL, $content); 148 | } 149 | 150 | 151 | } 152 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | commands(MakeViewCommand::class); 16 | } 17 | 18 | } 19 | 20 | --------------------------------------------------------------------------------