Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Laravel Facades with Statical library #8

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ xcuserdata
# svn & cvs
.svn
CVS

# dev dependencies
vendor/
*.lock
80 changes: 54 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Route::get('/hello-world', function()
})
```

This library is based on the Laravel 4 Facade class, more info about facades in the [Laravel documentation](http://laravel.com/docs/facades).
This library uses [Statical](https://github.com/johnstevenson/statical), which provides a static proxy interface
similar to [Laravel Facades](http://laravel.com/docs/facades) with enhanced features.

## Installation

Expand All @@ -32,19 +33,18 @@ To install latest version simply add it to your `composer.json`:
"itsgoingd/slim-facades": "dev-master"
```

Once the package is installed, you need to initialize the Facade class:
Once the package is installed, you need to initialize the StaticalManager class:

```php
require 'vendor/autoload.php';

use SlimFacades\Facade;
use SlimFacades\StaticalManager;

$app = new Slim\Slim();

// initialize the Facade class
// initialize the StaticalManager class

Facade::setFacadeApplication($app);
Facade::registerAliases();
$statical = new StaticalManager($app);

// now you can start using the facades

Expand All @@ -60,7 +60,7 @@ Route::get('/hello/:name', function($name)
App::run();
```

Following facades are available:
The following facades are available:

### App
- facade for Slim instance and following additional methods:
Expand Down Expand Up @@ -134,33 +134,61 @@ $output = View::render('world.html');
```

### Custom facades
You can create a custom facades by extending `SlimFacades\Facade` class.

Since the *StaticalManager* extends Statical, you have all the power that the library offers to create
your own facades. You can create a custom facade by creating a static class that extends
the `Statical\BaseProxy` class. It is usually empty:
```php
class MyFacade extends SlimFacades\Facade
class MyPayment extends \Statical\BaseProxy
{
// return the name of the component from the DI container
protected static function getFacadeAccessor() { return 'my_component'; }
}
```

You can register custom facades by passing the aliases to the _Facade::registerAliases()_ function.
You must then register your facade with the StaticalManager, using `addProxyService` if your object
has been registered with the Slim container, or `addProxyInstance` if you have an object instance or closure.

```php
Facade::registerAliases(array(
'App' => 'SlimFacades\App',
'Config' => 'SlimFacades\Config',
'Input' => 'SlimFacades\Input',
// 'Log' => 'SlimFacades\Log',
'Log' => 'CustomLogFacade',
'Request' => 'SlimFacades\Request',
'Response' => 'SlimFacades\Response',
'Route' => 'SlimFacades\Route',
'View' => 'SlimFacades\View',
));
// The StaticalManager class needs to be initialized
$app = new Slim\Slim();
$statical = new StaticalManager($app);
...

$alias = 'Payment'; // The class we call
$proxy = 'MyPayment'; // The facade class
$id = 'payment'; // The id of our payment service in the Slim container

$statical->addProxyService($alias, $proxy, $id);
// or Statical::addProxyService
...

Payment::process();
```
If you need to add methods to your facade that require the Slim application, you can extend the
`SlimFacades\StaticalProxy` class instead and use its `static::$slim` property.

Note that the StaticalManager creates a facade to itself. This means you can call it using
`Statical::method()` notation.

### Namespaces

Note that calling _Facade::registerAliases()_ with a list of aliases will register ONLY the specified facades, if you want to register default facades as well as custom ones, you can call the function two times, with and without the array argument.

By default, each facade is registered in the global namespace. This means that any calls to `App` will not work
in a namespace unless prefixed with a backslash `\App` or there is a use statement referencing the facade class,
for example `use SlimFacades\App`. You can make use of Statical's namespacing feature to add namespace patterns
for any facade.

```php
$app = new Slim\Slim();
$statical = new StaticalManager($app);

$alias = '*';
$namespace = 'App\\Library\\*'
Statical::addNamespace($alias, $namespace);
// Any facade can now be called in any App\Library... namespace

