The complexity of existing packages providing service containers and providers led to the creation of this project. Falcon doesn't force you to create a BIND. If a bind exists for an ID, it uses it; otherwise, it proceeds with AUTOWIRE
.
- add this package to your project:
composer require hesam-mousavi/falcon-container
- add composer autoload:
<?php
require_once __DIR__.'/vendor/autoload.php';
- then create the container:
$container = \HesamMousavi\FalconContainer\FalconContainer::getInstance();
If you want to use a service as a singleton:
$container->singleton('test', Test::class);
// Or
$container->singleton(Test::class);
To execute the service in the first case:
$container->get('test');
In the second case:
$container->get(Test::class);
If you don't
want to use the service as a singleton
, you need to bind
it:
$container->bind('test', Test::class);
// Or
$container->bind(Test::class);
Calling them is the same as with the singleton and both are accessible with get
method:
$container->get('test');
$container->get(Test::class);
You can also use closures:
$container->singleton('test', function ($container) {
return $container->get(Test::class);
});
Suppose we have a class titled Test:
namespace HesamMousavi\FalconContainer;
class Test {}
Result of Calling This Class:
$container = \HesamMousavi\FalconContainer\FalconContainer::getInstance();
$container->singleton('test', \HesamMousavi\FalconContainer\Test::class);
//or
//$container->bind('test', \HesamMousavi\FalconContainer\Test::class);
dump($container->get('test'));
dump($container->get('test'));
Output in Bind
Mode:
HesamMousavi\FalconContainer\Test {#6}
HesamMousavi\FalconContainer\Test {#13}
Output in Singleton
Mode:
HesamMousavi\FalconContainer\Test {#6}
HesamMousavi\FalconContainer\Test {#6}
If the class is not bound and called directly via the get
method, it will be automatically
resolved using reflection. This means that the autowire
mode is always active
, and if a bind(or singleton) is defined for a service, it uses it; otherwise, it resolves it automatically.
You can also use service providers. To do this, you need to create a class that extends ServiceProvider
. You can have a directory called providers and place your providers there.
namespace HesamMousavi\FalconContainer\Providers;
use HesamMousavi\FalconContainer\FalconServiceProvider;
use HesamMousavi\FalconContainer\Test;
class TestServiceProvider extends FalconServiceProvider {
public function register() {
$this->container->singleton('test', Test::class);
}
public function boot() {}
}
Each provider consists of two methods: register
and boot
. As you know, first the register method of all providers is executed, then the boot method is called.
Then in a file, for example named providers.php, you should include the path to the created provider.
<?php
return [
\HesamMousavi\FalconContainer\Providers\TestServiceProvider::class,
];
After creating an instance of the container, you can run all providers by calling the runProviders method and passing the path to the providers.php file.
$container = \HesamMousavi\FalconContainer\FalconContainer::getInstance();
$container->runProviders(__DIR__.'/bootstrap/providers.php');
These steps help you use service providers in your Falcon Container plugin, adding more capabilities to your project. 🚀