Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Yasumi PHP Library to Laravel (in Laravel way)

I would like to use https://github.com/azuyalabs/yasumi package in a laravel application.

I did the installation using:

$ composer require azuyalabs/yasumi

now, with a laravel package I would, after installing register the service providers and the aliases (facades) in config/app.php:

This library has some facades of it's own.

What would be the way to integrate this library into Laravel ?

Should I create a new Service Provider class and register Yasumi within that ? Something like:

class YasumiServiceProvider extends ServiceProvider
{
   /**
    * Bootstrap the application services.
    *
    * @return void
    */
   public function boot()
   {
       //
   }

   /**
    * Register the application services.
    *
    * @return void
    */
   public function register()
   {

   }
}

I wouldn't do that in the vendor folder as that is to be overwritten or even replaced at times.

like image 706
Angelin Calu Avatar asked Dec 12 '25 14:12

Angelin Calu


2 Answers

Expanding from my comment:

Fork the repository and make changes (note, you can always make "self pull-requests" from original to your fork).

After changes are done use this inside your composer.json file

{
  "name": "...",
  "type": "project",
  "description": "...",
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/{YOUR GITHUB NAME}/yasumi"
    }
  ],
  "require": {
    "php": ">=5.6.4",
    "laravel/framework": "5.3.*",
    "azuyalabs/yasumi": "dev-{NAME OF BRANCH IN FORK}"
  },
  ...

Note: placeholders are in use {YOUR GITHUB NAME} and {NAME OF BRANCH IN FORK}
Note: to sync your fork using webhooks see this.


Additional make a PR so other can use Laravel service provider.

like image 174
Kyslik Avatar answered Dec 15 '25 05:12

Kyslik


Here's a simpler way.

In App\Providers\AppServiceProvider

public function register()
{
    $this->app->singleton('yasumi', \Yasumi\Yasumi::create('USA', 2016));
}

then in your app, you can use

 $holidays = app('yasumi'); // now the equivalent of = \Yasumi\Yasumi::create('USA', 2016)

Then carry on as in the documentation.

You could even avoid the variable assignment, e.g.

foreach(app('yasumi')->getHolidayNames() as $name) 
{
    echo $name . PHP_EOL;
}
like image 24
Mei Gwilym Avatar answered Dec 15 '25 06:12

Mei Gwilym



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!