phossa/phossa-shared

The shared package for other phossa packages

1.0.11 2016-05-23 07:39 UTC

This package is not auto-updated.

Last update: 2024-11-21 00:23:31 UTC


README

Build Status HHVM Latest Stable Version License

Introduction

Phossa-shared is the shared package required by other phossa packages.

Installation

Install via the composer utility.

composer require "phossa/phossa-shared=1.*"

or add the following lines to your composer.json

{
    "require": {
       "phossa/phossa-shared": "^1.0.9"
    }
}

Features

  • Exception

    All phossa exceptions implement Phossa\Shared\Exception\ExceptionInterface. To extend phossa exceptions,

    <?php
    namespace Phossa\Cache\Exception;
    
    class BadMethodCallException extends
        \Phossa\Shared\Exception\BadMethodCallException
    {
    }
  • Message

    MessageAbstract class is the base class for all Message classes in all phossa packages.

    • Define package related Message class

      Message class is used to convert message code into human-readable messages, and MUST define its own property $messages.

      <?php
      namespace Phossa\Cache\Message;
      
      use Phossa\Shared\Message\MessageAbstract;
      
      class Message extends MessageAbstract
      {
          // use current year_month_date_hour_minute
          const CACHE_MESSAGE     = 1512220901;
      
          // driver failed
          const CACHE_DRIVER_FAIL = 1512220902;
      
          protected static $messages = [
              self::CACHE_MESSAGE      => 'cache %s',
              self::CACHE_DRIVER_FAILT => 'cache driver %s failed',
          ];
      }
    • Message class usage

      Usually only Message::get() and Message::CONST_VALUE are used.

      use Phossa\Cache\Message\Message;
      ...
      // throw exception
      throw new Exception\RuntimeException(
          Message::get(Message::CACHE_DRIVER_FAIL, get_class($driver)),
          Message::CACHE_DRIVER_FAIL
      );
    • Message loader

      Used for loading different code to message mapping such as language files.

      namespace Phossa\Cache;
      
      use Phossa\Shared\Message\Loader\LanguageLoader;
      
      // set language to 'zh_CN'
      $langLoader = new LanguageLoader('zh_CN');
      
      // will load local `Message\Message.zh_CN.php` language file
      Message\Message::setLoader($langLoader);
      
      // print message in chinese
      echo Message\Message::get(
          Message::CACHE_MESSAGE, get_class($object)
      );
    • Message formatter

      Used for formatting messages for different devices such as HTML page.

      namespace Phossa\Cache;
      
      use Phossa\Shared\Message\Formatter\HtmlFormatter;
      
      // format message as HTML
      $formatter = new HtmlFormatter();
      
      Message\Message::setFormatter($formatter);
      
      // print as HTML
      echo Message\Message::get(
          Message::CACHE_MESSAGE, get_class($object)
      );
  • Pattern

    Commonly used patterns in Interface or Trait

    • StaticTrait

      Used to be included in a static class which can not extends StaticAbstract

      <?php
      namespace Phossa\MyPackage;
      
      class MyStaticClass extends SomeClass
      {
          use \Phossa\Shared\Pattern\StaticTrait;
          ...
      }
    • StaticAbstract

      Used to be extended by other classes.

      <?php
      namespace Phossa\MyPackage;
      
      class MyStaticClass extends \Phossa\Shared\Pattern\StaticAbstract
      {
          ...
      }
    • SingletonInterface and SingletonTrait

      Used to be included in a singleton class.

      <?php
      namespace Phossa\MyPackage;
      
      class MySingletonClass extends SomeClass
      {
          use \Phossa\Shared\Pattern\SingletonTrait;
          ...
      }

      Usage,

      $obj =  MySingletonClass::getInstance();

      This singleton implementation has a feature which allows singleton class to extended.

      <?php
      namespace Phossa\MyPackage;
      
      class MyNewSingletonClass extends MySingletonClass
      {
          ...
      }
    • ShareableInterface and ShareableTrait

      Multiple instances are allowed for Shareable, but only one global copy. Such as global event manager and lots of local event managers.

      <?php
      namespace Phossa\Event;
      
      use Phossa\Shared\Pattern\ShareableInterface;
      
      class EventManager implements ShareableInterface
      {
          use \Phossa\Shared\Pattern\ShareableTrait;
          ...
      }

      Usage,

      // global event manager instance
      $globalEM = EventManager::getShareable();
      
      // local event manager
      $localEM  = new EventManager();
      
      // this EM global ?
      if ($globalEM->isShareable()) {
          ...
      } else {
          ...
      }
  • Taggable

    Added TaggableInterface and TaggableTrait.

  • Support PHP 5.4+, PHP 7.0+, HHVM

  • PHP7 ready for return type declarations and argument type declarations.

  • PSR-1, PSR-2, PSR-4 compliant.

  • Decoupled packages can be used seperately without the framework.

Dependencies

PHP >= 5.4.0

License

MIT License