1. Go to this page and download the library: Download stellarwp/container library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stellarwp / container example snippets
namespace Acme\SomePlugin;
class SomeModule extends Module
{
/**
* @var Settings
*/
protected $settings;
/**
* @param Settings $settings
*/
public function __construct(Settings $settings)
{
$this->settings = $settings;
}
}
namespace Acme\SomePlugin;
class SomeModule extends Module
{
/**
* @var Settings
*/
protected $settings;
public function __construct()
{
$this->settings = new Settings();
}
}
namespace Acme\SomePlugin;
use StellarWP\Container\Container as BaseContainer;
class Container extends BaseContainer
{
/**
* Retrieve a mapping of identifiers to callables.
*
* When an identifier is requested through the container, the container will find the given
* dependency in this array, execute the callable, and return the result.
*
* @return Array<string,callable> A mapping of identifiers to callables.
*/
public function config()
{
return [
// ...
];
}
}
namespace Acme\SomePlugin;
class PBandJ implements SandwichInterface
{
public function __construct(Bread $bread,PeanutButter $pb, Jelly $jelly)
{
// ...
}
}
use Acme\SomePlugin\Bread;
use Acme\SomePlugin\Jelly;
use Acme\SomePlugin\PeanutButter;
use Acme\SomePlugin\SandwichInterface;
public function config()
{
return [
Bread::class => null,
Jelly::class => null,
PeanutButter::class => null,
// In order to construct a PBandJ, we need both PeanutButter and Jelly.
SandwichInterface::class => function ($container) {
return new PBandJ(
$container->make(Bread::class),
$container->make(PeanutButter::class),
$container->make(Jelly::class)
);
},
];
}
$sandwich = (new Container())->get(SandwichInterface::class);
var_dump($sandwich instanceof PBandJ);
# => bool(true)
use Acme\SomePlugin\Container;
Container::getInstance()->get(SomeAbstract::class);
use Acme\SomePlugin\Container;
$container = new Container();
// Elsewhere.
$singleton = Container::getInstance();
var_dump($singleton === $container);
# => bool(false)
use Acme\SomePlugin\Container;
$container = new Container();
// Elsewhere.
$singleton = Container::getInstance($container);
var_dump($singleton === $container);
# => bool(true)
use Acme\SomePlugin\UserController;
use Vendor\Package\Response;
use Vendor\Package\Sdk as ServiceSdk;
/**
* @test
*/
public function saveUser_should_update_the_account_email()
{
$user_id = $this->factory()->user->create([
'email' => '[email protected]',
]);
/*
* Expect that our code will end up calling ServiceSdk::patch() once with the given args and will
* return a Response object with status code 200.
*
* @link https://phpunit.readthedocs.io/en/9.5/test-doubles.html
*/
$service = $this->createMock(ServiceSdk::class);
$service->expects($this->once())
->method('patch')
->withArgs('/users/' . $user_id, ['email' => '[email protected]'])
->willReturn(new Response(200));
// Replace the default ServiceSdk instance with our mock.
$this->container->extend(ServiceSdk::class, function () use ($service) {
return $service;
});
$this->container->get(UserController::class)->update([
'user' => $user_id,
'email' => '[email protected]',
]);
}
diff
// Replace the default ServiceSdk instance with our mock.
- $this->container->extend(ServiceSdk::class, function () use ($service) {
- return $service;
- });
+ $this->container->extend(ServiceSdk::class, $service);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.