Skip to content

Commit

Permalink
Support providers user defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Mar 28, 2023
1 parent cc9a6b6 commit fc2a4fd
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 25 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=8.0",
"ext-fileinfo": "*",
"ext-openssl": "*",
"ext-simplexml": "*",
Expand All @@ -32,7 +32,8 @@
"mockery/mockery": "^1.2.3",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^4.3"
"symfony/var-dumper": "^4.3",
"symfony/event-dispatcher": "^6.2"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 11 additions & 4 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/
namespace AdMarketingAPI;

use AdMarketingAPI\OceanEngine\Application;

/**
* Class Factory.
*
* @method static \AdMarketingAPI\OceanEngine\Application oceanengine(array $config)
* @method static \AdMarketingAPI\OceanEngine\Application oceanEngine(array $config, array $providers = [])
*/
class Factory
{
Expand All @@ -29,13 +31,18 @@ public static function __callStatic($name, $arguments)
/**
* @param string $name
*
* @return \EasyWeChat\Kernel\ServiceContainer
* @return \AdMarketingAPI\Kernel\ServiceContainer
*/
public static function make($name, array $config)
public static function make($name, array $config, array $providers = [])
{
$namespace = Kernel\Supports\Str::studly($name);
$application = "\\AdMarketingAPI\\{$namespace}\\Application";

return new $application($config);
return new $application($config, providers: $providers);
}

public static function makeOceanEngine(array $config, array $providers = []): Application
{
return new Application($config, providers: $providers);
}
}
4 changes: 2 additions & 2 deletions src/Gdt/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class Application extends ServiceContainer
Dmp\ServiceProvider::class,
];

public function __construct(array $config = [], array $prepends = [])
public function __construct(array $config = [], array $prepends = [], array $providers = [])
{
if (isset($config['mode']) && $config['mode'] == self::MODE_DEV) {
$config['http']['base_uri'] = self::URL[self::MODE_DEV];
} else {
$config['http']['base_uri'] = self::URL[self::MODE_NORMAL];
}

parent::__construct($config, $prepends);
parent::__construct($config, $prepends, providers: $providers);
}
}
2 changes: 1 addition & 1 deletion src/Kernel/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getToken(bool $refresh = false): array
// cache access token and refresh token
$this->setToken($token);

$this->app->events->dispatch(new Events\AccessTokenRefreshed($this));
$this->app->events?->dispatch(new Events\AccessTokenRefreshed($this));

return $token;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function request(string $url, string $method = 'GET', array $options = []

$response = $this->performRequest($url, $method, $options);

$this->app->events->dispatch(new Events\HttpResponseCreated($response));
$this->app->events?->dispatch(new Events\HttpResponseCreated($response));

return $this->proccessApiResult($url, $response);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Kernel/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,10 @@ protected function createSyslogDriver(array $config)
return new Monolog($this->parseChannel($config), [
$this->prepareHandler(
new SyslogHandler(
'AdMarketingAPI',
$config['facility'] ?? LOG_USER,
$this->level($config)
)
'AdMarketingAPI',
$config['facility'] ?? LOG_USER,
$this->level($config)
)
),
]);
}
Expand All @@ -477,9 +477,9 @@ protected function createErrorlogDriver(array $config)
return new Monolog($this->parseChannel($config), [
$this->prepareHandler(
new ErrorLogHandler(
$config['type'] ?? ErrorLogHandler::OPERATING_SYSTEM,
$this->level($config)
)
$config['type'] ?? ErrorLogHandler::OPERATING_SYSTEM,
$this->level($config)
)
),
]);
}
Expand Down
13 changes: 10 additions & 3 deletions src/Kernel/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
namespace AdMarketingAPI\Kernel;

use Pimple\Container;
use Psr\EventDispatcher\EventDispatcherInterface;

/**
* @property ?EventDispatcherInterface $events
*/
class ServiceContainer extends Container
{
/**
Expand Down Expand Up @@ -44,7 +48,7 @@ public function __construct(array $config = [], array $prepends = [], string $id

$this->id = $id;

$this->events->dispatch(new Events\ApplicationInitialized($this));
$this->events?->dispatch(new Events\ApplicationInitialized($this));
}

/**
Expand All @@ -56,7 +60,11 @@ public function __construct(array $config = [], array $prepends = [], string $id
*/
public function __get($id)
{
return $this->offsetGet($id);
if ($this->offsetExists($id)) {
return $this->offsetGet($id);
}

return null;
}

/**
Expand Down Expand Up @@ -105,7 +113,6 @@ public function getProviders()
Providers\LogServiceProvider::class,
Providers\RequestServiceProvider::class,
Providers\HttpClientServiceProvider::class,
Providers\EventDispatcherServiceProvider::class,
], $this->providers);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Kernel/Supports/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function all()
/**
* Return specific items.
*
* @return \EasyWeChat\Kernel\Support\Collection
* @return \AdMarketingAPI\Kernel\Support\Collection
*/
public function only(array $keys)
{
Expand Down Expand Up @@ -149,7 +149,7 @@ public function except($keys)
*
* @param array|Collection $items
*
* @return \EasyWeChat\Kernel\Support\Collection
* @return \AdMarketingAPI\Kernel\Support\Collection
*/
public function merge($items)
{
Expand Down
4 changes: 2 additions & 2 deletions src/OceanEngine/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class Application extends ServiceContainer
// DPA\ServiceProvider::class,
];

public function __construct(array $config = [], array $prepends = [])
public function __construct(array $config = [], array $prepends = [], array $providers = [])
{
if (isset($config['mode']) && $config['mode'] == self::MODE_DEV) {
$config['http']['base_uri'] = self::URL[self::MODE_DEV];
} else {
$config['http']['base_uri'] = self::URL[self::MODE_NORMAL];
}

parent::__construct($config, $prepends);
parent::__construct($config, $prepends, providers: $providers);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestCase extends BaseTestCase
/**
* Tear down the test case.
*/
public function tearDown()
public function tearDown(): void
{
$this->finish();
parent::tearDown();
Expand Down

0 comments on commit fc2a4fd

Please sign in to comment.