Skip to content

Commit

Permalink
Improve Scope to add support external DI Container of your choice. #330
Browse files Browse the repository at this point in the history
  • Loading branch information
Arul- committed Jul 19, 2015
1 parent b5bc2dc commit 8f20077
Showing 1 changed file with 49 additions and 32 deletions.
81 changes: 49 additions & 32 deletions vendor/Luracast/Restler/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,53 @@ class Scope
public static $classAliases = array(

//Core
'Restler' => 'Luracast\Restler\Restler',
'Restler' => 'Luracast\Restler\Restler',

//Format classes
'AmfFormat' => 'Luracast\Restler\Format\AmfFormat',
'JsFormat' => 'Luracast\Restler\Format\JsFormat',
'JsonFormat' => 'Luracast\Restler\Format\JsonFormat',
'HtmlFormat' => 'Luracast\Restler\Format\HtmlFormat',
'PlistFormat' => 'Luracast\Restler\Format\PlistFormat',
'UploadFormat' => 'Luracast\Restler\Format\UploadFormat',
'UrlEncodedFormat' => 'Luracast\Restler\Format\UrlEncodedFormat',
'XmlFormat' => 'Luracast\Restler\Format\XmlFormat',
'YamlFormat' => 'Luracast\Restler\Format\YamlFormat',
'CsvFormat' => 'Luracast\Restler\Format\CsvFormat',
'TsvFormat' => 'Luracast\Restler\Format\TsvFormat',
'AmfFormat' => 'Luracast\Restler\Format\AmfFormat',
'JsFormat' => 'Luracast\Restler\Format\JsFormat',
'JsonFormat' => 'Luracast\Restler\Format\JsonFormat',
'HtmlFormat' => 'Luracast\Restler\Format\HtmlFormat',
'PlistFormat' => 'Luracast\Restler\Format\PlistFormat',
'UploadFormat' => 'Luracast\Restler\Format\UploadFormat',
'UrlEncodedFormat' => 'Luracast\Restler\Format\UrlEncodedFormat',
'XmlFormat' => 'Luracast\Restler\Format\XmlFormat',
'YamlFormat' => 'Luracast\Restler\Format\YamlFormat',
'CsvFormat' => 'Luracast\Restler\Format\CsvFormat',
'TsvFormat' => 'Luracast\Restler\Format\TsvFormat',

//Filter classes
'RateLimit' => 'Luracast\Restler\Filter\RateLimit',
'RateLimit' => 'Luracast\Restler\Filter\RateLimit',

//UI classes
'Forms' => 'Luracast\Restler\UI\Forms',
'Nav' => 'Luracast\Restler\UI\Nav',
'Emmet' => 'Luracast\Restler\UI\Emmet',
'T' => 'Luracast\Restler\UI\Tags',
'Forms' => 'Luracast\Restler\UI\Forms',
'Nav' => 'Luracast\Restler\UI\Nav',
'Emmet' => 'Luracast\Restler\UI\Emmet',
'T' => 'Luracast\Restler\UI\Tags',

//API classes
'Resources' => 'Luracast\Restler\Resources',
'Explorer' => 'Luracast\Restler\Explorer',
'Resources' => 'Luracast\Restler\Resources',
'Explorer' => 'Luracast\Restler\Explorer',

//Cache classes
'HumanReadableCache' => 'Luracast\Restler\HumanReadableCache',
'ApcCache' => 'Luracast\Restler\ApcCache',
'MemcacheCache' => 'Luracast\Restler\MemcacheCache',
'ApcCache' => 'Luracast\Restler\ApcCache',
'MemcacheCache' => 'Luracast\Restler\MemcacheCache',

//Utility classes
'Object' => 'Luracast\Restler\Data\Object',
'String' => 'Luracast\Restler\Data\String',
'Arr' => 'Luracast\Restler\Data\Arr',
'Object' => 'Luracast\Restler\Data\Object',
'String' => 'Luracast\Restler\Data\String',
'Arr' => 'Luracast\Restler\Data\Arr',

//Exception
'RestException' => 'Luracast\Restler\RestException'
'RestException' => 'Luracast\Restler\RestException'
);
/**
* @var null|Callable adding a resolver function that accepts
* the class name as the parameter and returns an instance of the class
* as a singleton. Allows the use of your favourite DI container
*/
public static $resolver = null;
public static $properties = array();
protected static $instances = array();
protected static $registry = array();
Expand All @@ -84,8 +90,18 @@ public static function get($name)
} elseif (!empty(static::$registry[$name])) {
$function = static::$registry[$name]->function;
$r = $function();
if (static::$registry[$name]->singleton)
if (static::$registry[$name]->singleton) {
static::$instances[$name] = (object)array('instance' => $r);
}
} elseif (is_callable(static::$resolver)) {
$fullName = $name;
if (isset(static::$classAliases[$name])) {
$fullName = static::$classAliases[$name];
}
/** @var Callable $function */
$function = static::$resolver;
$r = $function($fullName);
static::$instances[$name] = (object)array('instance' => $r);
} else {
$fullName = $name;
if (isset(static::$classAliases[$name])) {
Expand All @@ -102,10 +118,10 @@ public static function get($name)
$properties = Util::nestedValue(
$m, 'class', $fullName,
CommentParser::$embeddedDataName
) ? : (Util::nestedValue(
) ?: (Util::nestedValue(
$m, 'class', $shortName,
CommentParser::$embeddedDataName
) ? : array());
) ?: array());
} else {
static::$instances[$name]->initPending = true;
}
Expand All @@ -119,7 +135,7 @@ public static function get($name)
) {
static::$instances[$name]->authVerified = true;
$r->__setAuthenticationStatus
(static::get('Restler')->_authenticated);
(static::get('Restler')->_authenticated);
}
if (isset(static::$instances[$name]->initPending)) {
$m = Util::nestedValue(static::get('Restler'), 'apiMethodInfo', 'metadata');
Expand All @@ -128,17 +144,18 @@ public static function get($name)
$shortName = Util::getShortName($name);
} else {
$shortName = $name;
if (isset(static::$classAliases[$name]))
if (isset(static::$classAliases[$name])) {
$fullName = static::$classAliases[$name];
}
}
if ($m) {
$properties = Util::nestedValue(
$m, 'class', $fullName,
CommentParser::$embeddedDataName
) ? : (Util::nestedValue(
) ?: (Util::nestedValue(
$m, 'class', $shortName,
CommentParser::$embeddedDataName
) ? : array());
) ?: array());
unset(static::$instances[$name]->initPending);
$initialized = false;
}
Expand Down

0 comments on commit 8f20077

Please sign in to comment.