Coder Social home page Coder Social logo

laravel-initializer's Introduction

Application installation command

Latest Version on Packagist Software License Build Status StyleCI Coverage Status Quality Score Total Downloads

This package adds app:install and app:update artisan commands, which runs predefined commands related to the current environment to initialize your application.

Installation

For Laravel <= 5.4 - Via Composer

composer require mad-web/laravel-initializer:~0.1.0

add the service provider in config/app.php file:

'providers' => [
    ...
    MadWeb\Initializer\InitializerServiceProvider::class,
];

For Laravel >= 5.5 - Via Composer

composer require mad-web/laravel-initializer

Run artisan make:initializers command to create install and update config classes in app directory.

You can override config key which stores current environment value, just publish config file, and set env_config_key value.

php artisan vendor:publish --provider="MadWeb\Initializer\InitializerServiceProvider" --tag=config

by default value is set to app.env for laravel, in most cases you don't need to override this value.

Usage

Usage of app:install and app:update command are the same except that app:install uses Install class and app:update uses Update class.

Install class contents:

namespace App;

use MadWeb\Initializer\Contracts\Runner;

class Install
{
    public function production(Runner $run)
    {
        return $run
            ->artisan('key:generate')
            ->artisan('migrate')
            ->external('npm', 'install', '--production')
            ->external('npm', 'run', 'production')
            ->artisan('route:cache')
            ->artisan('config:cache')
            ->artisan('optimize')
            ->external('composer', 'dump-autoload', '--optimize');
    }

    public function local(Runner $run)
    {
        return $run
            ->artisan('key:generate')
            ->artisan('migrate')
            ->external('npm', 'install')
            ->external('npm', 'run', 'development');
    }
}

You can add any another method which should be called the same as your environment name, for example staging, and define different commands.

If you need to run commands with root privileges separately, you can define a method according to the following convention.

namespace App;

use MadWeb\Initializer\Contracts\Runner;
use MadWeb\Initializer\Jobs\Supervisor\MakeQueueSupervisorConfig;
use MadWeb\Initializer\Jobs\Supervisor\MakeSocketSupervisorConfig;

class Install
{
    public function local(Runner $run)
    {
        return $run
            ->artisan('key:generate')
            ->artisan('migrate')
            ->external('npm', 'install')
            ->external('npm', 'run', 'development');
    }

    public function localRoot(Runner $run)
    {
        return $run
            ->dispatch(new MakeQueueSupervisorConfig)
            ->dispatch(new MakeSocketSupervisorConfig)
            ->external('supervisorctl', 'reread')
            ->external('supervisorctl', 'update');
    }
}

Run it by passing "root" option:

artisan app:install --root

If you want to move config classes from the app directory to a different place, just rebind project.installer and project.updater keys in the AppServiceProvider.

$this->app->bind('project.installer', \AnotherNameSpace\Install::class);
$this->app->bind('project.updater', \AnotherNameSpace\Update::class);

List of commands available to run

$run
    ->artisan('command', ['argument' => 'argument_value', '-param' => 'param_value', '--option' => 'option_value', ...]) // Artisan command
    ->external('command', 'argument', '-param', 'param_value', '--option=option_value', ...) // Any external command by array
    ->external('command argument -param param_value --option=option_value') // Any external command by string
    ->callable('command', 'argument', ...) // Callable function (like for call_user_func)
    ->dispatch(new JobClass) // Dispatch job task
    ->dispatchNow(new JobClass) // Dispatch job task without queue
    ->publish(ServiceProvider::class) // Publish single service provider assets
    ->publish([
        ServiceProvider::class,
        AnotherServiceProvider::class,
    ]) // Publish multiple packages assets
    ->publish([ServiceProvider::class => 'tag']) // Publish package assets with tag

Useful jobs

Laravel initializer provides some useful jobs to make initializing of your application much easier.

Create cron task for scheduling tasks

To enable Laravel Scheduling add dispatch MakeCronTask job to runner chain to create cron task for your application.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\MakeCronTask)

This job will add

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

to crontab list.

Create laravel-echo-server.json config file

If you use Laravel Echo Server for broadcasting events in your application, add dispatch MakeEchoServerConfig job to runner chain to create configuration file for laravel-echo-server.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\MakeEchoServerConfig);

It will create configuration file with default options of laravel-echo-server and prefilled values from your laravel application configuration.

You can override default value by passing array into the job constructor. It would be a good practice to create additional config value for laravel-echo-server in broadcasting.php config:

/*
|--------------------------------------------------------------------------
| Laravel Echo server configurations
|--------------------------------------------------------------------------
|
| Here you may define all of laravel echo server options
|
*/
'server' => [
    'authEndpoint' => '/broadcasting/auth',
    'port' => env('SOCKET_PORT', '6001'),
    'sslCertPath' => env('SSL_CERT', ''),
    'sslKeyPath' => env('SSL_PATH', '')
],

And pass these values to MakeEchoServerConfig job constructor.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\MakeEchoServerConfig(config('broadcasting.server')));

Create supervisor config file for queues

This job creates supervisor config file for queue workers. Just add dispatch MakeQueueSupervisorConfig job to runner chain.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\Supervisor\MakeQueueSupervisorConfig);

This job creates configuration file with the command php artisan queue:work --sleep=3 --tries=3 in /etc/supervisor/conf.d/ folder by default, with a filename according to this convention your-application-name-queue.conf.

If you want to override default options just pass it into job constructor. For example if you want to use Laravel Horizon instead of default queue workers.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\Supervisor\MakeQueueSupervisorConfig([
        'command' => 'php artisan horizon',
    ]));

Create supervisor config file for laravel echo server

On the same way as MakeQueueSupervisorConfig this job creates supervisor config file for launching laravel echo server. Just add dispatch MakeSocketSupervisorConfig job to runner chain. The difference from MakeQueueSupervisorConfig is the command node ./node_modules/.bin/laravel-echo-server start and the config filename is your-application-name-socket.conf.

Both config files save log files to your-project-path/storage/logs.

Installation by one command

It would be nice to have ability to install an application by one command. We provide nice hack to implement this behavior.

Add project-install script into scripts section in composer.json.

scripts": {
    ...
    "project-install": [
        "@composer install --no-scripts",
        "@php artisan app:install"
    ],
    ...
},

Then you can run just

composer project-install

to initialize your project.

If your application has commands that requires root privileges and you use Unix based system, add the following command into your runner chain.

public function local(Runner $run)
{
    return $run
        ->artisan('key:generate')
        ->artisan('migrate')
        ->external('npm', 'install')
        ->external('npm', 'run', 'development')
        ->external('sudo', 'php', 'artisan', 'app:install', '--root');
}

public function localRoot(Runner $run)
{
    return $run
        ->dispatch(new MakeQueueSupervisorConfig)
        ->dispatch(new MakeSocketSupervisorConfig)
        ->external('supervisorctl', 'reread')
        ->external('supervisorctl', 'update');
}

Upgrading

Please see UPGRADING for details.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-initializer's People

Contributors

zembrowski avatar mnabialek avatar

Stargazers

SylveK avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.