A Laravel package which helps you automate creation of files.

Overview

Build Status

Laravel file generator

This is a Laravel package which helps you automate creation of files.

High Res Link

Benefits

  • If you create a type of file frequently, you can automate it and improve productivity.
  • Prevent "context switching" where you lose focus for 30 seconds while you create new files, directories and populate it with boilerplate.
  • Comes with several built-in boilerplates. Easy to share your own as github gists.
  • All boilerplates and generators are part of your repository, letting you share standard templates with your team.
  • Find things like artisan make:model and artisan make:controller useful? You can make your own.
  • All boilerplates can be written as blade templates.

Quick Start

Step 1: Install the package

$ composer require skyronic/laravel-file-generator

Step 2: Add FileGeneratorServiceProvider to your config/app.php

'providers' => [
    // ... other providers ...
    
    \Skyronic\FileGenerator\FileGeneratorServiceProvider::class,
]

Step 3: Publish the "boilerplates" - an included set of useful boilerplates like PHP Classes, Vue Components, etc.

$ php artisan vendor:publish --tag='boilerplates'

Step 4: You can list all the installed boilerplates

$ php artisan generate:list

+---------------+------------------------------+
| Type          | Name                         |
+---------------+------------------------------+
| css           | CSS File                     |
| js            | JavaScript File              |
| php:class     | PHP Class in 'app' Directory |
| php:trait     | PHP Trait in 'app' Directory |
| scope         | Eloquent Global Scope        |
| scss          | SCSS File                    |
| view          | Blade Template               |
| vue:component | Vue Component as a .vue file |
| vue:store     | Vuex Store                   |
+---------------+------------------------------+

Use `artisan generate <type>` to create a new file!

Step 5: You can create a php class now:

$ php artisan generate php:class "Support/Helpers/AwesomeHelper" --extends "BaseHelper" --constructor

Created file [ app/Support/Helpers/AwesomeHelper.php ]

The generator php:class creates one by default in. You can now open app/Support/Helpers/AwesomeHelper.php

<?php

namespace App\Support\Helpers\AwesomeHelper;

class AwesomeHelper extends BaseHelper  {
    public function __construct () {

    }
}

Step 6: Create your own template:

$ php artisan generate:new mytemplate --description "My New Template"

Created new boilerplate at [ resources/boilerplates/mytemplate.boilerplate.txt ]

Understanding Boilerplate Template

Open the file created by Step 6. You will see something like this:

{
   "name": "My Template",
   "out": "edit/me/{{ $name }}.txt",
   "params": {
        "myParam": "optional"
   }
}
---

Template goes here. Blade syntax works. You can use a parameter like {{ $myParam }}

There's two parts to the file, separated by ---.

  • The top part is the configuration of how the template should behave, and also specifying parameters
  • The bottom part is the actual template which will be.

The configuration object

Let's take a closer look at this config object:

{
   "name": "My Template",
   "out": "edit/me/{{ $name }}.txt",
   "params": {
        "myParam": "optional"
   }
}

This should be valid JSON. The key name is the name of the template used for generate:list. Not to be confusued with the $name variable.

Setting Output Path

If you try to run the template with something like:

$ php artisan generate mytemplate foo/bar

The output path here will be: edit/me/foo/bar.txt. $name contains the second parameter, and even the strings can use blade so {{ $name }} will produce the path.

Parameters

Parameters allow you to customize and change the content of the file. For example, here we have myParam. So running this boilerplate with

$ php artisan generate mytemplate foo/bar --myParam "Hello"

Will result in the text file:

Template goes here. Blade syntax works. You can use a parameter like Hello

Flag Parameters

Here's a simple template (some elements omitted for brevity).

"params": {
    "someFlag": "optional"
}
---
This is always visible.

@if($someFlag)
This is only visible when the flag is set
@endif

Now we can run it like:

# $someFlag will be set to false
$ php artisan generate mytemplate foo/bar 

# $someFlag will be set to true
$ php artisan generate mytemplate foo/bar --someFlag

