Skip to content

Commit

Permalink
feat(file-storage): 储存增加上传后的 node 验证规则
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Sep 12, 2018
1 parent ebafb5c commit 3fe06d7
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 20 deletions.
10 changes: 6 additions & 4 deletions app/FileStorage/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Zhiyi\Plus\FileStorage\ChannelManager;
use Zhiyi\Plus\FileStorage\Http\MakeRoutes;
use Zhiyi\Plus\FileStorage\StorageInterface;
use Zhiyi\Plus\FileStorage\Validators\Rulers\ValidatorRegister;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -49,9 +50,10 @@ public function register()
*/
public function boot()
{
$this
->app
->make(MakeRoutes::class)
->register();
// Register routes.
$this->app->make(MakeRoutes::class)->register();

// Register validate rules.
$this->app->make(ValidatorRegister::class)->register();
}
}
16 changes: 0 additions & 16 deletions app/FileStorage/StorageInterface.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
<?php

/*
* +----------------------------------------------------------------------+
* | ThinkSNS Plus |
* +----------------------------------------------------------------------+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
* +----------------------------------------------------------------------+
* | This source file is subject to version 2.0 of the Apache license, |
* | that is bundled with this package in the file LICENSE, and is |
* | available through the world-wide-web at the following url: |
* | http://www.apache.org/licenses/LICENSE-2.0.html |
* +----------------------------------------------------------------------+
* | Author: Slim Kit Group <master@zhiyicx.com> |
* | Homepage: www.thinksns.com |
* +----------------------------------------------------------------------+
*/

declare(strict_types=1);

/*
Expand Down
41 changes: 41 additions & 0 deletions app/FileStorage/Validators/Rulers/FileStorageRuler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Zhiyi\Plus\FileStorage\Validators\Rulers;

use Zhiyi\Plus\FileStorage\StorageInterface;

class FileStorageRuler implements RulersInterface
{
/**
* The storage.
* @var \Zhiyi\Plus\FileStorage\StorageInterface
*/
protected $storage;

/**
* Create the ruler instalce.
* @param Zhiyi\Plus\FileStorage\StorageInterface $storage
*/
public function __construct(StorageInterface $storage)
{
$this->storage;
}

/**
* Rule handler.
* @param array $params
* @return bool
*/
public function handle(array $params): bool
{
try {
return (bool) $this->storage
->meta(new Resource($params[1]))
->getSize();
} finally {
return false;
}
}
}
15 changes: 15 additions & 0 deletions app/FileStorage/Validators/Rulers/RulerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Zhiyi\Plus\FileStorage\Validators\Rulers;

interface RulerInterface
{
/**
* Rule handler.
* @param array $params
* @return bool
*/
public function handle(array $params): bool;
}
56 changes: 56 additions & 0 deletions app/FileStorage/Validators/Rulers/ValidatorRulesRegister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Zhiyi\Plus\FileStorage\Validators\Rulers;

use Zhiyi\Plus\AppInterface;
use Illuminate\Contracts\Validation\Factory as ValidationFactoryContract;

class ValidatorRegister
{
/**
* The app.
* @var \Zhiyi\Plus\AppInterface
*/
protected $app;

/**
* The app validator.
* @var \Illuminate\Contracts\Validation\Factory
*/
protected $validator;

/**
* The rulers.
* @var array
*/
protected $rules = [
'file_storage'
];

/**
* Create the validator rules register instance.
* @param \Zhiyi\Plus\AppInterface $app
* @param \Illuminate\Contracts\Validation\Factory $validator
*/
public function __construct(AppInterface $app, ValidationFactoryContract $validator)
{
$this->app = $app;
$this->validator = $validator;
}

/**
* The reguster.
* @return void
*/
public function register(): void
{
$app = $this->app;
foreach($this->rules as $ruleName => $rulerClassname) {
$this->validator->extend($ruleName, function (...$params) use ($app, $rulerClassname): bool {
return $app->make($rulerClassname)->handle($params);
});
}
}
}

0 comments on commit 3fe06d7

Please sign in to comment.