Giter Club home page Giter Club logo

restatic's Introduction

ReStatic

Build Status

Laravel like Facades (static proxies) for PSR11 containers.

class HomeController
{
    public function __invoke()
    {
        // It just works!
        return View::render('home.index', [
            'articles' => DB::query('SELECT * FROM articles')
        ]);
    }
}

Requirements

PHP 7.2+ or 8+

Usage

Setup Container

$container = new \DI\Container();
$container->set('db', function () {
    return new PDO('mysql:dbname=testdb;host=127.0.0.1', 'dbuser', 'dbpass');
});

Create Static Proxies

// app/Facades/DB.php

class DB extends StaticProxy
{
    public static function getInstanceIdentifier()
    {
        return 'db';
    }
}

Setup Facades

$proxyManager = new ProxyManager($container);
$proxyManager->addProxy('DB', DB::class);
$proxyManager->addProxy('View', View::class);
$proxyManager->enable(ProxyManager::ROOT_NAMESPACE_ANY);

Concepts

  • Static Proxy – Static class that proxies static method calls to instance methods on its Proxy Subject.
  • Proxy Subject (Instance) – An object instance, stored in a Container, that is linked to a Static Proxy.
  • Proxy Manager – Mediating object used to associate Static Proxies to an Alias Loader and Container.
  • Alias – A memorable class name used as an alias to a fully-qualified class name of a Static Proxy class.
  • Alias Loader – Maintainer of the associations between Aliases and Static Proxies. It is injected into the autoloader stack to handle Aliases as they are referenced.
  • Container – A IoC container (e.g., a Service Locator or DIC) that provides the Proxy Subject instances. It must implement the PSR-11 stardard's ContainerInterface.
  • Instance Identifier – An identifier used to fetch a Proxy Subject from a Container. Each Static Proxy must specify the Instance Identifier needed to get its Proxy Subject.
  • Root Namespace – The namespace that an Alias can be referenced in. This can be configured as the global namespace (default), a specific namespace, or any namespace (i.e., the Alias works from any namespace).

More

Inspiration

ReStatic is based on the awesome package XStatic created by Jeremy Lindblom.

Disclaimer

I would not consider myself to be for or against the use of static proxy interfaces (or Laravel's "Facades"), but I do think it is a fascinating and unique idea, and that it is very cool that you can write code this way and still have it work and be testable. I am curious to see if developers, especially library and framework developers, find ways to use, but not require, these static proxy interfaces in order to make their projects appeal to a wider range of PHP developers.
Jeremy Lindblom

restatic's People

Contributors

dlundgren avatar jeremeamia avatar lhsazevedo avatar pherrymason avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

dlundgren camkem

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.