Support the ongoing development of Laravel.io →
posted 9 years ago
IOC Packages
Last updated 1 year ago.
0
Solution

If it's installed via Composer then is already available in any part of your application. You didn't bother to mention which package are you talking about so I'll assume it's UnknownPackage from UnknownVendor:

// Instantiate the library
$library = new \UnknownVendor\UnknownPackage\MainLibraryClass();

// Use the library
$result = $library->someMethod('foo')->anotherMethod('bar');

But creating a service provider is a piece of cake:

<?php namespace App\Providers;

class UnknownPackageServiceprovider extends \Illuminate\Support\ServiceProvider {

	/**
	* Indicates if loading of the provider is deferred.
	*
	* @var bool
	*/
	protected $defer = false;

	/**
	* Register the service provider.
	*
	* @return void
	*/
	public function register()
	{
		// Bind 'unknown.package' shared component to the IoC container
		$this->app->singleton('unknown.package', function($app)
		{
			return new \UnknownVendor\UnknownPackage\MainLibraryClass();
		});
	}
}

And same for the facade

<?php namespace App\Facades;

class Foo extends \Illuminate\Support\Facades\Facade
{
	/**
	* Get the registered name of the component.
	*
	* @return string
	*/
	protected static function getFacadeAccessor()
	{
		return 'unknown.package';
	}
}

Note I'm assuming you are using the App namespace from Laravel 5. After adding both the service provider and the the facade to your config/app.php file (and assuming the choosed facade alias name is 'Foo') then you could use

Foo::someMethod('foo')->anotherMethod('bar');
Last updated 1 year ago.
0

Hey Stolz,

Thanks for that explanation. The package in question is https://packagist.org/packages/viion/xivpads-lodestoneapi (sorry for not specifying that first).

I followed your suggestion and it seems to work, but only partially - I can't call any methods statically, for example:

Non-static method Viion\Lodestone\API::get() should not be called statically, assuming $this from incompatible context

I assume this has something to do with the way this class is written, but I can now at least instantiate it with

$lodestone = new Lodestone;

I'm happy with that if there's no easy way to make it available statically. The only other concern I have is that these added files might get lost whenever the package is updated via Composer - is that possible?

Regardless, thanks again for your help :)

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Riari riari Joined 20 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.