Required Parameters

"params": {
    "className": "required"
}
---
class {{ $className }} {
   
}
$ php artisan generate mytemplate foo/bar

  [Skyronic\FileGenerator\FileGeneratorException]
  Needs argument [ className ]

Optional Parameters

"params": {
    "authorName": "optional"
}
---
@if($authorName)
/* Author: {{ $authorName }} */
@endif
class MyClass {
   
}

You can recognize the if and endif as blade conditional structures. If authorName is set like:

$ php artisan generate mytemplate foo/bar --authorName John

Then the value is set to "John". Else it's null.

Default Values

If you set the parameter to anything except flag or optional or required it's considered a default value.

"params": {
    "copyrightYear": "2017"
}
---

/* Copyright (c) {{ $copyrightYear }} */

The value is going to be set to 2017 unless specified otherwise.

# Set to default value of 2017
$ php artisan generate mytemplate foo/bar

# Override the value to 2016
$ php artisan generate mytemplate foo/bar --copyrightYear 2016

Tips for writing boilerplates

  • A template like vue__component.boilerplate.txt will become vue:component for cleaner organization. You can use __ in your own templates.
  • Important: Pass a --dry-run flag like php artisan generate --dry-run mytemplate foo/bar --myParam "paramvalue" to display the output in console. This lets you iterate and fix any potential issues without creating files.
  • You can use most of laravel's helper functions and even some other PHP classes with some advanced blade and the @php directive
  • You can use paths like foo/bar/{{ $name }} and FileGenerator will automatically adjust directory separators on windows.

If you're using this tool to generate blade files, using keywords like @section and @extends might not work. Instead use @@section and @@extends

For example:

{
   "name": "Blade Template",
   "out": "resources/views/{{ $name }}.blade.php",
   "params": {
       "title": "required"
   }
}
---
@@extends('layouts.main')
@@section("title", '{{ $title }}')

@@section('content')

@@endsection

Formatter

Sometimes you might need to do some string manipulation. Later versions of File Generator will contain more comprehensive string manipulation later.

Camel-case, Snake-case, etc

You can use Laravel's built in helpers for things like camel_case and others.

Basename from path

If you've got something like app/Support/MyHelper.php and want to extract MyHelper you can use Format::baseName ($path) which extracts a classname like entity, ignoring any file extension.

Getting a namespace from path

Namespaces are a bit tricky, since they need to render forward-slashes. FileGenerator contains a simple format helper which can generate a namespace from a given file path. It uses the laravel app directory and App namespace by default.

// $path = "app/Support/Helpers/AwesomeHelper.php"
Format::getNamespace ($path)
// -> "App\Support\Helpers"

// For non `app` directories, you need to manually specify namespace routes
// $path = "tests/Unit/HelperTests/AwesomeHelperTest.php"
Format::getNamespace ($path, 'tests', "Tests")
// -> "Tests\Unit\HelperTests"

Example: PHP Class generator

First, be sure that you've run php artisan vendor:publish --tag='boilerplates' and check app/resources/boilerplates/php__class.boilerplate.txt

{
   "name": "PHP Class in 'app' Directory",
   "out": "app/{{ $name }}.php",
   "params": {
       "extends": "optional",
       "constructor": "flag"
   }
}
---
<?php

namespace {{ Format::getNamespace($path) }};

class {{ Format::baseName($name) }} @if($extends)extends {{ $extends }}@endif  {
@if($constructor)
    public function __construct () {

    }
@endif
}

The example should be pretty self explanatory. But can illustrate that even a little blade templating can go a long way.

You might also like...
Nue Boilerplate - A package for those of you who are tired of coding
Nue Boilerplate - A package for those of you who are tired of coding

