Packages commands priority in Laravel - php

I would like to redefine default make:migration command in my composer package. It works if I add this command in Console/Kernel.php, but I want to use it automatically through Service Provider. Commands like make:model, make:controller, etc was redifined in this package correctly. Can I solve this problem in package without any actions in main project (not package) composer?
if ($this->app->runningInConsole()) {
$this->commands([
Migrations::class,
]);
}

Related

Laravel - Migrate database on package register

I'm creating a laravel package and want to run a migration file in the service provider register method.
In other laravel packages I want to add entries to the new database table, which should also be handled in the register method.
The database is created before running composer install.
How can I run the migration in the register method?
How can I create an entry for the new database table from an other laravel package within the register method?
You can register migrations with this command:
$this->publishes([
__DIR__.'/../database/migrations/' => database_path('migrations')
], 'migrations');
After installing your package, user should run php artisan vendor:publish command to publish configs, migrations, commands etc.
Then you can try to call commands your a code:
Artisan::call('vendor:publish');

Publishing config and migrations from included package in Laravel

I'm using Laravel to build a package which will include some Admin functionality and will include some other packages.
I have included a package which has migrations and a config file, But how do I copy those to the correct folder in Laravel. The package has it's own ServiceProvider but How do I call that from my package?
I'm using the following to register the package in my package.
class CldServiceProvider extends ServiceProvider {
public function register()
{
$this->app->register(MediaLibraryServiceProvider::class);
}
}
I've tried
php artisan vendor:publish
But it says there's nothing to publish. Because it's only looking at the packages for this Laravel installation, and not the nested packages.
In my case I found I needed to include MY package in the application I was working on. This step was not included in the original tutorial I followed.
So in composer.json in the main app:
"repositories": [
{
"type": "path",
"url": "packages/namespace/name",
"options": {
"symlink": true
}
}
],
"require": {
// ...
"namespace/name": "dev-master"
},
And then we run this command from main folder:
composer update
After that running php artisan vendor:publish will include the option to publish the sub packages.
NB: The source for this info is: https://laraveldaily.com/how-to-create-a-laravel-5-package-in-10-easy-steps/
Try to publish the config separately and use the force switch
php artisan vendor:publish --tag=config --force
As #scrubmx mentioned, you need to include the code that defines what can be published, though this code shouldn't really be in your own service provider, but rather the other package you're including. If it doesn't seem to have this code, it's best to list it as an issue on that package's repository or create a pull request to add it.
Create database/migrations folder in your package root folder. Then add publish details in boot method in your service provider.
final class MyPackageServiceProvider extends BaseServiceProvider
{
public function boot(): void
{
$this->publishes([
__DIR__ . '/../database/migrations/' => database_path('migrations/my-package'),
], 'my-package-migrations');
}
}
Second part could be like database_path('migrations') or database_path('migrations/my-package'). I advise using with subfolder because if there are many packages migrations folder grows too much.
The part of 'my-package-migrations' is our tag. And run publish command with tag.
php artisan vendor:publish --tag=my-package-migrations
If you are using the subfolder php artisan migrate command won't work because we need to specify to exact migration folder like this,
php artisan migrate --path=/database/migrations/my-package
Yes, this looks a little bit dirty but organizes cables. And don't forget the using composer dump-autoload. If the service provider is registered this solution works. Also, you can use this method for config but config folder does not support subfolders.
I think you have publish the config files yourself
$this->publishes([
'other-package/absolute/path/some-config.php' => config_path('my-package.php'),
]);
Solution
I included the /packages/ folder in my current project where I was working on.
The problem is that when including the package in my composer.json
It is now reachable from 2 places inside my project, in the vendor folder and in my packages folder. This was causing issue's
I moved my packages outside my current project, this fixed the problem.
Publish vendor migration :
php artisan vendor:publish --tag=migration
Publish vendor config :
php artisan vendor:publish --tag=config

PHPStorm plugin for Lumen just like Laravel

