2017 © Pedro Peláez
 

library std-lib

Webiny StdLib Component

image

webiny/std-lib

Webiny StdLib Component

  • Friday, September 29, 2017
  • by SvenAlHamad
  • Repository
  • 3 Watchers
  • 8 Stars
  • 9,075 Installations
  • PHP
  • 22 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 24 Versions
  • 8 % Grown

The README.md

StdLib

The standard library is a set of core Webiny framework components that make coding a bit more pleasurable when working with some low level objects like strings, array, date time and url., (*1)

Install the component

The best way to install the component is using Composer., (*2)

composer require webiny/std-lib

For additional versions of the package, visit the Packagist page., (*3)

About

The standard library consists of following libraries: - Exception - [StdObject] (StdObject), (*4)

.. and following helper traits: - ComponentTrait - FactoryLoaderTrait - SingletonTrait - ValidatorTrait - StdLibTrait - StdObjectTrait, (*5)

ComponentTrait

This trait is used when creating a new Webiny Framework component. By implementing this trait you automatically get the possibility to register the required class loader libraries, services and additional parameters for your new component. The trait is based on two main methods setConfig and getConfig, both methods are defined as public static., (*6)

Here is an example Webiny Framework component that uses the ComponentTrait., (*7)

$crypy = \Webiny\Component\Crypt::setConfig('ExampleConfig.yaml');

The ExampleConfig.yaml file is defined like this:, (*8)

Crypt:
    Services:
        Password:
            Class: \Webiny\Component\Crypt\Crypt
            Arguments: [Blowfish, CCM, rijndael-128, _FOO_VECTOR]
        Cookie:
            Class: \Webiny\Component\Crypt\Crypt
            Arguments: [Blowfish, CCM, rijndael-128, _FOO_VECTOR]
    Bridge: \Webiny\Component\Crypt\Bridge\CryptLib\CryptLib
    ClassLoader:
        CryptLib: /var/www/Vendors/PHP-CryptLib/lib/CryptLib

There are couple of things you should notice: - the root of yaml file matches the class name (Crypt) and the class name matches the root component namespace \Webiny\Component\Crypt - the Services definition is based on rules defined by [ServiceManager)(../ServiceManager) component and all services defined in the yaml file are automatically registered with the ServiceManager under the name ComponentName.ServiceName in this example that would be Crypt.Password and Crypt.Cookie - the Bridge is an optional parameter. You can define as many optional parameters as you want - the ClassLoader parameter is an array that contains the namespace as key and path as a value. All the definitions under the ClassLoader parameter are automatically assigned to Webiny Framework ClassLoader component, (*9)

So basically as you see, the ComponentTrait does a lot of handy stuff and makes Webiny Framework components very neatly organised., (*10)

Callback

If you want to know when the configuration file on your class has been parsed, and all the services and class loader paths have been assigned, you can just define a protected static function _postSetConfig inside your component class, and the ComponentTrait will automatically call it each time you define a configuration for that class., (*11)

FactoryLoaderTrait

A handy trait when you want to load some classes where the class name is defined as a string or inside a variable and you want to pass along some parameters to the constructor. You can also pass a interface or a class name that the factory class must implement/extend. This trait will first construct the object, with the given parameters, and then it will verify its instance against the given interface or a class. If everything matches, the class instance is returned, otherwise, and Exception is thrown., (*12)

class MyClass
{
    use FactoryTrait;

    function myMethod()
    {
        // let's say we have a class we want to construct
        // the class has a constructor defined like this __construct($param1, $param2)
        // the class should implement \Webiny\Crypt\Bridge\CryptInterface
        $class = '\SomeVendor\SomeLib\SomeClass';

        try {
            $classInstance = $this->factory($class, '\Webiny\Crypt\Bridge\CryptInterface', ['foo1', 'foo2']);
        } catch (\Webiny\StdLib\Exception\Exception $e) {
            throw $e; // the class probably doesn't implement the required interface
        }
    }
}

SingletonTrait

The SingletonTrait is used on classes that must implement singleton pattern. You just use the trait on the class you want to be singleton, and that's it., (*13)

There are two methods that you can optionally implement in your class, public function init() and protected function _init(), they are called only once and at the moment when you request a singleton instance., (*14)

class MyClass
{
    use SingletonTrait;

    protected function __init()
    {
        // this will be called only once
        $this->__somePrivateMethod();
    }
}

To use your class you just call the static getInstance method that is implemented by the trait., (*15)

$instance = MyClass::getInstance(); // this calls the internal __init method, but only the first time, when it creates the instance

StdLibTrait

Also a helper trait with some commonly used functions. The trait itself only supports a limited number of methods, but we plan to expand it with more, so feel free to give suggestions., (*16)

This trait, not only that it defines helper methods, but it also uses ValidatorTrait and StdObjectTrait traits., (*17)

StdObjectTrait

This trait provides helper functions when working with StdObject library., (*18)

class MyClass
{
    use StdObjectTrait;

    public function test()
    {
        // create StringObject
        $this->str('This is a string');

        // create ArrayObject
        $this->arr(['one', 'two']);

        // create UrlObject
        $this->url('http://www.webiny.com/');

        // create DateTimeObject
        $this->dateTime('now');
    }
}

ValidatorTrait

This is a helper trait with some common validation methods like isNull, isEmpty, isInstanceOf and many more. Just view the class, all the methods are documented and, mostly self-explanatory., (*19)

Resources

To run unit tests, you need to use the following command:, (*20)

$ cd path/to/Webiny/Component/StdLib/
$ composer.phar install
$ phpunit

The Versions

29/09 2017

dev-master

9999999-dev http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php ^7

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

29/09 2017

1.6.x-dev

1.6.9999999.9999999-dev http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php ^7

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

29/09 2017

v1.6.0

1.6.0.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php ^7

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

29/09 2017

v1.6.1

1.6.1.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php ^7

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

08/09 2017

1.5.x-dev

1.5.9999999.9999999-dev http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php ^7

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

08/09 2017

v1.5.0

1.5.0.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php ^7

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

21/08 2016

1.4.x-dev

1.4.9999999.9999999-dev http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

21/08 2016

v1.4.3

1.4.3.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

04/02 2016

v1.4.0

1.4.0.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

04/02 2016

v1.4.1

1.4.1.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

04/02 2016

v1.4.2

1.4.2.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

16/03 2015

1.3.x-dev

1.3.9999999.9999999-dev http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

16/03 2015

v1.3.0

1.3.0.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

05/02 2015

v1.2.2

1.2.2.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

05/02 2015

v1.2.3

1.2.3.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

05/02 2015

v1.2.1

1.2.1.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

04/02 2015

1.2.x-dev

1.2.9999999.9999999-dev http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

04/02 2015

v1.2.0

1.2.0.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

28/11 2014

v1.1.1

1.1.1.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

23/11 2014

1.1.x-dev

1.1.9999999.9999999-dev http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

23/11 2014

v1.1.0

1.1.0.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

11/11 2014

1.0.x-dev

1.0.9999999.9999999-dev http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

11/11 2014

v1.0.1

1.0.1.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton

20/09 2014

v1.0.0

1.0.0.0 http://www.webiny.com/

Webiny StdLib Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

array factory string datetime singleton