Skip to content

Commit

Permalink
Merge pull request #13 from ytake/feature/patch-hhvm3.28
Browse files Browse the repository at this point in the history
Feature/patch hhvm3.28
  • Loading branch information
ytake authored Oct 7, 2018
2 parents cfdffd4 + 51048fb commit 6381eb5
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 240 deletions.
3 changes: 3 additions & 0 deletions .hhconfig
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
assume_php=false
safe_array = true
safe_vector_array = true
unsafe_rx = false
4 changes: 2 additions & 2 deletions .travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ hhvm --version
curl https://getcomposer.org/installer | hhvm -d hhvm.jit=0 --php -- /dev/stdin --install-dir=/usr/local/bin --filename=composer

cd /var/source
hhvm -d hhvm.php7.all=1 -d hhvm.jit=0 -d hhvm.hack.lang.auto_typecheck=0 /usr/local/bin/composer install
hhvm -d hhvm.jit=0 -d hhvm.hack.lang.auto_typecheck=0 /usr/local/bin/composer install

hhvm -d hhvm.php7.all=1 -d hhvm.jit=0 -d hhvm.hack.lang.auto_typecheck=0 vendor/bin/phpunit
hhvm -d hhvm.jit=0 -d hhvm.hack.lang.auto_typecheck=0 vendor/bin/hacktest tests/
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ services:
- docker
env:
matrix:
- HHVM_VERSION=3.24-lts-latest
- HHVM_VERSION=3.25.3
- HHVM_VERSION=3.26.0
- HHVM_VERSION=3.28.0
- HHVM_VERSION=3.28.1
- HHVM_VERSION=3.28.2
- HHVM_VERSION=3.28.3
- HHVM_VERSION=latest
- HHVM_VERSION=nightly
install:
Expand Down
33 changes: 15 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,24 @@ $container->set('scope:prototype', $container ==> new \stdClass(), \Ytake\HHCont
## Dependency Injection

### set parameters

```hack
$container->parameters(
'string className',
'parameter name',
$container ==> 'parameter value'
);
```

sample class

```hack
final class MessageClass {
public function __construct(protected string $message) {
}
public function __construct(
protected string $message
) {}
public function message(): string {
return $this->message;
}
}
final class MessageClient {
public function __construct(protected MessageClass $message) {
public function __construct(
protected MessageClass $message
) {}
}
public function message(): MessageClass {
return $this->message;
}
Expand All @@ -80,7 +74,11 @@ final class MessageClient {
```hack
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('message.class', $container ==> new MessageClass('testing'));
$container->parameters(MessageClient::class, 'message', $container ==> $container->get('message.class'));
$container->set(MessageClient::class, $container ==> {
$instance = $container->get('message.class');
invariant($instance instanceof MockMessageClass, 'error');
new MessageClient($instance);
});
$instance = $container->get(MessageClient::class);
```

Expand Down Expand Up @@ -112,10 +110,9 @@ $container->set(TestingInvokable::class, $container ==>
use Ytake\HHContainer\ServiceModule;
use Ytake\HHContainer\FactoryContainer;
class ExampleModule extends ServiceModule
{
public function provide(FactoryContainer $container): void
{
class ExampleModule extends ServiceModule {
public function provide(FactoryContainer $container): void {
$container->set('example', $container ==> new \stdClass());
}
}
Expand Down
15 changes: 6 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"container"
],
"require": {
"hhvm": ">=3.24",
"hhvm": ">=3.28",
"hhvm/hhvm-autoload": "^1.6",
"hhvm/hsl": "^3.28",
"ytake/psr-container-hhi": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0",
"91carriage/phpunit-hhi": "^5.7.1",
"hhvm/hsl": "^1.0.0|^3.26.0",
"hhvm/hhast": "^1.0.1|>=3.26.0"
"hhvm/hacktest": "^1.0",
"facebook/fbexpect": "^2.1.3",
"hhvm/hhast": ">=3.28.0"
},
"autoload": {
"psr-4": {
Expand All @@ -28,9 +28,6 @@
"autoload-dev": {
"classmap": [
"tests/"
]
},
"scripts": {
"test": "./vendor/bin/phpunit"
]
}
}
5 changes: 1 addition & 4 deletions hhast-lint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@
"roots": [
"src/",
"tests/"
],
"namespaceAliases": {
"HHAST": "Facebook\\HHAST\\Linters"
}
]
}
23 changes: 0 additions & 23 deletions phpunit.xml

This file was deleted.

2 changes: 1 addition & 1 deletion src/ContainerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

use type Psr\Container\ContainerExceptionInterface;

class ContainerException extends \Exception
final class ContainerException extends \Exception
implements ContainerExceptionInterface {}
97 changes: 15 additions & 82 deletions src/FactoryContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@

use type Psr\Container\ContainerInterface;

enum Scope : int {
PROTOTYPE = 0;
SINGLETON = 1;
}

type TServiceModule = classname<ServiceModule>;
type TCallable = (function(FactoryContainer): mixed);

Expand All @@ -39,38 +34,19 @@ enum Scope : int {
*/
class FactoryContainer implements ContainerInterface {

protected Vector<TServiceModule> $modules = Vector {};

protected array<string, array<string, TCallable>>
$parameters = [];

protected bool $locked = false;

protected Map<string, Map<Scope, TCallable>> $mapper = Map{};

public function set(
string $id,
TCallable $callback,
Scope $scope = Scope::PROTOTYPE,
): void {
if (!$this->locked) {
$this->mapper->add(Pair {$id, Map{$scope => $callback}});
}
}

public function parameters(
string $id,
string $name,
TCallable $callback,
): void {
if (!$this->locked) {
$this->parameters[$id][$name] = $callback;
}
$this->mapper->add(Pair {$id, Map{$scope => $callback}});
}

public function get($id): mixed {
if ($this->has($id)) {
$resolved = $this->mapper->get($id);
$resolved = $this->mapper[$id];
if (!is_null($resolved)) {
if ($resolved->firstKey() === Scope::SINGLETON) {
return $this->shared($id);
Expand All @@ -81,86 +57,43 @@ public function get($id): mixed {
}
}
}
try {
$reflectionClass = new \ReflectionClass($id);
if ($reflectionClass->isInstantiable()) {
$arguments = Vector{};
$constructor = $reflectionClass->getConstructor();
if ($constructor instanceof \ReflectionMethod) {
$resolvedParameters = $this->resolveConstructorParameters($id, $constructor);
if ($resolvedParameters->count()) {
$arguments = $resolvedParameters;
}
}
return $reflectionClass->newInstanceArgs($arguments);
}
} catch (\ReflectionException $e) {
throw new NotFoundException(
sprintf('Identifier "%s" is not binding.', $id),
);
}
throw new ContainerException(sprintf('Error retrieving "%s"', $id));
throw new NotFoundException(
sprintf('Identifier "%s" is not binding.', $id),
);
}

<<__Memoize>>
protected function shared(string $id): mixed {
$shared = $this->mapper->at($id);
$call = $shared->firstValue();
$call = $this->mapper[$id]->firstValue();
if(!is_null($call)) {
return call_user_func($call, $this);
}
}

<<__Rx, __Mutable>>
public function has($id): bool {
return $this->mapper->containsKey($id);
return array_key_exists($id, $this->mapper);
}

<<__Rx>>
public function bindings(
): ImmMap<string, Map<Scope, TCallable>> {
return $this->mapper->toImmMap();
}

public function flush(): void {
$this->mapper->clear();
$this->locked = false;
}

public function remove(string $id): void {
if (!$this->locked) {
if ($this->has($id)) {
$this->mapper->removeKey($id);
}
}

public function registerModule(TServiceModule $moduleClassName): void {
if (!$this->locked) {
$this->modules->add($moduleClassName);
}
}

public function lockModule(): void {
foreach ($this->modules->getIterator() as $iterator) {
(new $iterator())->provide($this);
}
$this->locked = true;
public function flush(): void {
$this->mapper->clear();
}

protected function resolveConstructorParameters(
string $id,
\ReflectionMethod $constructor,
): Vector<mixed> {
$r = Vector{};
$parameters = $constructor->getParameters();
foreach ($parameters as $parameter) {
if (array_key_exists($id, $this->parameters)) {
if (array_key_exists($parameter->getName(), $this->parameters[$id])) {
$r->add(call_user_func(
$this->parameters[$id][$parameter->getName()],
$this,
));
}
}
}
return $r;
public function registerModule(TServiceModule $moduleClassName): void {
new $moduleClassName()
|> $$->provide($this);
}

public function callable(MethodCallIntreface $invokable): mixed {
Expand Down
2 changes: 2 additions & 0 deletions src/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ interface FactoryInterface {

public function provide(FactoryContainer $container): this::T;

<<__Rx>>
public function scope(): Scope;

<<__Rx>>
public function name(): string;
}
1 change: 1 addition & 0 deletions src/MethodCallIntreface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@

interface MethodCallIntreface {

<<__Rx>>
public function proceed(): mixed;
}
3 changes: 3 additions & 0 deletions src/MethodCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace Ytake\HHContainer;

use function is_object;

class MethodCaller implements MethodCallIntreface {

public function __construct(
Expand All @@ -30,6 +32,7 @@ public function __construct(
}
}

<<__Rx>>
public function proceed(): mixed {
$dynamicMethod = $this->invokeMethod;
return /* UNSAFE_EXPR */ $this->instance->$dynamicMethod(...$this->args);
Expand Down
2 changes: 1 addition & 1 deletion src/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

use type Psr\Container\NotFoundExceptionInterface;

class NotFoundException extends \Exception
final class NotFoundException extends \Exception
implements NotFoundExceptionInterface {}
8 changes: 8 additions & 0 deletions src/Scope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?hh // strict

namespace Ytake\HHContainer;

enum Scope : int {
PROTOTYPE = 0;
SINGLETON = 1;
}
Loading

0 comments on commit 6381eb5

Please sign in to comment.