interitty / application
在Interitty项目中使用的标准Nette/Application的一些特定功能的扩展。
Requires
- php: ~8.3
- dg/composer-cleaner: ~2.2
- interitty/component-model: ~1.0
- interitty/flash-message-control: ~1.0
- interitty/utils: ~1.0
- latte/latte: ~3.0
- nette/application: ~3.2
- nette/bootstrap: ~3.2
- nette/caching: ~3.3
Requires (Dev)
- interitty/code-checker: ~1.0
- interitty/phpunit: ~1.0
- nette/forms: ~3.2
README
通过一些特定于Interitty项目的功能扩展了标准Nette/Application。
要求
- PHP >= 8.3
安装
安装interitty/application的最佳方式是使用Composer。
composer require interitty/application
特性
除了使用标准的Nette\Application\*
和Nette\Bootstrap\*
类之外,还提供了新的Interitty\Application\*
和Interitty\Bootstrap\*
类,这些类提供了一些其他特性。
参数
除了来自Nette\Appliacation的默认参数之外,还有以下参数可供使用,以便使应用程序更容易配置。
parameters:
applicationNamespace: 'App' # Can be used in code generators
controlsDir: %appDir%/Controls # Can be used in code generators
组件工厂定位器
由于Interitty\Application\Presenter
扩展了组件工厂定位器功能,因此可以方便地创建和访问所有已注册的组件,而无需编写不必要的代码。
为了更方便地使用,当创建DI容器时,会自动生成一个辅助辅助特性,它包含为每个已注册组件显式定义的方法,这些方法使IDE自动完成和静态代码分析工作得更好。
由于这个辅助特性与特定的应用程序、其命名空间及其自动加载器直接相关,因此有必要在应用程序的config.neon
文件中设置其生成,并将其在BasePresenter中使用。对于生产使用和更好的代码清晰度,建议将生成的辅助作为项目的一部分添加到版本控制系统。
parameters:
applicationNamespace: 'App' # Can be used in code generators
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\Application\UI\Presenter;
use Interitty\ComponentModel\ComponentLocatorHelperTrait;
abstract class BasePresenter extends Presenter
{
use ComponentLocatorHelperTrait;
}
模板全局参数
有时,可以从neon配置直接在模板中设置变量。为此,有一个TemplateGlobalParametersExtension
。
templateParameters:
author: "Interitty <info@interitty.org>"
description: "Description of the example application"
keywords: ["application", "interitty"]
name: "Example application"
环境控制配置
某些应用程序属性受其运行环境的影响。生产行为可能不同于开发行为,为此,Configurator
被扩展以设置环境类型,它可以自动区分调试模式。
// bootstrap.php
<?php
declare(strict_types=1);
use Interitty\Bootstrap\Configurator;
use Interitty\Utils\Strings;
require __DIR__ . '/../vendor/autoload.php';
$environment = getenv('ENVIRONMENT');
$configurator = new Configurator();
$configurator->setEnvironment(Strings::lower($environment === false ? $configurator::ENVIRONMENT_LOCAL : $environment));
$configurator->setDebugMode($configurator::DEBUG_MODE_BY_ENVIRONMENT);
$configurator->enableTracy(__DIR__ . '/../logs');
$configurator->setTempDirectory(__DIR__ . '/../temp');
$configurator->addConfig(__DIR__ . '/Config/config.neon');
$container = $configurator->createContainer();
return $container;
扩展基本控件
所有Interitty图形组件从一开始就与Translator一起工作。它们与模板以统一的方式工作,其中在组件类定义文件的默认位置搜索源文件,并使用默认名称。
/[control-name].latte
/[ControlClassName].latte
/default.latte
/template/[control-name].latte
/template/[ControlClassName].latte
/template/default.latte
然而,同时,可能还有能力在特定应用程序中覆盖默认模板是有用的。
services:
ControlFactory:
implement: Vendor\Namespace\Controls\Control\ControlFactoryInterface
setup:
- setTemplateFile(%appDir%/Controls/Control/ControlCustom.latte)
扩展基本表单
所有Interitty表单都从底层支持与Translator集成。表单基础处理已扩展以处理异常和没有按钮发送的情况。已添加processFormSetup
方法到表单生命周期,用于添加表单元素及其设置。
表单有一个预设的提交按钮。然而,如果需要,可以更改其文字。
/**
* @inheritDoc
*/
public function processFormSetup(): void
{
// ... Add another form controls
parent::processFormSetup();
$this->getComponentButtonSubmit()->setCaption('Send');
}
闪存消息控制
为了使与闪存消息的交互尽可能简单,集成了interitty/flash-message-control
。这为Presenter添加了助手方法,负责整个生命周期,包括ajax重绘。
通用表单错误处理器
通常需要在Presenter中直接将所有可能发生的表单错误渲染为FlashMessages。为此,添加了一个助手函数processFormError
来处理这一点。
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
/** @persistent */
public $parameter;
/**
* ExampleForm control factory
*
* @param string $name [OPTIONAL]
* @return ExampleForm
*/
public function createComponentExampleForm(string $name = self::CONTROL_EXAMPLE_FORM): ExampleForm
{
$form = parent::createComponentExampleForm($name);
$form->onError[] = fn(Form $form) => $this->processFormError($form);
return $form;
}
}
安全持久参数
持久参数使在请求之间传输值变得非常容易。然而,从原则上讲,这是一个潜在的攻击向量,因为值通过URL中的参数传递,因此可以被用户更改。因此,应该对其进行验证。
最佳做法是使用适当的setter
,在其中进行必要的验证。interitty/application包扩展了持久参数的功能,如果存在适当的setter
,则使用它而不是直接设置。
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
/** @persistent */
public $parameter;
/**
* Parameter setter
*
* @param string $parameter
* @return static Provides fluent interface
*/
protected function setParameter(string $parameter): static
{
// Any additional validation can be here
$this->parameter = $parameter;
return $this;
}
}
SendFile助手
通常,应用程序需要为用户提供可下载的文件。因此,为Presenter添加了一个名为sendFile
的扩展,以标准化这一操作并简化进一步的测试。
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\Application\UI\Presenter;
class FilePresenter extends Presenter
{
/**
* Download action handler
*
* @return void
*/
public function actionDownload(): void
{
$assetsDir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'assets';
$file = assetsDir . DIRECTORY_SEPARATOR . 'file.pdf';
$fileName = 'filename.pdf';
$contentType = ''application/pdf';
$this->sendFile($file, $fileName, $contentType);
}
}