Commit c2003402 authored by deviantintegral's avatar deviantintegral

Symfony 2.1 update.

parent 63df91d2
......@@ -55,7 +55,7 @@ static public function load($classes, $cacheDir, $name, $autoReload, $adaptive =
$reload = false;
if ($autoReload) {
$metadata = $cacheDir.'/'.$name.$extension.'.meta';
if (!file_exists($metadata) || !file_exists($cache)) {
if (!is_file($metadata) || !is_file($cache)) {
$reload = true;
} else {
$time = filemtime($cache);
......@@ -65,7 +65,7 @@ static public function load($classes, $cacheDir, $name, $autoReload, $adaptive =
$reload = true;
} else {
foreach ($meta[0] as $resource) {
if (!file_exists($resource) || filemtime($resource) > $time) {
if (!is_file($resource) || filemtime($resource) > $time) {
$reload = true;
break;
......@@ -75,7 +75,7 @@ static public function load($classes, $cacheDir, $name, $autoReload, $adaptive =
}
}
if (!$reload && file_exists($cache)) {
if (!$reload && is_file($cache)) {
require_once $cache;
return;
......
<?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\ClassLoader;
/**
* ClassMapGenerator
*
* @author Gyula Sallai <salla016@gmail.com>
*/
class ClassMapGenerator
{
/**
* Generate a class map file
*
* @param array|string $dirs Directories or a single path to search in
* @param string $file The name of the class map file
*/
static public function dump($dirs, $file)
{
$dirs = (array) $dirs;
$maps = array();
foreach ($dirs as $dir) {
$maps = array_merge($maps, static::createMap($dir));
}
file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true)));
}
/**
* Iterate over all files in the given directory searching for classes
*
* @param Iterator|string $dir The directory to search in or an iterator
*
* @return array A class map array
*/
static public function createMap($dir)
{
if (is_string($dir)) {
$dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
}
$map = array();
foreach ($dir as $file) {
if (!$file->isFile()) {
continue;
}
$path = $file->getRealPath();
if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
continue;
}
$classes = self::findClasses($path);
foreach ($classes as $class) {
$map[$class] = $path;
}
}
return $map;
}
/**
* Extract the classes in the given file
*
* @param string $path The file to check
*
* @return array The found classes
*/
static private function findClasses($path)
{
$contents = file_get_contents($path);
$tokens = token_get_all($contents);
$T_TRAIT = version_compare(PHP_VERSION, '5.4', '<') ? -1 : T_TRAIT;
$classes = array();
$namespace = '';
for ($i = 0, $max = count($tokens); $i < $max; $i++) {
$token = $tokens[$i];
if (is_string($token)) {
continue;
}
$class = '';
switch ($token[0]) {
case T_NAMESPACE:
$namespace = '';
// If there is a namespace, extract it
while (($t = $tokens[++$i]) && is_array($t)) {
if (in_array($t[0], array(T_STRING, T_NS_SEPARATOR))) {
$namespace .= $t[1];
}
}
$namespace .= '\\';
break;
case T_CLASS:
case T_INTERFACE:
case $T_TRAIT:
// Find the classname
while (($t = $tokens[++$i]) && is_array($t)) {
if (T_STRING === $t[0]) {
$class .= $t[1];
} elseif ($class !== '' && T_WHITESPACE == $t[0]) {
break;
}
}
$classes[] = ltrim($namespace . $class, '\\');
break;
default:
break;
}
}
return $classes;
}
}
......@@ -38,6 +38,7 @@ static public function enable()
$loader->registerPrefixFallbacks($function[0]->getPrefixFallbacks());
$loader->registerNamespaces($function[0]->getNamespaces());
$loader->registerPrefixes($function[0]->getPrefixes());
$loader->useIncludePath($function[0]->getUseIncludePath());
$function[0] = $loader;
}
......
Copyright (c) 2004-2011 Fabien Potencier
Copyright (c) 2004-2012 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
ClassLoader Component
=====================
ClassLoader loads your project classes automatically if they follow some
standard PHP conventions.
The Universal ClassLoader is able to autoload classes that implement the PSR-0
standard or the PEAR naming convention.
First, register the autoloader:
require_once __DIR__.'/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->register();
Then, register some namespaces with the `registerNamespace()` method:
$loader->registerNamespace('Symfony', __DIR__.'/src');
$loader->registerNamespace('Monolog', __DIR__.'/vendor/monolog/src');
The `registerNamespace()` method takes a namespace prefix and a path where to
look for the classes as arguments.
You can also register a sub-namespaces:
$loader->registerNamespace('Doctrine\\Common', __DIR__.'/vendor/doctrine-common/lib');
The order of registration is significant and the first registered namespace
takes precedence over later registered one.
You can also register more than one path for a given namespace:
$loader->registerNamespace('Symfony', array(__DIR__.'/src', __DIR__.'/symfony/src'));
Alternatively, you can use the `registerNamespaces()` method to register more
than one namespace at once:
$loader->registerNamespaces(array(
'Symfony' => array(__DIR__.'/src', __DIR__.'/symfony/src'),
'Doctrine\\Common' => __DIR__.'/vendor/doctrine-common/lib',
'Doctrine' => __DIR__.'/vendor/doctrine/lib',
'Monolog' => __DIR__.'/vendor/monolog/src',
));
For better performance, you can use the APC based version of the universal
class loader:
require_once __DIR__.'/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
require_once __DIR__.'/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';
use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
$loader = new ApcUniversalClassLoader('apc.prefix.');
Furthermore, the component provides tools to aggregate classes into a single
file, which is especially useful to improve performance on servers that do not
provide byte caches.
......@@ -41,6 +41,10 @@
* 'Swift_' => __DIR__.'/Swift',
* ));
*
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->useIncludePath(true);
*
* // activate the autoloader
* $loader->register();
*
......@@ -60,6 +64,29 @@ class UniversalClassLoader
private $prefixes = array();
private $namespaceFallbacks = array();
private $prefixFallbacks = array();
private $useIncludePath = false;
/**
* Turns on searching the include for class files. Allows easy loading
* of installed PEAR packages
*
* @param Boolean $useIncludePath
*/
public function useIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return Boolean
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Gets the configured namespaces.
......@@ -114,7 +141,17 @@ public function registerNamespaceFallbacks(array $dirs)
}
/**
* Registers the directory to use as a fallback for class prefixes.
* Registers a directory to use as a fallback for namespaces.
*
* @param string $dir A directory
*/
public function registerNamespaceFallback($dir)
{
$this->namespaceFallbacks[] = $dir;
}
/**
* Registers directories to use as a fallback for class prefixes.
*
* @param array $dirs An array of directories
*
......@@ -125,6 +162,16 @@ public function registerPrefixFallbacks(array $dirs)
$this->prefixFallbacks = $dirs;
}
/**
* Registers a directory to use as a fallback for class prefixes.
*
* @param string $dir A directory
*/
public function registerPrefixFallback($dir)
{
$this->prefixFallbacks[] = $dir;
}
/**
* Registers an array of namespaces
*
......@@ -219,47 +266,54 @@ public function findFile($class)
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$namespace = substr($class, 0, $pos);
$className = substr($class, $pos + 1);
$normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
foreach ($this->namespaces as $ns => $dirs) {
if (0 !== strpos($namespace, $ns)) {
continue;
}
foreach ($dirs as $dir) {
$className = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
if (file_exists($file)) {
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
if (is_file($file)) {
return $file;
}
}
}
foreach ($this->namespaceFallbacks as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
if (is_file($file)) {
return $file;
}
}
} else {
// PEAR-like class name
$normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
foreach ($this->prefixes as $prefix => $dirs) {
if (0 !== strpos($class, $prefix)) {
continue;
}
foreach ($dirs as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
if (is_file($file)) {
return $file;
}
}
}
foreach ($this->prefixFallbacks as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
if (is_file($file)) {
return $file;
}
}
}
if ($this->useIncludePath && $file = stream_resolve_include_path($normalizedClass)) {
return $file;
}
}
}
......@@ -21,5 +21,10 @@
"autoload": {
"psr-0": { "Symfony\\Component\\ClassLoader": "" }
},
"target-dir": "Symfony/Component/ClassLoader"
"target-dir": "Symfony/Component/ClassLoader",
"extra": {
"branch-alias": {
"dev-master": "2.1-dev"
}
}
}
......@@ -39,10 +39,6 @@ class EventDispatcher implements EventDispatcherInterface
*/
public function dispatch($eventName, Event $event = null)
{
if (!isset($this->listeners[$eventName])) {
return;
}
if (null === $event) {
$event = new Event();
}
......@@ -50,7 +46,13 @@ public function dispatch($eventName, Event $event = null)
$event->setDispatcher($this);
$event->setName($eventName);
if (!isset($this->listeners[$eventName])) {
return $event;
}
$this->doDispatch($this->getListeners($eventName), $eventName, $event);
return $event;
}
/**
......
......@@ -28,8 +28,10 @@ interface EventDispatcherInterface
* @param string $eventName The name of the event to dispatch. The name of
* the event is the name of the method that is
* invoked on listeners.
* @param Event $event The event to pass to the event handlers/listeners.
* If not supplied, an empty Event instance is created.
* @param Event $event The event to pass to the event handlers/listeners.
* If not supplied, an empty Event instance is created.
*
* @return Event
*
* @api
*/
......@@ -48,7 +50,9 @@ function dispatch($eventName, Event $event = null);
function addListener($eventName, $listener, $priority = 0);
/**
* Adds an event subscriber. The subscriber is asked for all the events he is
* Adds an event subscriber.
*
* The subscriber is asked for all the events he is
* interested in and added as a listener for these events.
*
* @param EventSubscriberInterface $subscriber The subscriber.
......@@ -60,35 +64,33 @@ function addSubscriber(EventSubscriberInterface $subscriber);
/**
* Removes an event listener from the specified events.
*
* @param string|array $eventName The event(s) to remove a listener from.
* @param object $listener The listener object to remove.
* @param string|array $eventName The event(s) to remove a listener from
* @param callable $listener The listener to remove
*/
function removeListener($eventName, $listener);
/**
* Removes an event subscriber.
*
* @param EventSubscriberInterface $subscriber The subscriber.
* @param EventSubscriberInterface $subscriber The subscriber
*/
function removeSubscriber(EventSubscriberInterface $subscriber);
/**
* Gets the listeners of a specific event or all listeners.
*
* @param string $eventName The name of the event.
* @param string $eventName The name of the event
*
* @return array The event listeners for the specified event, or all event
* listeners by event name.
* @return array The event listeners for the specified event, or all event listeners by event name
*/
function getListeners($eventName = null);
/**
* Checks whether an event has any registered listeners.
*
* @param string $eventName The name of the event.
* @param string $eventName The name of the event
*
* @return Boolean TRUE if the specified event has any listeners, FALSE
* otherwise.
* @return Boolean true if the specified event has any listeners, false otherwise
*/
function hasListeners($eventName = null);
}
Copyright (c) 2004-2011 Fabien Potencier
Copyright (c) 2004-2012 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
......@@ -4,7 +4,6 @@
"description": "Symfony EventDispatcher Component",
"keywords": [],
"homepage": "http://symfony.com",
"version": "2.1.0",
"license": "MIT",
"authors": [
{
......@@ -22,5 +21,10 @@
"autoload": {
"psr-0": { "Symfony\\Component\\EventDispatcher": "" }
},
"target-dir": "Symfony/Component/EventDispatcher"
"target-dir": "Symfony/Component/EventDispatcher",
"extra": {
"branch-alias": {
"dev-master": "2.1-dev"
}
}
}
<?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\HttpFoundation\File\MimeType;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
/**
* Guesses the mime type using the PHP function mime_content_type().
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
*/
class ContentTypeMimeTypeGuesser implements MimeTypeGuesserInterface
{
/**
* Returns whether this guesser is supported on the current OS/PHP setup
*
* @return Boolean
*/
static public function isSupported()
{
return function_exists('mime_content_type');
}
/**
* Guesses the mime type of the file with the given path
*
* @see MimeTypeGuesserInterface::guess()
*/
public function guess($path)
{
if (!is_file($path)) {
throw new FileNotFoundException($path);
}
if (!is_readable($path)) {
throw new AccessDeniedException($path);
}
if (!self::isSupported()) {
return null;
}
$type = mime_content_type($path);
// remove charset (added as of PHP 5.3)
if (false !== $pos = strpos($type, ';')) {
$type = substr($type, 0, $pos);
}
return $type;
}
}
<?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\HttpFoundation\File\MimeType;
/**
* A singleton mime type to file extension guesser.
*
* A default guesser is provided.
* You can register custom guessers by calling the register()
* method on the singleton instance.
*
* <code>
* $guesser = ExtensionGuesser::getInstance();
* $guesser->register(new MyCustomExtensionGuesser());
* </code>
*
* The last registered guesser is preferred over previously registered ones.
*
*/
class ExtensionGuesser implements ExtensionGuesserInterface
{
/**
* The singleton instance
* @var ExtensionGuesser
*/
static private $instance = null;
/**
* All registered ExtensionGuesserInterface instances
* @var array
*/
protected $guessers = array();
/**
* Returns the singleton instance
*
* @return ExtensionGuesser
*/
static public function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Registers all natively provided extension guessers
*/
private function __construct()
{
$this->register(new MimeTypeExtensionGuesser());
}
/**
* Registers a new extension guesser
*
* When guessing, this guesser is preferred over previously registered ones.
*
* @param ExtensionGuesserInterface $guesser
*/
public function register(ExtensionGuesserInterface $guesser)
{
array_unshift($this->guessers, $guesser);
}
/**
* Tries to guess the extension
*
* The mime type is passed to each registered mime type guesser in reverse order
* of their registration (last registered is queried first). Once a guesser
* returns a value that is not NULL, this method terminates and returns the
* value.
*
* @param string $mimeType The mime type
* @return string The guessed extension or NULL, if none could be guessed
*/
public function guess($mimeType)
{
foreach ($this->guessers as $guesser) {
$extension = $guesser->guess($mimeType);