68publishers/smart-nette-component 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

68publishers/smart-nette-component

Composer 安装命令:

composer require 68publishers/smart-nette-component

包简介

Features for Nette Components and Presenters. Authorization via annotations, template resolving and overloading etc. ...

README 文档

README

This package adds some useful features for Nette Presenters and Components, such as authorization for Presenters, their actions and signals using PHP8 attributes, authorization for component signals using attributes, and resolving of component template files.

Checks Coverage Status Total Downloads Latest Version PHP Version

Installation

The best way to install 68publishers/smart-nette-component is using Composer:

$ composer require 68publishers/smart-nette-component

Authorization attributes

To use the authorization attributes, you need to register a compiler extension.

extensions:
    component_authorization: SixtyEightPublishers\SmartNetteComponent\Bridge\Nette\DI\ComponentAuthorizationExtension

# The default configuration (you don't need to define it) is as follows:
component_authorization:
    cache: %debugMode%
    scanDirs:
    	- %appDir%
    scanComposer: yes
    scanFilters:
    	- *Presenter
    	- *Control
    	- *Component

Attributes can be cached when folding the DI container to avoid using reflection at runtime. The extension will create a classmap and a map of all attributes automatically, it just needs to know where to look for Presenters and Components. This is done with the scanDirs, scanComposer and scanFilters options, which behave similarly to nette/application.

Now add the following trait to your BasePresenter and BaseControl:

use Nette\Application\UI\Presenter;
use SixtyEightPublishers\SmartNetteComponent\Bridge\Nette\Application\AuthorizationTrait;

abstract class BasePresenter extends Presenter
{
    use AuthorizationTrait;
}
use Nette\Application\UI\Control;
use SixtyEightPublishers\SmartNetteComponent\Bridge\Nette\Application\AuthorizationTrait;

abstract class BaseControl extends Control
{
    use AuthorizationTrait;
}

From now, you can use authorization attributes in your Presenters and Components:

use SixtyEightPublishers\SmartNetteComponent\Attribute\LoggedIn;
use SixtyEightPublishers\SmartNetteComponent\Attribute\Allowed;

#[LoggedIn]
final class AdminProductPresenter extends BasePresenter
{
    #[Allowed('product_resource', 'add')]
    public function actionAdd(): void {}

    #[Allowed('product_resource', 'delete')]
    public function handleDelete(): void {}
}
use SixtyEightPublishers\SmartNetteComponent\Attribute\LoggedIn;
use SixtyEightPublishers\SmartNetteComponent\Attribute\Allowed;

final class EditOrderControl extends BaseControl
{
    #[Allowed('order_resource', 'delete_item')]
    public function handleDeleteItem(): void {}
}

The Presenter/Component throws the exception SixtyEightPublishers\SmartNetteComponent\Exception\ForbiddenRequestException if any of the conditions in the attributes are not met.

The package includes the following attributes:

  • Allowed
  • InRole
  • LoggedIn
  • LoggedOut

If you would like to react somehow to the thrown exception, you can overwrite the onForbiddenRequest() method in a Presenter/Component.

protected function onForbiddenRequest(ForbiddenRequestException $exception): void
{
    # `$exception->rule` contains failed attribute
    
    $this->flashMessage('You don\'t have access here!', 'error');
    $this->redirect('Homepage:');
}

Custom authorization attributes

You can register your own attributes in the following way:

use SixtyEightPublishers\SmartNetteComponent\Attribute\AttributeInterface;
use SixtyEightPublishers\SmartNetteComponent\Authorization\RuleInterface;
use SixtyEightPublishers\SmartNetteComponent\Authorization\RuleHandlerInterface;
use SixtyEightPublishers\SmartNetteComponent\Exception\ForbiddenRequestException;

final class CustomRule implements AttributeInterface, RuleInterface
{
    # ...
}

final class CustomRuleHandler implements RuleHandlerInterface
{
    public function canHandle(RuleInterface $rule): bool
    {
        return $rule instanceof CustomRule;
    }

    public function __invoke(RuleInterface $rule): void
    {
        assert($rule instanceof CustomRule);

        if (...) {
            throw new ForbiddenRequestException($rule);
        }
    }
}
services:
    -
        autowired: no
        factory: CustomRuleHandler

Template resolving

You don't need to register any compiler extension to use this feature, just use the TemplateResolverTrait trait in your BaseControl.

use Nette\Application\UI\Control;
use SixtyEightPublishers\SmartNetteComponent\Bridge\Nette\Application\TemplateResolverTrait;

abstract class BaseControl extends Control
{
    use TemplateResolverTrait;
}

The base render() method is already declared in the trait. To assign variables to the template, we can use the beforeRender() method. You can also define custom render*() methods that are called for rendering using {control myControl:foo}.

final class MyControl extends BaseControl
{
    protected function beforeRender(): void
    {
        # assign variables into the template here
    }

    public function renderFoo(): void
    {
        $this->doRender('foo');
    }
}

The template for the base render method will be resolved as COMPONENT_DIRECTORY/templates/myControl.latte or COMPONENT_DIRECTORY/templates/MyControl.latte.

The template for the foo render method will be resolved as COMPONENT_DIRECTORY/templates/foo.myControl.latte or COMPONENT_DIRECTORY/templates/foo.MyControl.latte.

Of course, you can set a template file manually:

final class MyPresenter extends BasePresenter
{
    protected function createComponentMyControl() : FooControl
    {
        $control = $this->myControlFactory->create();

        $control->setFile(__DIR__ . '/path/to/file.latte');
        # or relatively from a component's directory:
        $control->setRelativeFile('templates/new/myControl.latte');

        # you can change the template for a specific render type:
        $control->setFile(__DIR__ . '/path/to/myControl.latte', 'foo'); # template for `renderFoo()`

        return $control;
    }
}

Contributing

Before opening a pull request, please check your changes using the following commands

$ make init # to pull and start all docker images

$ make cs.check
$ make stan
$ make tests.all

68publishers/smart-nette-component 适用场景与选型建议

68publishers/smart-nette-component 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.12k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「components」 「nette」 「component」 「68publishers」 「smart-nette-component」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 68publishers/smart-nette-component 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 68publishers/smart-nette-component 我们能提供哪些服务?
定制开发 / 二次开发

基于 68publishers/smart-nette-component 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 25
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-10