zeecoder/good-to-know

为展示给用户收集值得知道的事实的一种解决方案。

v2.0 2015-12-13 23:40 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:54:26 UTC


README

Build Status Version SensioLabsInsight

描述

该项目的原始目的是为某些需要解释的页面/功能提供一个轻松收集值得知道的事实的方法。

为了实现这一点,代码被设计得非常通用

  • 通过仓库调用获取所有数据,
  • 通过监听器对返回的集合及其元素应用转换。

由于代码使用监听器,因此添加/删除功能非常容易。

内置功能

  • 参数注入
  • 翻译支持

集成

简单示例

# good-to-know.yml
# When loaded and fetched by the `YamlFileRepository`, these will be converted
# into `Fact` objects.
- text: Something important.
  groups: [feature1]
- text: Something important about feature2.
  groups: [feature2]
- text: Something important about both features.
  groups: [feature1, feature2]
use ZeeCoder\GoodToKnow\GoodToKnow;
use ZeeCoder\GoodToKnow\Repository\YamlFileRepository;

// The `GoodToKnow` object acts as a wrapper around the given repository.
// Every call made to this object is forwarded to the repository.
// The listeners are fired after getting the result collection from the
// repository.
$gtk = new GoodToKnow(
    new YamlFileRepository(__DIR__ . '/good-to-know.yml')
    // A dispatcher could be passed as the second argument.
    // If no dispatcher is given, one gets created
);

// `findAllByGroups` is a method in the `YamlFileRepository` repository.
// It returns a collection of `ZeeCoder\GoodToKnow\Fact` objects.
// (In this case, an `\SplObjectStorage`.)
$collection = $gtk->findAllByGroups([
    'feature1'
]);

// The collection would have the "Something important." and
// "Something important about both features." texts as Fact objects.

监听器

没有监听器,主GoodToKnow对象除了将调用转发到仓库外不做任何事情。

当添加一些监听器时,事情就变得有趣了。

ParameterListener监听器

这个监听器的任务是注入已注册的参数到值得知道字符串中。

# good-to-know.yml
# When loaded and fetched by the `YamlFileRepository`, these will be converted
# into `Fact` objects.
- text: Something important. %lorem%!
  groups: [feature1]
- text: Something important about feature2.
  groups: [feature2]
- text: Something important about both features: The upload max filesize is: %upload_max_filesize%.
  groups: [feature1, feature2]
// ...
// Assuming the example above

use ZeeCoder\GoodToKnow\ParameterInjector;
use ZeeCoder\GoodToKnow\Events;
use ZeeCoder\GoodToKnow\Listener\ParameterListener;

// Getting the dispatcher, since we didn't provide one explicitly.
$dispatcher = $gtk->getDispatcher();

// Creating the ParameterInjector
$parameterInjector = (new ParameterInjector())
    // Registering parameters
    ->addParameter('%lorem%', 'ipsum')
    ->addParameter('%upload_max_filesize%', function() {
        return ini_get('upload_max_filesize');
    })
;

// Adding the listener.
// The TRANSFORM event is fired for every result returned by the repository.
$dispatcher->addListener(
    Events::TRANSFORM,
    new ParameterListener($parameterInjector)
);

$collection = $gtk->findAllByGroups([
    'feature1'
]);

// Now the collection would have the "Something important. ipsum!" and
// "Something important about both features: The upload max filesize is: 2M."
// texts as Fact objects.

TranslationListener监听器

这个监听器假设实现Symfony的TranslatorInterface的翻译器。

use ZeeCoder\GoodToKnow\Events;
use ZeeCoder\GoodToKnow\Listener\TranslationListener;

// Getting the dispatcher, since we didn't provide one explicitly.
$dispatcher = $gtk->getDispatcher();

$dispatcher->addListener(
    Events::TRANSFORM,
    new TranslationListener(
        // Assuming we have a Symfony `$translator` object.
        $translator
        // The second parameter, is the translation domain. Default: "good-to-know"
    )
);

// Now all Fact texts will be translated.

重要

TranslationListener必须在ParameterListener之前注册,或者具有更高的优先级。

这样参数就可以在翻译发生之后注入。

源代码

请考虑查看源文件,以增强或修改基本功能。

(例如,创建数据库仓库。)

许可证

MIT