Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Jan 23, 2015
0 parents commit 8af3f70
Show file tree
Hide file tree
Showing 18 changed files with 1,060 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*~
*.kate-swp

!.*
.idea/

composer.phar
composer.lock

vendor/
18 changes: 18 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
filter:
excluded_paths:
- 'vendor/*'

tools:
php_mess_detector:
config:
controversial_rules: { superglobals: false }
php_cpd: true
php_pdepend: true
php_code_coverage: false
php_code_sniffer: true
php_cs_fixer: true
php_loc: true
php_analyzer: true
sensiolabs_security_checker: true
external_code_coverage:
timeout: '900'
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: php

matrix:
include:
- env: TYPE=coverage
php: 5.6
- env: TYPE=UNIT;
php: 5.4
- env: TYPE=UNIT;
php: hhvm

script:
- bash ./tests/travis/run-tests.sh

after_success:
- bash ./tests/travis/upload-coverage-report.sh
340 changes: 340 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Message reporter

[![Build Status](https://secure.travis-ci.org/onoi/message-reporter.svg?branch=master)](http://travis-ci.org/onoi/message-reporter)
[![Code Coverage](https://scrutinizer-ci.com/g/onoi/message-reporter/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/onoi/message-reporter/?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/onoi/message-reporter/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/onoi/message-reporter/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/onoi/message-reporter/version.png)](https://packagist.org/packages/onoi/message-reporter)
[![Packagist download count](https://poser.pugx.org/onoi/message-reporter/d/total.png)](https://packagist.org/packages/onoi/message-reporter)
[![Dependency Status](https://www.versioneye.com/php/onoi:message-reporter/badge.png)](https://www.versioneye.com/php/onoi:message-reporter)

An interface to report and relay arbitrary messages to registered handlers. This was part of
the [Semantic MediaWiki][smw] code base and is now being deployed as independent library.

## Requirements

PHP 5.3 or later

## Installation

The recommended installation method for this library is by either adding
the dependency to your [composer.json][composer].

```json
{
"require": {
"onoi/message-reporter": "~1.0"
}
}
```
or to execute `composer require onoi/message-reporter:~1.0`.

## Usage

The message reporter interface for all interactions is specified by `Onoi\MessageReporter\MessageReporter`.

```php
class Bar {

private $reporter;

public function __construct() {
$this->reporter = MessageReporterFactory::getInstance()->newNullMessageReporter();
}

public function setMessageReporter( MessageReporter $reporter ) {
$this->reporter = $reporter;
}

public function doSomethingElse() {
$this->reporter->reportMessage( 'Did something ...' );
}
}
```
```php
class Foo implements MessageReporter {

public function doSomething( Bar $bar ) {

$messageReporterFactory = new MessageReporterFactory();

$messageReporter = $messageReporterFactory->newObservableMessageReporter();
$messageReporter->registerReporterCallback( array( $this, 'reportMessage' ) );

or

// If the class implements the MessageReporter
$messageReporter->registerMessageReporter( $this );

$bar->setMessageReporter( $messageReporter );
$bar->doSomethingElse();
}

public function reportMessage( $message ) {
// output
}
}

$foo = new Foo();
$foo->doSomething( new Bar() );
```

## Contribution and support

If you want to contribute work to the project please subscribe to the
developers mailing list and have a look at the [contribution guidelinee](/CONTRIBUTING.md). A list of people who have made contributions in the past can be found [here][contributors].

* [File an issue](https://github.com/onoi/message-reporter/issues)
* [Submit a pull request](https://github.com/onoi/message-reporter/pulls)

### Tests

The library provides unit tests that covers the core-functionality normally run by the [continues integration platform][travis]. Tests can also be executed manually using the PHPUnit configuration file found in the root directory.

### Release notes

* 1.0.0 initial release (2015-01-X)

## License

[GNU General Public License 2.0 or later][license].

[composer]: https://getcomposer.org/
[contributors]: https://github.com/onoi/message-reporter/graphs/contributors
[license]: https://www.gnu.org/copyleft/gpl.html
[travis]: https://travis-ci.org/onoi/message-reporter
[smw]: https://github.com/SemanticMediaWiki/SemanticMediaWiki/
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "onoi/message-reporter",
"type": "library",
"description": "An interface to report and relay arbitrary messages to registered handlers",
"keywords": [
"messages"
],
"homepage": "https://github.com/onoi/message-reporter",
"license": "GPL-2.0+",
"authors": [
{
"name": "Jeroen De Dauw"
},
{
"name": "mwjames"
}
],
"require": {
"php": ">=5.3.2"
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Onoi\\MessageReporter\\": "src/"
}
},
"config": {
"process-timeout": 0
},
"scripts":{
"phpunit": "phpunit -c phpunit.xml.dist"
}
}
24 changes: 24 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
cacheTokens="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="true">
<testsuites>
<testsuite name="onoi-message-reporter">
<directory>tests/phpunit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
24 changes: 24 additions & 0 deletions src/MessageReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Onoi\MessageReporter;

/**
* Interface for objects that can report messages
*
* @license GNU GPL v2+
* @since 1.0
*
* @author Jeroen De Dauw < [email protected] >
*/
interface MessageReporter {

/**
* Report the provided message
*
* @since 1.0
*
* @param string $message
*/
public function reportMessage( $message );

}
57 changes: 57 additions & 0 deletions src/MessageReporterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Onoi\MessageReporter;

/**
* @license GNU GPL v2+
* @since 1.0
*
* @author mwjames
*/
class MessageReporterFactory {

/**
* @var MessageReporterFactory
*/
private static $instance = null;

/**
* @since 1.0
*
* @return MessageReporterFactory
*/
public static function getInstance() {

if ( self::$instance === null ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* @since 1.0
*/
public static function clear() {
self::$instance = null;
}

/**
* @since 1.0
*
* @return NullMessageReporter
*/
public function newNullMessageReporter() {
return new NullMessageReporter();
}

/**
* @since 1.0
*
* @return ObservableMessageReporter
*/
public function newObservableMessageReporter() {
return new ObservableMessageReporter();
}

}
20 changes: 20 additions & 0 deletions src/NullMessageReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Onoi\MessageReporter;

/**
* @license GNU GPL v2+
* @since 1.0
*
* @author Jeroen De Dauw < [email protected] >
*/
class NullMessageReporter implements MessageReporter {

/**
* @since 1.0
*
* {@inheritDoc}
*/
public function reportMessage( $message ) {}

}
69 changes: 69 additions & 0 deletions src/ObservableMessageReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Onoi\MessageReporter;

/**
* Message reporter that reports messages by passing them along to all
* registered handlers.
*
* @license GNU GPL v2+
* @since 1.0
*
* @author Jeroen De Dauw < [email protected] >
*/
class ObservableMessageReporter implements MessageReporter {

/**
* @since 1.0
*
* @var MessageReporter[]
*/
protected $reporters = array();

/**
* @since 1.0
*
* @var callable[]
*/
protected $callbacks = array();

/**
* @see MessageReporter::report
*
* @since 1.0
*
* @param string $message
*/
public function reportMessage( $message ) {
foreach ( $this->reporters as $reporter ) {
$reporter->reportMessage( $message );
}

foreach ( $this->callbacks as $callback ) {
call_user_func( $callback, $message );
}
}

/**
* Register a new message reporter.
*
* @since 1.0
*
* @param MessageReporter $reporter
*/
public function registerMessageReporter( MessageReporter $reporter ) {
$this->reporters[] = $reporter;
}

/**
* Register a callback as message reporter.
*
* @since 1.0
*
* @param callable $handler
*/
public function registerReporterCallback( $handler ) {
$this->callbacks[] = $handler;
}

}
Loading

0 comments on commit 8af3f70

Please sign in to comment.