2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Routing\Loader ;
use Symfony\Component\Config\Loader\FileLoader ;
use Symfony\Component\Config\Resource\FileResource ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\Routing\RouteCollection ;
/**
* PhpFileLoader loads routes from a PHP file .
*
* The file must return a RouteCollection instance .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class PhpFileLoader extends FileLoader
{
/**
* Loads a PHP file .
*
* @ param string $file A PHP file path
* @ param string | null $type The resource type
*
* @ return RouteCollection A RouteCollection instance
*/
public function load ( $file , $type = null )
{
$path = $this -> locator -> locate ( $file );
2018-11-23 12:29:20 +00:00
$this -> setCurrentDir ( \dirname ( $path ));
// the closure forbids access to the private scope in the included file
$loader = $this ;
$load = \Closure :: bind ( function ( $file ) use ( $loader ) {
return include $file ;
}, null , ProtectedPhpFileLoader :: class );
$result = $load ( $path );
if ( $result instanceof \Closure ) {
$collection = new RouteCollection ();
$result ( new RoutingConfigurator ( $collection , $this , $path , $file ), $this );
} else {
$collection = $result ;
}
2015-08-17 17:00:26 -07:00
$collection -> addResource ( new FileResource ( $path ));
return $collection ;
}
/**
* { @ inheritdoc }
*/
public function supports ( $resource , $type = null )
{
2018-11-23 12:29:20 +00:00
return \is_string ( $resource ) && 'php' === pathinfo ( $resource , PATHINFO_EXTENSION ) && ( ! $type || 'php' === $type );
2015-08-17 17:00:26 -07:00
}
2018-11-23 12:29:20 +00:00
}
2015-08-17 17:00:26 -07:00
2018-11-23 12:29:20 +00:00
/**
* @ internal
*/
final class ProtectedPhpFileLoader extends PhpFileLoader
{
2015-08-17 17:00:26 -07:00
}