PHP Classes
elePHPant
Icontem

Packager: Implement Facades to call classes statically

Recommend this page to a friend!
  Info   View files Documentation   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-02-22 (16 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 59 This week: 2All time: 9,067 This week: 379Up
Version License PHP version Categories
packager 1.0.1GNU General Publi...5PHP 5, Design Patterns
Description Author

This package implement Facades to call classes statically.

It can register mappings of facade class names to the respective classes in their own name spaces.

The facade classes can be called statically and this package forwards the calls to the correct implementation class.

Innovation Award
PHP Programming Innovation award nominee
November 2016
Number 10


Prize: SourceGuarding PHP encoder tool
Facades are useful to provide interfaces to access packages with a common interface regardless of the actual classes that implement the desired functionality.

This package provides means call implementation classes in their own namespaces by registering facades in the current namespace that forward the calls to the implementation classes.

Manuel Lemos
  Performance   Level  
Name: Omar Andrés Barbosa ... <contact>
Classes: 3 packages by
Country: Colombia Colombia
Innovation award
Innovation award
Nominee: 2x

Details

Packager: This package allows calling classes in static style, through the use of facade classes.

Installation

To install via composer (http://getcomposer.org/), place the following in your composer.json file:

{
    "require": {
        "barbosa/packager": "dev-master"
    }
}

or download package from github.com:

http://github.com/barbosa89/packager

Configuration

Consider the following folder structure:

/project
    /src
        MainClass.php
        Router.php
        TextProcessing.php
        Request.php
        services.php
        /Facades
            Router.php
            TextProcessing.php
            Request.php

The services.php file, returns an array with an alias and the corresponding namespace and the following sintax:


<?php 

/
 * file: services.php
 */

return [

    'router' => Some\Namespace\Router::class,
    'text' => Some\Namespace\TextProcessing::class,
    'request' => Some\Namespace\Request::class  

];

In the MainClass.php file, invoke to AliasLoader::setAliases method:


<?php

namespace Some\Namespace;

use Barbosa\Packager\AliasLoader;

class MainClass
{
    public function __construct
    {
        $aliases = require 'services.php';
        AliasLoader::setAliases($aliases);
    }
}

The AliasLoader::setAliases method used to record the namespaces and can be called from anywhere in the application.

The facades classes must be created in the folder Facades:

<?php

namespace Some\Namespace\Facade;

use Barbosa\Packager\AccessFacade;
use Barbosa\Packager\FacadeInterface;

class Router extends AccessFacade implements FacadeInterface
{
    public static function getServiceName()
    {
        return 'router';
    }
}

For each service or class, it must be created a facade.

Now you can invoke static style services from anywhere in the application, just by using the namespace of the facade. Example:

<?php

namespace Some\Namespace;

use Barbosa\Packager\AliasLoader;
use Some\Namespace\Facades\Request

class MainClass
{
    public function __construct
    {
        $aliases = require 'services.php';
        AliasLoader::setAliases($aliases);
    }

    public function resolveUri($uri)
    {
        return Request::parseUri($uri);
    }
}

Credits

  • www.sitepoint.com
  • Inspired by Laravel: Facade class

Contribute

  1. Check for open issues or open a new issue to start a discussion around a bug or feature.
  2. Fork the repository on GitHub to start making your changes.
  3. Write one or more tests for the new feature or that expose the bug.
  4. Make code changes to implement the feature or fix the bug.
  5. Send a pull request to get your changes merged and published.

Thanks...

Omar Andrés Barbosa

  Files folder image Files  
File Role Description
Files folder imagesrc (4 files)
Files folder imagetests (2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONDUCT.md Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file AccessFacade.php Class Class source
  Plain text file AliasLoader.php Class Class source
  Plain text file FacadeInterface.php Class Class source
  Plain text file ServicesContainer.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageSimulations (2 files)
Files folder imageUnit (1 file)

  Files folder image Files  /  tests  /  Simulations  
File Role Description
  Plain text file Request.php Class Class source
  Plain text file Router.php Class Class source

  Files folder image Files  /  tests  /  Unit  
File Role Description
  Plain text file ServicesContainerTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:59
This week:2
All time:9,067
This week:379Up