Skip to content

Commit

Permalink
Merge branch 'main' of personal:youyingxiang/laravel-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
youyingxiang committed Jan 15, 2022
2 parents ace4502 + 21ac310 commit ac04294
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 45 deletions.
6 changes: 1 addition & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// 插件市场 api 域名
'api_base' => 'http://plugin.you-tang.com/',
// 插件市场默认调用的 client class
'default' => \Yxx\LaravelPlugin\Support\Client\Market::class
'default' => \Yxx\LaravelPlugin\Support\Client\Market::class,
],

'stubs' => [
Expand Down Expand Up @@ -39,7 +39,6 @@
],
'paths' => [


'plugins' => base_path('plugins'),

// 资源发布目录
Expand Down Expand Up @@ -67,11 +66,9 @@
],
],


// 自定义命令
'commands' => [],


'cache' => [
'enabled' => false,
'key' => 'laravel-plugin',
Expand All @@ -93,5 +90,4 @@

'activator' => 'file',


];
25 changes: 12 additions & 13 deletions src/Console/Commands/DownLoadCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yxx\LaravelPlugin\Console\Commands;

use Illuminate\Console\Command;
Expand All @@ -15,31 +16,30 @@ class DownLoadCommand extends Command

protected $name = 'plugin:download';


protected $description = 'Download plugin from server to local.';


public function handle(): int
{
$path = Str::uuid() . ".zip";
$path = Str::uuid().'.zip';
try {
$plugins = data_get(app('plugins.client')->plugins(1), 'data');
$rows = array_reduce($plugins,function($rows,$item){
$rows = array_reduce($plugins, function ($rows, $item) {
$rows[] = [
count($rows),
$item['name'],
$item['author'],
$item['download_times'],
];

return $rows;
},[]);
}, []);
$this->comment(__('plugins.plugin_list'));

$this->table([
__('plugins.serial_number'),
__('plugins.name'),
__('plugins.author'),
__('plugins.download_times')
__('plugins.download_times'),
], $rows);

$sn = $this->ask(__('plugins.input_sn'));
Expand All @@ -48,14 +48,14 @@ public function handle(): int
throw new \InvalidArgumentException(__('plugins.sn_not_exist'));
}

$versions = array_map(fn($version) => [
$versions = array_map(fn ($version) => [
$version['id'],
$version['version'],
$version['description'],
$version['download_times'],
$version['status_str'],
$version['price'],
], data_get($plugin, 'versions'));
], data_get($plugin, 'versions'));

$this->comment(__('plugins.version_list'));

Expand All @@ -70,24 +70,23 @@ public function handle(): int

$versionId = $this->ask(__('plugins.input_version_id'));


if (! in_array($versionId, Arr::pluck($plugin['versions'], 'id'))) {
throw new \InvalidArgumentException(__('plugins.version_not_exist'));
}

Storage::put($path, app('plugins.client')->download($versionId));

Artisan::call("plugin:install", ["path" => Storage::path($path)]);
Artisan::call('plugin:install', ['path' => Storage::path($path)]);

$this->info(__('plugins.download_successful'));

} catch (\Exception $exception) {
$this->error($exception->getMessage());

return E_ERROR;
} finally {
Storage::delete($path);
Storage::delete($path);
}

return 0;
}
}
}
4 changes: 2 additions & 2 deletions src/Console/Commands/RegisterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function handle(): int
$account = $this->ask('Account');
$password = $this->secret('Password');
if (Str::length($password) < 8) {
throw new \InvalidArgumentException("The password must be at least 8 characters.");
throw new \InvalidArgumentException('The password must be at least 8 characters.');
}
$passwordConfirmation = $this->secret('Confirmation Password');

if ($passwordConfirmation !== $password) {
throw new \InvalidArgumentException("The password confirmation does not match.");
throw new \InvalidArgumentException('The password confirmation does not match.');
}

$result = app('plugins.client')->register(
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/UploadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handle(): int
]);
} catch (\Exception $exception) {
$this->line('');
$this->error('Plugin upload failed : ' . $exception->getMessage());
$this->error('Plugin upload failed : '.$exception->getMessage());

return E_ERROR;
}
Expand Down
16 changes: 7 additions & 9 deletions src/Contracts/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Yxx\LaravelPlugin\Contracts;

use Psr\Http\Message\StreamInterface;

