apishka/easy-extend

Easy extends class implementation for PHP

Installs: 1 906

Dependents: 11

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 1

Open Issues: 0

Type:composer-plugin

2.1.1 2019-11-25 10:59 UTC

README

Current build status image

What is easy extend?

This is composer component which helps developers to write common libraries and extend them in project.

Quick start

  • Add easy-extend to composer requirements of your library
"require": {
    "apishka/easy-extend": "^1.0.0"
}
  • Create .apishka.php (structure documentation). Most common file looks like this:
<?php

return array(
    'easy-extend' => array(
        'finder' => function($finder)
        {
            $finder
                ->in('source/')
                ->files()
                ->name('*.php')
            ;

            return $finder;
        }
    ),
);
  • Run composer update
  • Implement traits to classes which you want to be easy extendable.

How to extend?

Extending classes

Some library has basic implementation of class you want extend and this library implements EasyExtend:

class Library_User_Implementation
{
    /**
     * We have to include trait
     */
    use Apishka\EasyExtend\Helper\ByClassNameTrait;
}

One thing you have to do to extend this class and don't rewrite any library code, or check where new user class should pass is to extends that class:

class My_Library_User_Implementation extends Library_User_Implementation
{
}

One thing you have to do is create new instance of class using

Library_User_Implementation::apishka(); // instanceof My_Library_User_Implementation

All libraries can be easy extended for you custom project with your custom requirements. No need to require tons of code.

##Items routing Create router and realize two methods:

class My_Library_ItemsRouter extends \Apishka\EasyExtend\Router\ByKeyAbstract
{
    protected function isCorrectItem(\ReflectionClass $reflector)
    {
        return $reflector->isSubclassOf('My_Library_ItemsInterface');
    }

    protected function getClassVariants(\ReflectionClass $reflector, $item)
    {
        return $item->getAliases();
    }
}

Create item:

class My_Library_ItemApple implements My_Library_ItemsInterface
{
    public function getAliases()
    {
        return array(
            'apple',
            'red_apple',
        );
    }
}

Now you can get class by alias:

var_dump(
    get_class(My_Library_ItemsRouter::apishka()->getItem('apple')
); // My_Library_ItemApple

var_dump(
    get_class(My_Library_ItemsRouter::apishka()->getItem('red_apple')
); // My_Library_ItemApple

Also you can extend your item:

class My_Library_ItemRedApple extends My_Library_ItemApple
{
}

// after ./vendor/bin/easy-extend
var_dump(
    get_class(My_Library_ItemsRouter::apishka()->getItem('apple')
); // My_Library_ItemRedApple