Skip to content

Commit

Permalink
Add static calling.
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyoung committed Jun 8, 2017
1 parent 085ab94 commit 9d7e8ab
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 39 deletions.
93 changes: 54 additions & 39 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Doctrine\Common\Cache\FilesystemCache;
use EasyWeChat\Applications\Base\Core\Http;
use EasyWeChat\Config\Repository as Config;
use EasyWeChat\Exceptions\FaultException;
use EasyWeChat\Support\Log;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\NullHandler;
Expand All @@ -39,36 +40,10 @@
use Symfony\Component\HttpFoundation\Request;

/**
* Class Application.
*
* @property \EasyWeChat\Applications\OfficialAccount\Core\AccessToken $access_token
* @property \EasyWeChat\Applications\OfficialAccount\Server\Guard $server
* @property \EasyWeChat\Applications\OfficialAccount\User\User $user
* @property \EasyWeChat\Applications\OfficialAccount\User\Tag $user_tag
* @property \EasyWeChat\Applications\OfficialAccount\User\Group $user_group
* @property \EasyWeChat\Applications\OfficialAccount\Js\Js $js
* @property \Overtrue\Socialite\Providers\WeChatProvider $oauth
* @property \EasyWeChat\Applications\OfficialAccount\Menu\Menu $menu
* @property \EasyWeChat\Applications\OfficialAccount\TemplateMessage\TemplateMessage $template_message
* @property \EasyWeChat\Applications\OfficialAccount\Material\Material $material
* @property \EasyWeChat\Applications\OfficialAccount\Material\Temporary $material_temporary
* @property \EasyWeChat\Applications\OfficialAccount\CustomerService\CustomerService $customer_service
* @property \EasyWeChat\Applications\OfficialAccount\Url\Url $url
* @property \EasyWeChat\Applications\OfficialAccount\QRCode\QRCode $qrcode
* @property \EasyWeChat\Applications\OfficialAccount\Semantic\Semantic $semantic
* @property \EasyWeChat\Applications\OfficialAccount\Stats\Stats $stats
* @property \EasyWeChat\Applications\OfficialAccount\Payment\Merchant $merchant
* @property \EasyWeChat\Applications\OfficialAccount\Payment\Payment $payment
* @property \EasyWeChat\Applications\OfficialAccount\Payment\LuckyMoney\LuckyMoney $lucky_money
* @property \EasyWeChat\Applications\OfficialAccount\Payment\MerchantPay\MerchantPay $merchant_pay
* @property \EasyWeChat\Applications\OfficialAccount\Payment\CashCoupon\CashCoupon $cash_coupon
* @property \EasyWeChat\Applications\OfficialAccount\Reply\Reply $reply
* @property \EasyWeChat\Applications\OfficialAccount\Broadcast\Broadcast $broadcast
* @property \EasyWeChat\Applications\OfficialAccount\Card\Card $card
* @property \EasyWeChat\Applications\OfficialAccount\Device\Device $device
* @property \EasyWeChat\Applications\OfficialAccount\ShakeAround\ShakeAround $shakearound
* @property \EasyWeChat\Applications\OpenPlatform\OpenPlatform $open_platform
* @property \EasyWeChat\Applications\MiniProgram\MiniProgram $mini_program
* @method static \EasyWeChat\Applications\WeWork\WeWork wework(array $config)
* @method static \EasyWeChat\Applications\MiniProgram\MiniProgram miniProgram(array $config)
* @method static \EasyWeChat\Applications\OpenPlatform\OpenPlatform openPlatform(array $config)
* @method static \EasyWeChat\Applications\OfficialAccount\OfficialAccount officialAccount(array $config)
*/
class Application extends Container
{
Expand Down Expand Up @@ -121,6 +96,14 @@ class Application extends Container
\EasyWeChat\Applications\MiniProgram\Material\ServiceProvider::class,
\EasyWeChat\Applications\MiniProgram\CustomerService\ServiceProvider::class,
\EasyWeChat\Applications\MiniProgram\TemplateMessage\ServiceProvider::class,

/*
* WeWork Service Providers...
*/
\EasyWeChat\Applications\WeWork\Core\ServiceProvider::class,
\EasyWeChat\Applications\WeWork\Department\ServiceProvider::class,
\EasyWeChat\Applications\WeWork\Attendance\ServiceProvider::class,
\EasyWeChat\Applications\WeWork\Agent\ServiceProvider::class,
];

