2011-04-08 00:41:16 +02:00
|
|
|
<?php
|
|
|
|
/*
|
2011-07-18 22:43:12 +02:00
|
|
|
* This file is part of the BeSimpleSoapBundle.
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* (c) Christian Kerl <christian-kerl@web.de>
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
namespace BeSimple\SoapBundle\ServiceDefinition\Loader;
|
2011-04-08 00:41:16 +02:00
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition;
|
2011-04-08 00:41:16 +02:00
|
|
|
|
|
|
|
use Symfony\Component\Config\FileLocator;
|
2011-07-17 10:46:54 +02:00
|
|
|
use Symfony\Component\Config\Loader\FileLoader;
|
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2011-04-08 00:41:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AnnotationFileLoader loads ServiceDefinition from annotations set
|
|
|
|
* on a PHP class and its methods.
|
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* Based on \Symfony\Component\Routing\Loader\AnnotationFileLoader
|
|
|
|
*
|
|
|
|
* @author Christian Kerl <christian-kerl@web.de>
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
|
|
|
class AnnotationFileLoader extends FileLoader
|
|
|
|
{
|
|
|
|
protected $loader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param AnnotationClassLoader $loader An AnnotationClassLoader instance
|
|
|
|
* @param string|array $paths A path or an array of paths where to look for resources
|
|
|
|
*/
|
|
|
|
public function __construct(FileLocator $locator, AnnotationClassLoader $loader, $paths = array())
|
|
|
|
{
|
|
|
|
if (!function_exists('token_get_all')) {
|
|
|
|
throw new \RuntimeException('The Tokenizer extension is required for the routing annotation loaders.');
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($locator, $paths);
|
|
|
|
|
|
|
|
$this->loader = $loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads from annotations from a file.
|
|
|
|
*
|
|
|
|
* @param string $file A PHP file path
|
|
|
|
* @param string $type The resource type
|
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* @return ServiceDefinition A ServiceDefinition instance
|
2011-04-08 00:41:16 +02:00
|
|
|
*
|
2011-04-09 00:40:31 +02:00
|
|
|
* @throws \InvalidArgumentException When the file does not exist
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
|
|
|
public function load($file, $type = null)
|
|
|
|
{
|
|
|
|
$path = $this->locator->locate($file);
|
|
|
|
|
|
|
|
if ($class = $this->findClass($path)) {
|
2011-07-17 10:46:54 +02:00
|
|
|
return $definition = $this->loader->load($class, $type);
|
2011-04-08 00:41:16 +02:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
return null;
|
2011-04-08 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this class supports the given resource.
|
|
|
|
*
|
|
|
|
* @param mixed $resource A resource
|
|
|
|
* @param string $type The resource type
|
|
|
|
*
|
|
|
|
* @return Boolean True if this class supports the given resource, false otherwise
|
|
|
|
*/
|
|
|
|
public function supports($resource, $type = null)
|
|
|
|
{
|
|
|
|
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the full class name for the first class in the file.
|
|
|
|
*
|
|
|
|
* @param string $file A PHP file path
|
|
|
|
*
|
2011-07-14 17:45:03 +02:00
|
|
|
* @return string|false Full class name if found, false otherwise
|
2011-04-08 00:41:16 +02:00
|
|
|
*/
|
|
|
|
protected function findClass($file)
|
|
|
|
{
|
2011-07-17 10:46:54 +02:00
|
|
|
$class = false;
|
2011-04-08 00:41:16 +02:00
|
|
|
$namespace = false;
|
2011-07-17 10:46:54 +02:00
|
|
|
$tokens = token_get_all(file_get_contents($file));
|
2011-04-08 00:41:16 +02:00
|
|
|
while ($token = array_shift($tokens)) {
|
|
|
|
if (!is_array($token)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true === $class && T_STRING === $token[0]) {
|
|
|
|
return $namespace.'\\'.$token[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true === $namespace && T_STRING === $token[0]) {
|
|
|
|
$namespace = '';
|
|
|
|
do {
|
|
|
|
$namespace .= $token[1];
|
|
|
|
$token = array_shift($tokens);
|
|
|
|
} while ($tokens && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (T_CLASS === $token[0]) {
|
|
|
|
$class = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (T_NAMESPACE === $token[0]) {
|
|
|
|
$namespace = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|