artisan | Matthew Daly's Bloghttps://matthewdaly.co.uk/blog/categories/artisan/
artisan | I'm a web developer in Norfolk. This is my blog...Sat, 10 Mar 2018 15:12:44 GMThttp://blogs.law.harvard.edu/tech/rssgrunt-blogbuilder https://github.com/matthewbdaly/grunt-blogbuilderMatthew Daly 2018https://matthewdaly.co.uk/blog/2018/01/08/creating-an-artisan-task-to-set-up-a-user-account/
https://matthewdaly.co.uk/blog/2018/01/08/creating-an-artisan-task-to-set-up-a-user-account/Mon, 08 Jan 2018 12:52:39 GMTWhen working with any Laravel application that implements authentication, you’ll need to set up a user account to be able to work with it. One way of doing that is to add a user in a seeder, but that’s only really suitable if every user is going to use the same details.

Instead, you may want to create an Artisan command to set up the user account. Here’s an example of a command that does that:

<?php

namespaceApp\Console\Commands;

useIlluminate\Console\Command;

useHash;

classCreateUserextendsCommand

{

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = 'create:user';

/**

* The console command description.

*

* @var string

*/

protected $description = 'Creates a single user';

/**

* Create a new command instance.

*

* @return void

*/

publicfunction__construct()

{

parent::__construct();

}

/**

* Execute the console command.

*

* @return mixed

*/

publicfunctionhandle()

{

// Get user model from config

$model = config('auth.providers.users.model');

// Let user know what this will do

$this->info('I\'ll ask you for the details I need to set up the user');

// Get username

$name = $this->ask('Please provide the username');

// Get email

$email = $this->ask('Please provide the email address');

// Get password

$password = $this->secret('Please provide the password');

// Create model

$user = new $model;

$user->name = $name;

$user->email = $email;

$user->password = Hash::make($password);

$user->save();

$this->info('User saved');

}

}

We fetch the user model from the config, before asking the user for the data we need. Then we insert it into the database and confirm it to the user.

Then we just need to register the command in App\Console\Kernel.php:

protected $commands = [

\App\Console\Commands\CreateUser::class,

];

And we can run our command with php artisan create:user.

]]>https://matthewdaly.co.uk/blog/2018/01/02/a-laravel-package-boilerplate/
https://matthewdaly.co.uk/blog/2018/01/02/a-laravel-package-boilerplate/Tue, 02 Jan 2018 12:12:15 GMTThe second package I’ve been working on recently is Laravel Package Boilerplate. It’s a basic starter boilerplate for building your own Laravel packages.

It’s not meant to be installed as a project dependency. Instead, run the following command to create a new project boilerplate with it:

This will create a new folder that includes a src folder containing a service provider, and a tests folder containing a preconfigured base test case, as well as a simple test case for tests that don’t need the full application instantiated, in order to help keep your test suite as fast as possible.

In addition, it includes configuration files for:

PHPUnit

PHP CodeSniffer

Travis CI

That way you can start your project off the right way with very little effort.

I’ve also added my Artisan Standalone project as a dependency - that way you can access any Artisan commands you need to generate files you need as follows:

$ vendor/bin/artisan

Hopefully this package should make it a lot easier to create new Laravel packages in future.

]]>https://matthewdaly.co.uk/blog/2018/01/02/using-artisan-from-standalone-laravel-packages/
https://matthewdaly.co.uk/blog/2018/01/02/using-artisan-from-standalone-laravel-packages/Tue, 02 Jan 2018 12:01:10 GMTRecently I’ve been building and publishing a significant number of Laravel packages, and I thought I’d share details of some of them over the next few days.

Artisan Standalone is a package that, when installed in a standalone Laravel package (eg, not in an actual Laravel install, but in a package that you’re building that is intended for use with Laravel), allows you to use Artisan. It’s intended largely to make it quicker and easier to build functionality as separate packages by giving you access to the same generator commands as you have when working with a Laravel application. It came about largely from a need to scratch my own itch, as when building packages I was having to either run Artisan commands in a Laravel app and move them over, or copy them from existing files, which was obviously a pain in the proverbial.