/**
Expand Down Expand Up @@ -283,21 +266,53 @@ private function initializeLogger()
}

/**
* Magic call.
* Get the instance from the container.
*
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public static function make($name, array $arguments)
{
return (new self($arguments))->offsetGet(
self::applicationInstanceKey($name)
);
}

/**
* Get the application instance key.
*
* @param string $method
* @param array $args
* @param string $name
*
* @return mixed
*
* @throws \Exception
* @throws \EasyWeChat\Exceptions\FaultException
*/
public function __call($method, $args)
protected static function applicationInstanceKey($name)
{
if (is_callable([$this['fundamental.api'], $method])) {
return call_user_func_array([$this['fundamental.api'], $method], $args);
}
$applications = [
'wework' => 'we_work.instance',
'miniProgram' => 'mini_program.instance',
'openPlatform' => 'open_platform.instance',
'officialAccount' => 'official_account.instance',
];

return $applications[$name] ?? (function () use ($name) {
throw new FaultException("No application named '{$name}'");
})();
}

throw new \Exception("Call to undefined method {$method}()");
/**
* Dynamically pass methods to the application.
*
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public static function __callStatic($name, $arguments)
{
return self::make($name, ...$arguments);
}
}
4 changes: 4 additions & 0 deletions src/Support/Traits/PrefixedContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public function fetch($key, callable $callable = null)
*/
public function __get($key)
{
if ($this->container->offsetExists($key)) {
return $this->container->offsetGet($key);
}

$className = basename(str_replace('\\', '/', static::class));

$name = Str::snake($className).'.'.$key;
Expand Down
44 changes: 44 additions & 0 deletions tests/Foundation/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,48 @@ public function testSetCustomAccessToken()

$this->assertSame('iamtokenhere', $app['official_account.access_token']->getToken());
}

public function testStaticCall()
{
$weworkInstances = [
Application::wework(['client_id' => 'corpid@123', 'client_secret' => 'corpsecret@123', 'debug' => true]),
Application::make('wework', ['debug' => true, 'client_id' => 'corpid@123', 'client_secret' => 'corpsecret@123']),
];
foreach ($weworkInstances as $instance) {
$this->assertInstanceOf('EasyWeChat\Applications\WeWork\WeWork', $instance);
$expected = [
'debug' => true,
'client_id' => 'corpid@123',
'client_secret' => 'corpsecret@123',
];
$this->assertArraySubset($expected, $instance->fetch('config')->all());
}

$officialAccountInstances = [
Application::officialAccount(['appid' => 'appid@456']),
Application::make('officialAccount', ['appid' => 'appid@456']),
];
foreach ($officialAccountInstances as $instance) {
$this->assertInstanceOf('EasyWeChat\Applications\OfficialAccount\OfficialAccount', $instance);
$this->assertArraySubset(['appid' => 'appid@456'], $instance->fetch('config')->all());
}

$openPlatformInstances = [
Application::openPlatform(['appid' => 'appid@789']),
Application::make('openPlatform', ['appid' => 'appid@789']),
];
foreach ($openPlatformInstances as $instance) {
$this->assertInstanceOf('EasyWeChat\Applications\OpenPlatform\OpenPlatform', $instance);
$this->assertArraySubset(['appid' => 'appid@789'], $instance->fetch('config')->all());
}

$miniProgramInstances = [
Application::miniProgram(['appid' => 'appid@890']),
Application::make('miniProgram', ['appid' => 'appid@890']),
];
foreach ($miniProgramInstances as $instance) {
$this->assertInstanceOf('EasyWeChat\Applications\MiniProgram\MiniProgram', $instance);
$this->assertArraySubset(['appid' => 'appid@890'], $instance->fetch('config')->all());
}
}
}

0 comments on commit 9d7e8ab

Please sign in to comment.