定制 werx/messages 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

werx/messages

Composer 安装命令:

composer require werx/messages

包简介

Simple package for building and displaying various types of messages in a web app.

README 文档

README

Source Build Status Total Downloads Latest Stable Version

Simple package for displaying various types of messages in a web app.

Usage

<?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();

Now you can add some messages. Valid messages types are error, info, warning, and success.

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.');

You can also use printf compatible format codes in your message strings along with an array as the 2nd parameter containing string replacements to make it easier to embed dynamic data in your messages without doing a lot of string concatenation.

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

// The quick brown fox jumped over the log.

If you are only using one replacement, you can pass a string as the 2nd param instead of an array.

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

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

Furthermore, you can add messages as an array:

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

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

Once you've added the messages to the stack, you have a couple options.

  1. Fetch all the messages back as an array.
$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 the messages using a decorator
$display = Messages::display();
print $display;

The above renders something like this:

<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>

Decorators

By default, a simple decorator will be used that wraps the messages in a series of unordered lists as shown above. The <ul> for each type of message (error, info, success) will be classed with the name of the message type.

If you are using Bootstrap for your design, you can specify that messages should be decorated using the Bootstrap Alert HTML Markup instead.

If you want to create your own decorator, just create a class that implements werx\Messages\Decorators\DecoratorInterface and pass an instance to Messages::setDecorator().

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

Renders:

<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>

Sessions

By storing messages in session, they can persist across multiple page loads until you either display or delete them.

By default, this library will create a new instance of the Symfony Native Session Storage object for storage of messages. If you already have an instance of a Symfony Session Interface, you can pass that to 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);

Installation

This package is installable and autoloadable via Composer as werx/messages. If you aren't familiar with the Composer Dependency Manager for PHP, you should read this first.

$ composer require werx/messages --prefer-dist

Contributing

Unit Testing

$ vendor/bin/phpunit

Coding Standards

This library uses PHP_CodeSniffer to ensure coding standards are followed.

I have adopted the PHP FIG PSR-2 Coding Standard EXCEPT for the tabs vs spaces for indentation rule. PSR-2 says 4 spaces. I use tabs. No discussion.

To support indenting with tabs, I've defined a custom PSR-2 ruleset that extends the standard PSR-2 ruleset used by PHP_CodeSniffer. You can find this ruleset in the root of this project at PSR2Tabs.xml

Executing the codesniffer command from the root of this project to run the sniffer using these custom rules.

$ ./codesniffer

werx/messages 适用场景与选型建议

werx/messages 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.59k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 01 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 werx/messages 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-07