Nue Boilerplate Baca Dokumentasi Disini Screenshot Documentation Requirements Installation Configuration Components Alert License Requirements Laravel

Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects. It is to save your time when You start with new scalable Laravel projects with many features Bootstrap, cooreui, infyom admin Generator, roles and  permissions, translatable models, spatie media and much more Until 2018, Backpack v3 used this Base package to offer admin authentication and a blank admin panel using AdminLTE. Backpack v4 no longer uses this package, they're now built-in - use Backpack/CRUD instead.
Until 2018, Backpack v3 used this Base package to offer admin authentication and a blank admin panel using AdminLTE. Backpack v4 no longer uses this package, they're now built-in - use Backpack/CRUD instead.

Note: This package is only used by Backpack v3. Starting with Backpack v4, everything this package does is included in Backpack/CRUD - one package to

⚡️ This package provides a wonderful PHP skeleton to start building your next package idea.
⚡️ This package provides a wonderful PHP skeleton to start building your next package idea.

This package provides a wonderful PHP Skeleton to start building your next package idea. Requires PHP 8.0+ ⚡️ Create your package using Composer: comp

Laravel API starter Kit will provide you with the tools for making API's that everyone will love
Laravel API starter Kit will provide you with the tools for making API's that everyone will love

Laravel API Starter Kit Laravel API starter Kit will provide you with the tools for making API's that everyone will love, API Authentication is alread

This is a Starter Laravel 8 project with Vue 3 and Bootstrap 5 installed for you.

Laravel8-Vue3-Bootstrap5 This is a Starter Laravel 8 project with Vue 3 and Bootstrap 5. Instalation Guide: As always you need to: composer install Th

Kick-start you next Laravel based API with this awesome boilerplate 🚀
Kick-start you next Laravel based API with this awesome boilerplate 🚀

Laravel API boilerplate 🚀 An awesome boilerplate for your next Laravel 9 based API. It's only goal is to simply kick-start your API development and p

Mazer is a Admin Dashboard Template that can help you develop faster. We bring Mazer with Laravel starter project.
Mazer is a Admin Dashboard Template that can help you develop faster. We bring Mazer with Laravel starter project.

Mazer is a Admin Dashboard Template that can help you develop faster. We bring Mazer with Laravel starter project. It's completely free and you can use it in your projects.

A preconfigured Laravel, React, Typescript, Docker boilerplate to save you time!

Laravel - React - Docker - Boilerplate This repo is built with the following: Laravel 9 React 17 Vite 3 ESLint 8 TypeScript 4.7 Husky/Commit lint PHP

Comments
  • Allow multiple output files per boilerplate

    Allow multiple output files per boilerplate

    This would allow one command to generate multiple files (e.g. for the view composers, optionally generate the associated service provider if not already existing based on a flag)

    opened by vpratfr 7
  • Fix separator parsing

    Fix separator parsing

    Randomly started getting this:

    [2017-05-15 22:17:48] local.ERROR: Skyronic\FileGenerator\FileGeneratorException: JSON Exception: [css] Syntax error in /home/vagrant/Projects/Uproar/vendor/skyronic/laravel-file-generator/src/FileParser.php:84
    Stack trace:
    #0 /home/vagrant/Projects/Uproar/vendor/skyronic/laravel-file-generator/src/FileParser.php(71): Skyronic\FileGenerator\FileParser->getMeta('{\r\n   "name": "...')
    #1 /home/vagrant/Projects/Uproar/vendor/skyronic/laravel-file-generator/src/FileParser.php(61): Skyronic\FileGenerator\FileParser->firstPassParse()
    #2 /home/vagrant/Projects/Uproar/vendor/skyronic/laravel-file-generator/src/FileList.php(60): Skyronic\FileGenerator\FileParser->readFile(Object(Symfony\Component\Finder\SplFileInfo))
    #3 /home/vagrant/Projects/Uproar/vendor/skyronic/laravel-file-generator/src/FileGenListCommand.php(23): Skyronic\FileGenerator\FileList->readDirectory('/home/vagrant/P...')
    #4 [internal function]: Skyronic\FileGenerator\FileGenListCommand->handle()
    #5 /home/vagrant/Projects/Uproar/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
    #6 /home/vagrant/Projects/Uproar/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
    #7 /home/vagrant/Projects/Uproar/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
    #8 /home/vagrant/Projects/Uproar/vendor/laravel/framework/src/Illuminate/Container/Container.php(531): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
    #9 /home/vagrant/Projects/Uproar/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Illuminate\Container\Container->call(Array)
    #10 /home/vagrant/Projects/Uproar/vendor/symfony/console/Command/Command.php(264): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
    #11 /home/vagrant/Projects/Uproar/vendor/laravel/framework/src/Illuminate/Console/Command.php(167): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
    #12 /home/vagrant/Projects/Uproar/vendor/symfony/console/Application.php(835): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #13 /home/vagrant/Projects/Uproar/vendor/symfony/console/Application.php(200): Symfony\Component\Console\Application->doRunCommand(Object(Skyronic\FileGenerator\FileGenListCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #14 /home/vagrant/Projects/Uproar/vendor/symfony/console/Application.php(124): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #15 /home/vagrant/Projects/Uproar/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #16 /home/vagrant/Projects/Uproar/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #17 {main}
    

    This fixes the issue for me, no idea if it'd cause anything else to break.

    opened by kylewardnz 3
  • View composers

    View composers

    Could be rather nice too.

    See what I had submitted to the framework, but got rejected:

    https://github.com/laravel/framework/pull/18205

    Should give you a good basis for a generator of yours

    opened by vpratfr 2