interface ClientInterface
{
/**
* 用户登录
* 用户登录.
*
* @param string $account
* @param string $password
Expand All @@ -15,7 +16,7 @@ interface ClientInterface
public function login(string $account, string $password): array;

/**
* 用户注册
* 用户注册.
*
* @param string $account
* @param string $password
Expand All @@ -26,29 +27,26 @@ public function login(string $account, string $password): array;
public function register(string $account, string $name, string $password, string $passwordConfirmation): array;

/**
* 选择插件版本进行下载
* 选择插件版本进行下载.
*
* @param int $versionId
* @return StreamInterface
*/
public function download(int $versionId): StreamInterface;

/**
* 用户插件上传
* 用户插件上传.
*
* @param array $options
* @return array
*/
public function upload(array $options): array;

/**
* 获取插件市场发布的插件
* 获取插件市场发布的插件.
*
* @param int $page
* @return array
*/
public function plugins(int $page): array;



}
}
1 change: 0 additions & 1 deletion src/Providers/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Yxx\LaravelPlugin\Console\Commands\InstallCommand;
use Yxx\LaravelPlugin\Console\Commands\ListCommand;
use Yxx\LaravelPlugin\Console\Commands\LoginCommand;
use Yxx\LaravelPlugin\Console\Commands\MarketPluginsCommand;
use Yxx\LaravelPlugin\Console\Commands\MigrateCommand;
use Yxx\LaravelPlugin\Console\Commands\MigrationMakeCommand;
use Yxx\LaravelPlugin\Console\Commands\ModelMakeCommand;
Expand Down
3 changes: 2 additions & 1 deletion src/Providers/PluginServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ protected function registerServices(): void

return new $class($app);
});
$this->app->singleton(ClientInterface::class, function ($app){
$this->app->singleton(ClientInterface::class, function ($app) {
$class = $app['config']->get('plugins.market.default');
if ($class === null) {
throw InvalidActivatorClass::missingConfig();
}

return new $class();
});
$this->app->alias(RepositoryInterface::class, 'plugins.repository');
Expand Down
25 changes: 15 additions & 10 deletions src/Support/Client/Market.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
<?php

namespace Yxx\LaravelPlugin\Support\Client;

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\StreamInterface;
use Yxx\LaravelPlugin\Contracts\ClientInterface;
use Yxx\LaravelPlugin\Traits\HasGuzzleClient;
use Psr\Http\Message\StreamInterface;;

class Market implements ClientInterface
{
use HasGuzzleClient;

/**
* 用户登录
* 用户登录.
*
* @param string $account
* @param string $password
* @return array
*
* @throws GuzzleException
*/
public function login(string $account, string $password): array
{
return $this->httpPostJson('/api/pluginmarket/login', [
'email' => $account,
'password' => $password
'password' => $password,
]);
}

/**
* 用户注册
* 用户注册.
*
* @param string $account
* @param string $password
* @param string $name
* @param string $passwordConfirmation
* @return array
*
* @throws GuzzleException
*/
public function register(string $account, string $name, string $password, string $passwordConfirmation): array
Expand All @@ -48,10 +51,11 @@ public function register(string $account, string $name, string $password, string
}

/**
* 选择插件版本进行下载
* 选择插件版本进行下载.
*
* @param int $versionId
* @return StreamInterface
*
* @throws GuzzleException
*/
public function download(int $versionId): StreamInterface
Expand All @@ -71,30 +75,31 @@ public function download(int $versionId): StreamInterface
}

/**
* 用户插件上传
* 用户插件上传.
*
* @param array $options
* @return array
*
* @throws GuzzleException
*/
public function upload(array $options): array
{
return $this->request('/api/pluginmarket/plugins', 'POST', $options);
}


/**
* 获取插件市场发布的插件
* 获取插件市场发布的插件.
*
* @param int $page
* @return array
*
* @throws GuzzleException
*/
public function plugins(int $page): array
{
return $this->httpGet('/api/pluginmarket/plugins', [
'page' => $page,
'status' => 'release'
'status' => 'release',
]);
}
}
}
8 changes: 6 additions & 2 deletions src/Traits/HasGuzzleClient.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Yxx\LaravelPlugin\Traits;

use GuzzleHttp\Client;
Expand All @@ -10,7 +11,6 @@

trait HasGuzzleClient
{

/**
* @param string $url
* @param array $data
Expand All @@ -29,6 +29,7 @@ public function httpPost(string $url, array $data = []): array
* @param array $form
* @param array $query
* @return array|mixed
*
* @throws GuzzleException
*/
public function httpUpload(string $url, array $files = [], array $form = [], array $query = []): array
Expand Down Expand Up @@ -66,6 +67,7 @@ public function httpGet(string $url, array $query = []): array
{
return $this->request($url, 'GET', ['query' => $query]);
}

/**
* @param string $url
* @param array $data
Expand All @@ -84,6 +86,7 @@ public function httpPostJson(string $url, array $data = [], array $query = []):
* @param array $data
* @param array $query
* @return array
*
* @throws GuzzleException
*/
public function httpPutJson(string $url, array $data = [], array $query = []): array
Expand Down Expand Up @@ -124,6 +127,7 @@ public function request(string $url, string $method = 'GET', array $options = []
throw $e;
}
}

/**
* Get a HTTP client instance.
*
Expand Down Expand Up @@ -170,4 +174,4 @@ public function getAuthorization(): string
return request()->header('token', '');
}
}
}
}
1 change: 0 additions & 1 deletion tests/Commands/RouteProviderMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function tearDown(): void
parent::tearDown();
}


public function it_generates_a_new_service_provider_class()
{
$path = $this->pluginPath.'/Providers/RouteServiceProvider.php';
Expand Down

0 comments on commit ac04294

Please sign in to comment.