Created
March 18, 2015 19:54
-
-
Save mkoert/02363848484e337a613c to your computer and use it in GitHub Desktop.
Modified CallbackResolver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the Silex framework. | |
* | |
* (c) Fabien Potencier <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace App\Component; | |
class CallbackResolver | |
{ | |
const SERVICE_PATTERN = "/[A-Za-z0-9\._\-]+:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/"; | |
private $container; | |
public function __construct(\Pimple\Container $container) | |
{ | |
$this->container = $container; | |
} | |
/** | |
* Returns true if the string is a valid service method representation. | |
* | |
* @param string $name | |
* | |
* @return bool | |
*/ | |
public function isValid($name) | |
{ | |
return is_string($name) && preg_match(static::SERVICE_PATTERN, $name); | |
} | |
/** | |
* Returns a callable given its string representation. | |
* | |
* @param string $name | |
* | |
* @return array A callable array | |
* | |
* @throws \InvalidArgumentException In case the method does not exist. | |
*/ | |
public function convertCallback($name) | |
{ | |
list($service, $method) = explode(':', $name, 2); | |
if (!isset($this->container[$service])) { | |
throw new \InvalidArgumentException(sprintf('Service "%s" does not exist.', $service)); | |
} | |
return array($this->container[$service], $method); | |
} | |
/** | |
* Returns a callable given its string representation if it is a valid service method. | |
* | |
* @param string $name | |
* | |
* @return array A callable array | |
* | |
* @throws \InvalidArgumentException In case the method does not exist. | |
*/ | |
public function resolveCallback($name) | |
{ | |
return $this->isValid($name) ? $this->convertCallback($name) : $name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment