werx/messages

用于在Web应用程序中构建和显示各种类型消息的简单包。

1.2.0 2015-02-03 06:01 UTC

This package is auto-updated.

Last update: 2024-08-29 04:03:25 UTC


README

Source Build Status Total Downloads Latest Stable Version

用于在Web应用程序中显示各种类型消息的简单包。

用法

<?php
use werx\Messages\Messages;

// Import the composer autoloader, if you aren't already using it.
require '../vendor/autoload.php';

// Get an instance of the Messages class.
Messages::getInstance();

现在您可以添加一些消息。有效的消息类型有错误、信息、警告和成功。

Messages::error('Oops, something bad happened.');
Messages::info('Nothing big, this is just an informational message.');
Messages::warning('A little more serious than info, but not quite an error.');
Messages::success('Hooray! This is a success message.');
Messages::success('Here is another success message.');

您还可以在消息字符串中使用与printf兼容的格式代码,并将其与包含字符串替换的数组作为第二个参数一起使用,以简化将动态数据嵌入消息而不进行大量字符串连接的过程。

Messages::error('The quick %s %s jumped over the %s.', ['brown', 'fox', 'log']);

// The quick brown fox jumped over the log.

如果您只使用一个替换,您可以传递一个字符串作为第二个参数而不是数组。

Messages::error('The quick brown fox jumped over the %s.', 'lazy dog');

// The quick brown fox jumped over the lazy dog.

此外,您还可以以数组的形式添加消息

Messages::warning(['This is a warning.', 'This is also a warning.']);

// This is a warning.
// This is also a warning.

一旦将消息添加到堆栈中,您就有几个选项。

  1. 以数组的形式获取所有消息。
$items = Messages::all();
print_r($items);

/*
Array
(
	[error] => Array
		(
			[0] => Oops, something bad happened.
		)

	[info] => Array
		(
			[0] => Nothing big, this is just an informational message.
		)

	[success] => Array
		(
			[0] => Hooray! This is a success message.
			[1] => Here is another success message.
		)

	[warning] => Array
		(
			[0] => A little more serious than info, but not quite an error.
		)

)
*/
  1. 使用装饰器显示消息
$display = Messages::display();
print $display;

上面的渲染效果如下

<ul class="error">
	<li>Oops, something bad happened.</li>
</ul>

<ul class="warning">
	<li>A little more serious than info, but not quite an error.</li>
</ul>

<ul class="info">
	<li>Nothing big, this is just an informational message.</li>
</ul>

<ul class="success">
	<li>Hooray! This is a success message.</li>
	<li>Here is another success message.</li>
</ul>

装饰器

默认情况下,将使用一个简单的装饰器,将消息包裹在一系列无序列表中,如上所示。每种类型消息(错误、信息、成功)的

    将被赋予消息类型的名称。

    如果您使用Bootstrap进行设计,您可以指定使用Bootstrap Alert HTML标记来装饰消息。

    如果您想创建自己的装饰器,只需创建一个实现werx\Messages\Decorators\DecoratorInterface的类,并将其实例传递给Messages::setDecorator()

    Messages::setDecorator(new Decorators\Bootstrap);
    $display = Messages::display();
    print $display;

    渲染

    <div class="alert alert-danger">
    	<button type="button" class="close" data-dismiss="alert">&times;</button>
    	<ul>
    		<li>Oops, something bad happened.</li>
    	</ul>
    </div>
    
    <div class="alert alert-warning">
    	<button type="button" class="close" data-dismiss="alert">&times;</button>
    	<ul>
    		<li>A little more serious than info, but not quite an error.</li>
    	</ul>
    </div>
    
    <div class="alert alert-info">
    	<button type="button" class="close" data-dismiss="alert">&times;</button>
    	<ul>
    		<li>Nothing big, this is just an informational message.</li>
    	</ul>
    </div>
    
    <div class="alert alert-success">
    	<button type="button" class="close" data-dismiss="alert">&times;</button>
    	<ul>
    		<li>Hooray! This is a success message.</li>
    		<li>Here is another success message.</li>
    	</ul>
    </div>

    会话

    通过将消息存储在会话中,它们可以在多次页面加载之间持续存在,直到您显示或删除它们。

    默认情况下,此库将为消息存储创建一个新的Symfony Native Session Storage对象。如果您已经有一个Symfony会话接口的实例,您可以将其传递给Messages::getInstance()

    // Create a new session object.
    $session = new \Symfony\Component\HttpFoundation\Session\Session(
    				new \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage(['cookie_lifetime' => 604800])
    );
    
    Messages::getInstance($session);

    安装

    此包可以通过Composer安装并自动加载,作为werx/messages。如果您不熟悉PHP的Composer依赖管理器,您应该先阅读此内容

    $ composer require werx/messages --prefer-dist

    贡献

    单元测试

    $ vendor/bin/phpunit

    编码规范

    此库使用PHP_CodeSniffer来确保遵循编码规范。

    我采用了PHP FIG PSR-2编码规范,除了缩进时使用制表符或空格的规则。PSR-2说4个空格。我使用制表符。无讨论。

    为了支持使用制表符缩进,我定义了一个自定义的PSR-2规则集,该规则集扩展了由PHP_CodeSniffer使用的标准PSR-2规则集。您可以在本项目的根目录中找到此规则集,名为PSR2Tabs.xml。

    从本项目的根目录执行codesniffer命令,以使用这些自定义规则运行sniffer。

    $ ./codesniffer