Skip to content

Commit f783979

Browse files
authored
Merge pull request #18 from hyperf/feature-dependiences
Fixed custom dependencies do not work.
2 parents 2ee74d1 + adbdb3a commit f783979

File tree

4 files changed

+73
-23
lines changed

4 files changed

+73
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# v1.0.3 - TBD
22

3+
## Fixed
34

5+
- [#18](https://github.com/hyperf/nano/pull/18) Fixed custom dependencies do not work.

example/index.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,22 @@
1919

2020
require_once __DIR__ . '/../vendor/autoload.php';
2121

22-
class Foo
22+
interface FooInterface
2323
{
24-
public function bar()
24+
public function bar(): string;
25+
}
26+
27+
class Foo implements FooInterface
28+
{
29+
public function bar(): string
2530
{
2631
return 'bar';
2732
}
2833
}
2934

30-
$app = AppFactory::createBase();
35+
$app = AppFactory::createBase('0.0.0.0', 9501, [
36+
FooInterface::class => Foo::class,
37+
]);
3138
$app->config([
3239
'server' => [
3340
'settings' => [
@@ -60,6 +67,11 @@ public function bar()
6067
return $foo->bar();
6168
});
6269

70+
$app->get('/foo', function () {
71+
/* @var ContainerProxy $this */
72+
return $this->get(FooInterface::class)->bar();
73+
});
74+
6375
$app->get('/middleware', function () {
6476
return $this->request->getAttribute('key');
6577
});

src/Factory/AppFactory.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class AppFactory
3232
/**
3333
* Create an application.
3434
*/
35-
public static function create(string $host = '0.0.0.0', int $port = 9501): App
35+
public static function create(string $host = '0.0.0.0', int $port = 9501, array $dependencies = []): App
3636
{
37-
$app = self::createApp();
37+
$app = self::createApp($dependencies);
3838
$app->config([
3939
'server' => Preset::default(),
4040
'server.servers.0.host' => $host,
@@ -47,9 +47,9 @@ public static function create(string $host = '0.0.0.0', int $port = 9501): App
4747
/**
4848
* Create a single worker application in base mode, with max_requests = 0.
4949
*/
50-
public static function createBase(string $host = '0.0.0.0', int $port = 9501): App
50+
public static function createBase(string $host = '0.0.0.0', int $port = 9501, array $dependencies = []): App
5151
{
52-
$app = self::createApp();
52+
$app = self::createApp($dependencies);
5353
$app->config([
5454
'server' => Preset::base(),
5555
'server.servers.0.host' => $host,
@@ -59,7 +59,21 @@ public static function createBase(string $host = '0.0.0.0', int $port = 9501): A
5959
return $app;
6060
}
6161

62-
protected static function prepareContainer(): ContainerInterface
62+
/**
63+
* Create an application with a chosen preset.
64+
*/
65+
public static function createApp(array $dependencies = []): App
66+
{
67+
// Setting ini and flags
68+
self::prepareFlags();
69+
70+
// Prepare container
71+
$container = self::prepareContainer($dependencies);
72+
73+
return new App($container);
74+
}
75+
76+
protected static function prepareContainer(array $dependencies = []): ContainerInterface
6377
{
6478
$config = new Config(ProviderConfig::load());
6579
$config->set(StdoutLoggerInterface::class, [
@@ -74,7 +88,8 @@ protected static function prepareContainer(): ContainerInterface
7488
LogLevel::WARNING,
7589
],
7690
]);
77-
$container = new Container(new DefinitionSource($config->get('dependencies')));
91+
$dependencies = array_merge($config->get('dependencies', []), $dependencies);
92+
$container = new Container(new DefinitionSource($dependencies));
7893
$container->set(ConfigInterface::class, $config);
7994
$container->define(DispatcherFactory::class, DispatcherFactory::class);
8095
$container->define(BoundInterface::class, ContainerProxy::class);
@@ -96,18 +111,4 @@ protected static function prepareFlags(int $hookFlags = SWOOLE_HOOK_ALL)
96111
! defined('BASE_PATH') && define('BASE_PATH', $projectRootPath);
97112
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', $hookFlags);
98113
}
99-
100-
/**
101-
* Create an application with a chosen preset.
102-
*/
103-
protected static function createApp(): App
104-
{
105-
// Setting ini and flags
106-
self::prepareFlags();
107-
108-
// Prepare container
109-
$container = self::prepareContainer();
110-
111-
return new App($container);
112-
}
113114
}

tests/Cases/Http/ContainerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf Nano.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://nano.hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/nano/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Nano\Cases\Http;
13+
14+
use HyperfTest\Nano\HttpTestCase;
15+
16+
/**
17+
* @internal
18+
* @coversNothing
19+
*/
20+
class ContainerTest extends HttpTestCase
21+
{
22+
public function testDi()
23+
{
24+
$response = $this->client()->get('/di');
25+
$this->assertSame(200, $response->getStatusCode());
26+
$this->assertSame('bar', $response->getBody()->getContents());
27+
}
28+
29+
public function testDependencies()
30+
{
31+
$response = $this->client()->get('/foo');
32+
$this->assertSame(200, $response->getStatusCode());
33+
$this->assertSame('bar', $response->getBody()->getContents());
34+
}
35+
}

0 commit comments

Comments
 (0)