3

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.

Angelin Calu
  • 1,905
  • 8
  • 24
  • 44

2 Answers2

1

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.

Community
  • 1
  • 1
Kyslik
  • 8,217
  • 5
  • 54
  • 87
  • Browsing further I got to this post : http://stackoverflow.com/questions/36745626/cant-load-a-non-laravel-composer-package?rq=1 which made me wonder if it's really that important to load it as service providers or just use it as suggested. – Angelin Calu Dec 09 '16 at 12:43
  • https://github.com/azuyalabs/yasumi#usage I can use it like so. Is there any reason for register a new Service Provider for it instead of just using it ? – Angelin Calu Dec 09 '16 at 12:44
  • You mentioned Laravel way, my answer provides kind of on how to do it, create ServiceProvider in your fork and use your fork from now on. I suggest you make a PR! And when it is merged delete your fork and use original the Laravel Way :), I am not sure I would stick with Provider but thats just personal preference you know. – Kyslik Dec 09 '16 at 12:47
1

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;
}
Mei Gwilym
  • 397
  • 5
  • 15