$alias = '*'; // any alias
$namespace = '*' // any namespace
Statical::addNamespace($alias, $namespace);
// Any facade can now be called in any namespace
```

## Licence

Expand Down
6 changes: 2 additions & 4 deletions SlimFacades/App.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php
namespace SlimFacades;

class App extends Facade
class App extends StaticalProxy
{
protected static function getFacadeAccessor() { return self::$slim; }

public static function make($key)
{
return self::$app[$key];
return static::$slim[$key];
}
}
8 changes: 3 additions & 5 deletions SlimFacades/Config.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php
namespace SlimFacades;

class Config extends Facade
class Config extends StaticalProxy
{
protected static function getFacadeAccessor() { return self::$slim; }

public static function get($key)
{
return self::$slim->config($key);
return static::$slim->config($key);
}

public static function set($key, $value)
{
return self::$slim->config($key, $value);
return static::$slim->config($key, $value);
}
}
37 changes: 0 additions & 37 deletions SlimFacades/Facade.php

This file was deleted.

4 changes: 1 addition & 3 deletions SlimFacades/Input.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php
namespace SlimFacades;

class Input extends Facade
class Input extends StaticalProxy
{
protected static function getFacadeAccessor() { return 'request'; }

public static function file($name)
{
return isset($_FILES[$name]) && $_FILES[$name]['size'] ? $_FILES[$name] : null;
Expand Down
3 changes: 1 addition & 2 deletions SlimFacades/Log.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace SlimFacades;

class Log extends Facade
class Log extends StaticalProxy
{
protected static function getFacadeAccessor() { return 'log'; }
}
3 changes: 1 addition & 2 deletions SlimFacades/Request.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace SlimFacades;

class Request extends Facade
class Request extends StaticalProxy
{
protected static function getFacadeAccessor() { return 'request'; }
}
3 changes: 1 addition & 2 deletions SlimFacades/Response.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace SlimFacades;

class Response extends Facade
class Response extends StaticalProxy
{
protected static function getFacadeAccessor() { return 'response'; }
}
22 changes: 10 additions & 12 deletions SlimFacades/Route.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
<?php
namespace SlimFacades;

class Route extends Facade
class Route extends StaticalProxy
{
protected static function getFacadeAccessor() { return 'router'; }

public static function map()
{
return call_user_func_array(array(self::$slim, 'map'), func_get_args());
return call_user_func_array(array(static::$slim, 'map'), func_get_args());
}

public static function get()
{
return call_user_func_array(array(self::$slim, 'get'), func_get_args());
return call_user_func_array(array(static::$slim, 'get'), func_get_args());
}

public static function post()
{
return call_user_func_array(array(self::$slim, 'post'), func_get_args());
return call_user_func_array(array(static::$slim, 'post'), func_get_args());
}

public static function put()
{
return call_user_func_array(array(self::$slim, 'put'), func_get_args());
return call_user_func_array(array(static::$slim, 'put'), func_get_args());
}

public static function patch()
{
return call_user_func_array(array(self::$slim, 'patch'), func_get_args());
return call_user_func_array(array(static::$slim, 'patch'), func_get_args());
}

public static function delete()
{
return call_user_func_array(array(self::$slim, 'delete'), func_get_args());
return call_user_func_array(array(static::$slim, 'delete'), func_get_args());
}

public static function options()
{
return call_user_func_array(array(self::$slim, 'options'), func_get_args());
return call_user_func_array(array(static::$slim, 'options'), func_get_args());
}

public static function group()
{
return call_user_func_array(array(self::$slim, 'group'), func_get_args());
return call_user_func_array(array(static::$slim, 'group'), func_get_args());
}

public static function any()
{
return call_user_func_array(array(self::$slim, 'any'), func_get_args());
return call_user_func_array(array(static::$slim, 'any'), func_get_args());
}
}
40 changes: 40 additions & 0 deletions SlimFacades/StaticalManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace SlimFacades;

use Statical\Manager;

class StaticalManager extends Manager
{
public function __construct($slim)
{
// Call parent constructor with Slim container accessor
parent::__construct(array($slim, '__get'));

// Static proxy ourself - now we can use Statical::method
$this->addProxySelf();

// Set Slim application for proxies
StaticalProxy::$slim = $slim;

// Register proxies that use the Slim instance
foreach (array ('App', 'Config') as $alias) {
$proxy = 'SlimFacades\\' . $alias;
$this->addProxyInstance($alias, $proxy, $slim);
}

// Register services that are resolved out of the Slim container
$services = array(
'Input' => 'request',
'Log' => 'log',
'Request' => 'request',
'Response' => 'response',
'Route' => 'router',
'View' => 'view',
);

foreach ($services as $alias => $id) {
$proxy = 'SlimFacades\\' . $alias;
$this->addProxyService($alias, $proxy, $id);
}
}
}
7 changes: 7 additions & 0 deletions SlimFacades/StaticalProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace SlimFacades;

abstract class StaticalProxy extends \Statical\BaseProxy
{
public static $slim;
}
3 changes: 1 addition & 2 deletions SlimFacades/View.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace SlimFacades;

class View extends Facade
class View extends \Statical\BaseProxy
{
protected static function getFacadeAccessor() { return 'view'; }
}
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
"homepage": "http://twitter.com/itsgoingd"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/johnstevenson/statical"
}
],
"require": {
"php": ">=5.3.0",
"slim/slim": ">=2.3.0",
"illuminate/support": ">=4.0"
"statical/statical": "dev-master"
},
"autoload": {
"psr-0": {"SlimFacades": ""}
Expand Down
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<phpunit bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="SlimFacades Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>