Releases(v0.2.0)
Owner
Anirudh Sanjeev
Anirudh Sanjeev
Web-app that helps clinicians with interpreting ECG recordings

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

null 1 Jan 10, 2022
LaraAdmin is a Open source Laravel Admin Panel / CMS which can be used as Admin Backend, Data Management Tool or CRM boilerplate for Laravel with features like Advanced CRUD Generation, Module Manager, Backups and many more.

LaraAdmin 1.0 LaraAdmin is a Open source CRM for quick-start Admin based applications with features like Advanced CRUD Generation, Schema Manager and

Dwij IT Solutions 1.5k Dec 29, 2022
A Laravel admin panel which is creating CRUD for your application automatically.

Adds a zero configuration Admin Panel to your Laravel Application Installation You can install the package via composer: composer require max-hutschen

42coders 10 Aug 24, 2022
Github repository dedicated for my YT tutorial which shows how to use Sessions in Laravel

Sessions in Laravel The following documentation is based on my Laravel Sessions for Beginners tutorial we’re going to cover the basics of sessions in

Code With Dary 11 Nov 25, 2022
A starter template from which to build Laravel + Vite apps

Stack The Laravel framework is fast, clean, and filled with best practices. In this stack, it will handle the backend as an API. The Laravel Vite pack

null 7 Nov 14, 2022
LittleLink Custom provides you with a website similar to Linktree. Many social media platforms only allow you to add one link

LittleLink Custom is a fork of LittleLink Admin with a set goal of making the admin panel easier to use and setup, for inexperienced and first-time users, with the addition of many custom features themed around customization for the individual user's, LittleLink pages.

Julian Prieber 612 Jan 3, 2023
Kick-start laravel prepared package for you!

Laravel Platform Install For development composer install npm install npm run dev ?? npm run prod php artisan migrate --seed For testing composer ins

Nejc 6 Sep 16, 2022
Basic Crud Generator (With Code Files, like GII (YII2)) Using Laravel, Livewire and Tailwind CSS

LiveCrud Live Crud Generator. This package generates Basic Crud with Livewire. Features Generate Complete Crud With Livewire Component and Blade Files

Ritesh Singh 28 Oct 12, 2022
Allows to connect your `Laravel` Framework translation files with `Vue`.

Laravel Vue i18n laravel-vue-i18n is a Vue3 plugin that allows to connect your Laravel Framework JSON translation files with Vue. It uses the same log

Francisco Madeira 361 Jan 9, 2023
With this package you can create wallet for the users.

Laravel User Wallet With this package you can create wallet for the users. Note: Make sure you've already installed php ^8 Installation Install the pa

Mahbod Ahmadi 6 Feb 20, 2022