phossa2/shared

The shared library for other phossa2 libraries

2.0.28 2017-01-08 06:32 UTC

README

Build Status Code Quality Code Climate PHP 7 ready HHVM Latest Stable Version License

phossa2/shared is the shared library for other phossa2 libraries.

It requires PHP 5.4, supports PHP 7.0+ and HHVM. It is compliant with PSR-1, PSR-2, PSR-4.

Installation

Install via the composer utility.

composer require "phossa2/shared=2.*"

or add the following lines to your composer.json

{
    "require": {
       "phossa2/shared": "2.*"
    }
}

Features

  • Exception

    All phossa2 exceptions implement Phossa2\Shared\Exception\ExceptionInterface.

    To implment phossa2 exception interface,

    <?php
    namespace Phossa2\Cache\Exception;
    
    use Phossa2\Shared\Exception\ExceptionInterface;
    
    class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
    {
    }
  • Message

    Phossa2\Shared\Message\Message class is the base class for all message classes in all phossa2 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 Phossa2\Cache\Message;
      
      use Phossa2\Shared\Message\Message;
      
      class CacheMessage extends Message
      {
          // 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',
          ];
      }
    • Using message class

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

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

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

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

      Used for formatting messages for different devices such as HTML page. Formatter is shared among all siblings of Phossa2\Shared\Message\Message

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

    Some useful utilities here.

    • ClassNameTrait

      PHP 5.4 has ::class feature missing. ClassNameTrait provides three static methods ::getClassName(), ::getShortName(), ::getNameSpace()

    • StaticAbstract

      Used to be extended by other STATIC classes.

      <?php
      namespace Phossa2\MyPackage;
      
      class MyStaticClass extends \Phossa2\Shared\Base\StaticAbstract
      {
          ...
      }
  • *Trait

    Some useful traits in the Aware/ directory

    • TagAwareTrait adding tag support
  • Interface

    Some useful interfaces are in the Contract/ directory.

    • ArrayableInterface
  • 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.

Change log

Please see CHANGELOG from more information.

Testing

$ composer test

Contributing

Please see CONTRIBUTE for more information.

Dependencies

  • PHP >= 5.4.0

License

MIT License