Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Created July 29, 2016 03:22
Show Gist options
  • Select an option

  • Save mdmunir/c46bb362267a060daca3311de3320ea4 to your computer and use it in GitHub Desktop.

Select an option

Save mdmunir/c46bb362267a060daca3311de3320ea4 to your computer and use it in GitHub Desktop.
Facade Emulator
<?php
namespace app\classes;
/**
* Description of Db
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class Db extends Facade
{
}
<?php
namespace app\classes;
use yii\helpers\StringHelper;
/**
* Description of Facade
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class Facade
{
private static $_providers = [];
/**
* @return \yii\base\Object
*/
protected static function provider()
{
return \Yii::$app->get(lcfirst(StringHelper::basename(get_called_class())));
}
public static function __callStatic($name, $arguments)
{
$instance = static::instance();
return call_user_func_array([$instance, $name], $arguments);
}
public static function instance()
{
$class = get_called_class();
if (isset(self::$_providers[$class])) {
return self::$_providers[$class];
} else {
return self::$_providers[$class] = static::provider();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment