Skip to content

Commit

Permalink
Add Dependency Injection example to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 25, 2023
1 parent 546a4f9 commit 6634791
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/Phug/DependencyInjection/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

Phug Formatter
========
Phug Dependency Injection
=========================

What is Phug Dependency Injection?
-----------------
----------------------------------

This project allow to provide helpers functions and values,
to require them and to dump all required dependencies as
Expand All @@ -20,3 +20,31 @@ composer require phug/dependency-injection

Usage
-----

```php
use Phug\DependencyInjection;

$dependencies = new DependencyInjection();
$dependencies->register('limit', 42);
$dependencies->provider('clock', static function () {
return new Clock();
});

$dependencies->provider('expiration', ['clock', 'limit', static function (ClockInterface $clock, $limit) {
return static function ($margin) use ($clock, $limit) {
$delta = $limit - $margin;

return $clock->now()->modify("$delta days");
};
}]);

$expiration = $dependencies->call('expiration'); // return new DateTimeImmutable('now + 42 days')
$expiration = $dependencies->call('expiration', 20); // return new DateTimeImmutable('now + 22 days')
```

Security contact information
----------------------------

To report a security vulnerability, please use the
[Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.
92 changes: 92 additions & 0 deletions tests/Phug/DependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Phug\Test;

use DateTimeImmutable;
use Phug\DependencyException;
use Phug\DependencyInjection;
use Phug\Test\Utils\Clock;
use Phug\Test\Utils\ClockInterface;
use Phug\Util\UnorderedArguments;

class DependencyInjectionTest extends AbstractDependencyInjectionTest
Expand Down Expand Up @@ -383,4 +386,93 @@ public function testRecursion()

self::assertSame("+foo=\n+--bar=42", $result);
}

/**
* @covers ::__construct
* @covers ::register
* @covers ::provider
* @covers ::getProvider
* @covers ::has
* @covers ::get
* @covers ::set
* @covers ::setRequired
* @covers ::isRequired
* @covers ::setAsRequired
* @covers ::call
* @covers \Phug\DependencyInjection\Requirement::__construct
* @covers \Phug\DependencyInjection\Requirement::isRequired
* @covers \Phug\DependencyInjection\Requirement::setRequired
* @covers \Phug\DependencyInjection\Requirement::getDependency
* @covers \Phug\DependencyInjection\Requirement::setDependency
* @covers \Phug\DependencyInjection\Requirement::setDependency
* @covers \Phug\DependencyInjection\Dependency::__construct
* @covers \Phug\DependencyInjection\Dependency::setDependencies
* @covers \Phug\DependencyInjection\Dependency::getDependencies
*/
public function testLongScenario()
{
$dependencies = new DependencyInjection();

$dependencies->register('limit', 42);

self::assertTrue($dependencies->has('limit'));
self::assertFalse($dependencies->has('clock'));

$message = null;

try {
$dependencies->getProvider('clock');
} catch (DependencyException $exception) {
$message = $exception->getMessage();
}

self::assertSame('clock dependency not found.', $message);

$createClock = static function () {
return new Clock();
};

$dependencies->provider('clock', $createClock);

$requirement = $dependencies->getProvider('clock');

self::assertInstanceOf('Phug\\DependencyInjection\\Requirement', $requirement);
self::assertFalse($requirement->isRequired());
self::assertFalse($dependencies->isRequired('clock'));

$requirement->setRequired(true);

self::assertTrue($requirement->isRequired());
self::assertTrue($dependencies->isRequired('clock'));

$dependency = $requirement->getDependency();
self::assertInstanceOf('Phug\\DependencyInjection\\Dependency', $dependency);
self::assertSame(array(), $dependency->getDependencies());
self::assertSame('clock', $dependency->getName());
self::assertSame($createClock, $dependency->getValue());

self::assertSame(42, $dependencies->get('limit'));

$dependencies->provider('expiration', ['clock', 'limit', static function (ClockInterface $clock, $limit) {
return static function ($margin = 0) use ($clock, $limit) {
$delta = $limit - $margin;

return $clock->now()->modify("$delta days");
};
}]);

$before = new DateTimeImmutable('now + 42 days');
$expiration = $dependencies->call('expiration');
$after = new DateTimeImmutable('now + 42 days');

self::assertLessThan($after, $expiration);
self::assertGreaterThan($before, $expiration);

$before = new DateTimeImmutable('now + 22 days');
$expiration = $dependencies->call('expiration', 20);
$after = new DateTimeImmutable('now + 22 days');

self::assertLessThan($after, $expiration);
self::assertGreaterThan($before, $expiration);
}
}
13 changes: 13 additions & 0 deletions tests/Phug/Utils/Clock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Phug\Test\Utils;

use DateTimeImmutable;

class Clock implements ClockInterface
{
public function now()
{
return new DateTimeImmutable('now');
}
}
11 changes: 11 additions & 0 deletions tests/Phug/Utils/ClockInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Phug\Test\Utils;

use DateTimeImmutable;

interface ClockInterface
{
/** @return DateTimeImmutable */
public function now();
}

0 comments on commit 6634791

Please sign in to comment.