You can install it with the following command:

$ composer require --dev matthewbdaly/artisan-standalone

Once it’s installed, you can access Artisan as follows:

$ vendor/bin/artisan

Note that it doesn’t explicitly include Laravel as a dependency - you’ll need to add that in the parent package to pull in the libraries it needs (which you should be doing anyway). It’s possible that there are some commands that won’t work in this context, but they’re almost certainly ones you won’t need here, such as the migrate command. As far as I can tell the generator commands, which are the only ones we’re really interested in here, all work OK.

]]>https://matthewdaly.co.uk/blog/2018/01/01/creating-artisan-tasks-that-generate-files/
https://matthewdaly.co.uk/blog/2018/01/01/creating-artisan-tasks-that-generate-files/Mon, 01 Jan 2018 16:06:21 GMTWhile the documentation for creating Artisan tasks is generally pretty good, it doesn’t really touch on creating tasks that generate new files. The only way to figure it out was to go digging through the source code. In this case, I was building an Artisan command to create Fractal transformers as part of a package I’m working on.

There’s a specialised class for generating files at Illuminate\Console\GeneratorCommand, which your command class should extend instead of Illuminate\Console\Command. In addition to the usual properties such as the signature and description, you also need to specify $type to give the type of class being generated. Also, note that the constructor is different, so if you use php artisan make:console to create the boilerplate for this command, you’ll need to delete the constructor.

<?php

namespaceMatthewbdaly\MyPackage\Console\Commands;

useIlluminate\Console\GeneratorCommand;

useSymfony\Component\Console\Input\InputArgument;

classTransformerMakeCommandextendsGeneratorCommand

{

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = 'make:transformer {name : The required name of the transformer class}';

/**

* The console command description.

*

* @var string

*/

protected $description = 'Create a Fractal transformer';

/**

* The type of class being generated.

*

* @var string

*/

protected $type = 'Fractal transformer';

/**

* Get the stub file for the generator.

*

* @return string

*/

protectedfunctiongetStub()

{

return__DIR__.'/stubs/transformer.stub';

}

/**

* Get the console command arguments.

*

* @return array

*/

protectedfunctiongetArguments()

{

return [

['name', InputArgument::REQUIRED, 'The name of the command.'],

];

}

/**

* Get the default namespace for the class.

*

* @param string $rootNamespace

* @return string

*/

protectedfunctiongetDefaultNamespace($rootNamespace)

{

return $rootNamespace.'\Transformers';

}

}

Note the getDefaultNamespace() method. If your class will live directly under the app folder this is not necessary. Otherwise, it needs to return the root namespace, with the folder structure you want after it. Here my class will live under app\Transformers, so I’ve set it to reflect that.

Also, note the getStub() method. This tells Artisan that it should use the specified stub file as the basis for our class. Below you’ll find the stub file I used for my transformer:

<?php

namespaceDummyNamespace;

useMatthewbdaly\MyPackage\Transformers\BaseTransformer;

useIlluminate\Database\Eloquent\Model;

classDummyClassextendsBaseTransformer

{

publicfunctiontransform(Model $model)

{

return [

'id' => (int) $model->id,

];

}

}

Note that the DummyNamespace and DummyClass fields will be overwritten with the correct values.

Once this Artisan command is registered in the usual way, you can then run it as follows:

$ php artisan make:transformer Example

And it will generate a boilerplate class something like this:

<?php

namespaceApp\Transformers;

useMatthewbdaly\MyPackage\Transformers\BaseTransformer;

useIlluminate\Database\Eloquent\Model;

classExampleextendsBaseTransformer

{

publicfunctiontransform(Model $model)

{

return [

'id' => (int) $model->id,

];

}

}

You can then replace the model with your own one as necessary, and add any further content to this class.