I found this tutorial to install a plugin for Laravel and have its methods completed by PHPStorm.
I does not seem to work for Lumen. Are there any solutions out there that currently support Lumen since Lumen is kind of a subset of Laravel?
Laravel-ide-helper supports Lumen as of August 2015. Here's what you need to do:
Either execute the command
composer require barryvdh/laravel-ide-helper
or add the following to the require key in composer.json:
"barryvdh/laravel-ide-helper": "2.*"
Once you have installed the dependency, edit the file bootstrap/app.php and look for the Register Service Providers section and add the following line:
$app->register(Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
After that, you just need to create the _ide_helper.php file with the following command:
php artisan ide-helper:generate

How to include ServiceProviders in Laravel 5 manually?

I just want to ask a question. I just want to ask if there's a way to include ServiceProviders in Laravel manually? I looked in github page and I saw some laravel serviceprovider. How can I include those in my laravel file? I am new to laravel and I am studying this in our home without an Internet connection. I downloaded the HTMLServiceProvideer manually and I also tried the command:
composer require illuminate/html
Can the composer run without having an Internet Connection? If no how can I install a serviceprovider by using the master-files? Where should I place the files and what file should I need to update?
I believe this is possible but you need to manually do the work of the Composer.
So for instance lets assume want to manually install illuminate/html package.
You can follow these steps:
Add "illuminate/html": "5.*" to your composer.json although we are not using Composer for installing this package, its important we have this listed in composer.json should in case we run 'composer update' in the future our manually added package will not be deleted but rather updated.
"require":{
"laravel/framework": "5.0.*",
"illuminate/html": "5.*"
}
Manually download the package from Github and place the package inside
vendor directory which is located at the root of your laravel application
example in mylaravelapp/vendor
Add the package to mylaravelapp/vendor/composer/autoload_namespaces.php
return array(
.....
'Illuminate\\Html\\' => array($vendorDir . '/illuminate/html'),
);
Add the package to mylaravelapp/vendor/composer/autoload_namespaces.php
return array(
....
'Illuminate\\Html\\' => array($vendorDir . '/illuminate/html'),
);
Run composer dump-autoload to check for errors
Add Package service provider in config/app.php 'providers' => []
Run composer dump-autoload once more
Service Providers are added to your config/app.php
But you will need to have that package downloaded...

How to create composer library package that auto generates code when included

Every time I create a new PHP project I basically use the same MVC folder structure that I adopted and like, I use the same base classes, interfaces, and the same PDO DAL implementation.
When I'm creating a new project I copy&paste all the needed files to the new project in addition to few changes, like changes to namespaces (to match the new project name) etc.
I thought, why not creating a simple script to copy those files and folders, and make the additional changes.
So now, when I create a new project I just run the script and the code is generated automatically, which is much nicer.
And then I thought, I want it to be even simpler. I don't want to save the code in my computer, I want to save it on Github, and since I use and love composer, I thought I will make the Github project a composer package.
But when trying to implement it I realized that I can't make the new composer package auto generate the code that I want, or at least I don't know how to make it do that.
I tried googling it with no success.
Does anyone knows how to achieve this?
I don't see any need to generate code here. Simply add your skeleton files to your Git repository and use Composer's create-project feature. See the third point:
You can use Composer to create new projects from an existing package. This is the equivalent of doing a git clone/svn checkout followed by a composer install of the vendors.
There are several applications for this:
You can deploy application packages.
You can check out any package and start developing on patches for example.
Projects with multiple developers can use this feature to bootstrap the initial application for development.
An example of a major PHP project that supports this approach is Laravel. From its installation instructions:
Via Composer
The Laravel framework utilizes Composer for installation and dependency management. If you haven't already, start by installing Composer.
Now you can install Laravel by issuing the following command from your terminal:
composer create-project laravel/laravel your-project-name --prefer-dist
This command will download and install a fresh copy of Laravel in a new your-project-name folder within your current directory.
If you prefer, you can alternatively download a copy of the Laravel repository from Github manually. Next run the composer install command in the root of your manually created project directory. This command will download and install the framework's dependencies.
Edit:
To have a script run after composer create-project completes you should be able to use a Composer script:
A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command. Scripts are useful for executing a package's custom code or package-specific commands during the Composer execution process.
You are probably looking for the post-create-project-cmd event:
occurs after the create-project command is executed
Depending on whether your script is a static PHP method or a shell executable, you should define it as follows (modified from Composer documentation):
{
"scripts": {
"post-create-project-cmd": "MyVendor\\MyClass::postUpdate"
}
}
or
{
"scripts": {
"post-create-project-cmd": "my-shell-script arg1 arg2"
}
}

Categories