Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #36: Remove yiisoft/view dependency #56

Merged
merged 9 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
"phpspec/php-diff": "^1.1.0",
"yiisoft/aliases": "^2.0",
"yiisoft/arrays": "^2.1",
"yiisoft/data-response": "^1.0",
"yiisoft/http": "^1.2",
"yiisoft/json": "^1.0",
"yiisoft/request-model": "dev-master",
"yiisoft/strings": "^2.1",
"yiisoft/validator": "^3.0@dev",
"yiisoft/view": "^6.0",
"yiisoft/yii-console": "^1.3"
},
"require-dev": {
Expand Down
5 changes: 2 additions & 3 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
'gii/controller' => ControllerCommand::class,
],
],
'yiisoft/aliases' => [
'@yii-gii' => dirname(__DIR__),
],
'yiisoft/yii-gii' => [
'enabled' => true,
'allowedIPs' => ['127.0.0.1', '::1'],
'generators' => [
'controller' => \Yiisoft\Yii\Gii\Generator\Controller\Generator::class,
],
Expand Down
40 changes: 40 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,45 @@
* @var array $params
*/

use Psr\Http\Message\ResponseFactoryInterface;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\Router\Group;
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
use Yiisoft\Router\Route;
use Yiisoft\Validator\ValidatorInterface;
use Yiisoft\Yii\Debug\Api\Middleware\ResponseDataWrapper;
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
use Yiisoft\Yii\Gii\Controller\DefaultController;
use Yiisoft\Yii\Middleware\IpFilter;
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

if (!(bool)($params['yiisoft/yii-gii']['enabled'] ?? false)) {
return [];
}

return [
Group::create('/gii/api')
->middleware(
static function (ResponseFactoryInterface $responseFactory, ValidatorInterface $validator) use ($params) {
return new IpFilter(
validator: $validator,
responseFactory: $responseFactory,
ipRanges: $params['yiisoft/yii-gii']['allowedIPs']
);
}
)
->middleware(FormatDataResponseAsJson::class)
->middleware(ResponseDataWrapper::class)
->namePrefix('gii/api/')
->routes(
Route::get('/{generator}')
->action([DefaultController::class, 'get'])
->name('generator'),
Route::post('/{generator}/preview')
->action([DefaultController::class, 'preview'])
->name('preview'),
Route::post('/{generator}/generate')
->action([DefaultController::class, 'generate'])
->name('generate'),
Route::post('/{generator}/diff')
->action([DefaultController::class, 'diff'])
->name('diff')
),
];
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<ignoreFiles>
<directory name="vendor" />
<directory name="src/Generator/Controller/default" />
<file name="src/Generator/Controller/form.php" />
</ignoreFiles>
</projectFiles>
</psalm>
22 changes: 15 additions & 7 deletions src/CodeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Diff;
use RuntimeException;
use Yiisoft\Html\Html;
use Yiisoft\Yii\Gii\Component\DiffRendererHtmlInline;

/**
Expand Down Expand Up @@ -34,6 +33,12 @@ final class CodeFile
* The new code file and the existing one are identical.
*/
public const OP_SKIP = 2;
public const OPERATIONS = [
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
self::OP_CREATE => 'Create',
self::OP_OVERWRITE => 'Overwrite',
self::OP_SKIP => 'Skip',
];

/**
* @var string an ID that uniquely identifies this code file.
*/
Expand Down Expand Up @@ -140,7 +145,7 @@ public function getType(): string
/**
* Returns preview or false if it cannot be rendered
*/
public function preview(): bool|string
public function preview(): false|string
{
if (($pos = strrpos($this->path, '.')) !== false) {
$type = substr($this->path, $pos + 1);
Expand All @@ -153,7 +158,12 @@ public function preview(): bool|string
}

if (!in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
return nl2br(Html::encode($this->content));
$content = htmlspecialchars(
$this->content,
ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML5,
'UTF-8'
);
return nl2br($content);
}

return false;
Expand All @@ -162,7 +172,7 @@ public function preview(): bool|string
/**
* Returns diff or false if it cannot be calculated
*/
public function diff(): bool|string
public function diff(): false|string
{
$type = strtolower($this->getType());
if (in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
Expand Down Expand Up @@ -195,9 +205,7 @@ private function renderDiff(mixed $lines1, mixed $lines2): string
}

$renderer = new DiffRendererHtmlInline();
$diff = new Diff($lines1, $lines2);

return $diff->render($renderer);
return (new Diff($lines1, $lines2))->render($renderer);
}

public function getId(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Component/DiffRendererHtmlInline.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @psalm-suppress PropertyNotSetInConstructor
*/
class DiffRendererHtmlInline extends Diff_Renderer_Html_Array
final class DiffRendererHtmlInline extends Diff_Renderer_Html_Array
{
/**
* Render a and return diff with changes between the two sequences
Expand Down
Loading