Skip to content

Commit

Permalink
自定义组件支持别名配置 (swoft-cloud/swoft-component#181)
Browse files Browse the repository at this point in the history
* 增加自定义组件支持别名的单测

* 让组件地址支持别名
  • Loading branch information
limingxinleo authored and huangzhhui committed Aug 15, 2018
1 parent 8ad44b7 commit f9256a5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"autoload-dev": {
"psr-4": {
"SwoftTest\\": "test/Cases"
"SwoftTest\\": "test/Cases",
"SwoftTest\\Testing\\": "test/Testing/"
}
},
"repositories": {
Expand Down
2 changes: 2 additions & 0 deletions src/Bean/Resource/CustomComponentsRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function registerServerNamespace()
}

$this->componentNamespaces[] = $ns;
$componentDir = alias($componentDir);

foreach ($this->serverScan as $dir) {
$scanDir = $componentDir . DS . $dir;
Expand Down Expand Up @@ -48,6 +49,7 @@ public function registerWorkerNamespace()
}

$this->componentNamespaces[] = $ns;
$componentDir = alias($componentDir);

if (!is_dir($componentDir)) {
continue;
Expand Down
10 changes: 10 additions & 0 deletions test/Cases/BeanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Swoft\Proxy\Proxy;
use SwoftTest\Bean\ProxyTest;
use SwoftTest\Bean\TestHandler;
use SwoftTest\Testing\Bean\Config;

/**
* Class BeanTest
Expand Down Expand Up @@ -52,4 +53,13 @@ public function testCustomComponentNamespaces()
$namespace = $resource->getComponentNamespaces();
$this->assertNotFalse(array_search('SwoftTest', $namespace));
}

public function testCustomComponentSupportAlias()
{
$config = bean(Config::class);
$this->assertEquals('test', $config->getName());

$config = bean(\SwoftTest\Testing\Bean2\Config::class);
$this->assertEquals('test', $config->getName());
}
}
27 changes: 27 additions & 0 deletions test/Testing/Bean/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace SwoftTest\Testing\Bean;

use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Value;

/**
* Class Config
* @Bean
* @package SwoftTest\Testing\Bean
*/
class Config
{
/**
* @Value(env="${TEST_NAME}")
* @var string
*/
public $name = 'fail';

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
27 changes: 27 additions & 0 deletions test/Testing/Bean2/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace SwoftTest\Testing\Bean2;

use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Value;

/**
* Class Config
* @Bean
* @package SwoftTest\Testing\Bean
*/
class Config
{
/**
* @Value(env="${TEST_NAME}")
* @var string
*/
public $name = 'fail';

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
4 changes: 3 additions & 1 deletion test/config/properties/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
'env' => 'Base',
'components' => [
'custom' => [
'SwoftTest'
'SwoftTest',
'SwoftTest\\Testing\\Bean' => BASE_PATH . '/Testing/Bean',
'SwoftTest\\Testing\\Bean2' => '@root/Testing/Bean2',
]
],
'provider' => require __DIR__ . DS . 'provider.php',
Expand Down

0 comments on commit f9256a5

Please sign in to comment.