Commit 1a23e6c9 authored by Fabien Potencier's avatar Fabien Potencier

removed the autoloader service (use composer instead, closes #205, closes #176)

parent 6dcb2587
......@@ -3,6 +3,10 @@ Changelog
This changelog references all backward incompatibilities as we introduce them:
* **2012-05-24**: Removed the ``autoloader`` service (use composer instead).
The ``*.class_path`` settings on all the built-in providers have also been
removed in favor of Composer.
* **2012-05-21**: Changed error() to allow handling specific exceptions.
* **2012-05-20**: Added a way to define settings on a controller collection.
......
......@@ -9,7 +9,6 @@ In order to do that, you will need to register translator and point to translate
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale' => 'sr_Latn',
'translation.class_path' => __DIR__ . '/vendor/symfony/src',
'translator.messages' => array()
));
$app->before(function () use ($app) {
......
......@@ -34,15 +34,6 @@ Conventions
You need to watch out in what order you do certain things when
interacting with providers. Just keep to these rules:
* Class paths (for the autoloader) must be defined **before**
the provider is registered. Passing it as a second argument
to ``Application::register`` qualifies too, because it sets
the passed parameters first.
*Reason: The provider will set up the autoloader at
provider register time. If the class path is not set
at that point, no autoloader can be registered.*
* Overriding existing services must occur **after** the
provider is registered.
......@@ -138,51 +129,6 @@ You can now use this provider as follows::
In this example we are getting the ``name`` parameter from the
query string, so the request path would have to be ``/hello?name=Fabien``.
Class loading
~~~~~~~~~~~~~
Providers are great for tying in external libraries as you
can see by looking at the ``MonologServiceProvider`` and
``TwigServiceProvider``. If the library is decent and follows the
`PSR-0 Naming Standard <http://groups.google.com/group/php-standards/web/psr-0-final-proposal>`_
or the PEAR Naming Convention, it is possible to autoload
classes using the ``UniversalClassLoader``.
As described in the *Services* chapter, there is an
*autoloader* service which can be used for this.
Here is an example of how to use it (based on `Buzz <https://github.com/kriswallsmith/Buzz>`_)::
namespace Acme;
use Silex\Application;
use Silex\ServiceProviderInterface;
class BuzzServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app['buzz'] = $app->share(function () { ... });
if (isset($app['buzz.class_path'])) {
$app['autoloader']->registerNamespace('Buzz', $app['buzz.class_path']);
}
}
}
This allows you to simply provide the class path as an
option when registering the provider::
$app->register(new BuzzServiceProvider(), array(
'buzz.class_path' => __DIR__.'/vendor/buzz/lib',
));
.. note::
For libraries that do not use PHP 5.3 namespaces you can use ``registerPrefix``
instead of ``registerNamespace``, which will use an underscore as directory
delimiter.
Controllers providers
---------------------
......
......@@ -35,12 +35,6 @@ Parameters
These and additional options are described in detail in the `Doctrine DBAL
configuration documentation <http://www.doctrine-project.org/docs/dbal/2.0/en/reference/configuration.html>`_.
* **db.dbal.class_path** (optional): Path to where the
Doctrine DBAL is located.
* **db.common.class_path** (optional): Path to where
Doctrine Common is located.
Services
--------
......@@ -63,8 +57,6 @@ and *Doctrine Common* in ``vendor/doctrine-common``::
'driver' => 'pdo_sqlite',
'path' => __DIR__.'/app.db',
),
'db.dbal.class_path' => __DIR__.'/vendor/doctrine-dbal/lib',
'db.common.class_path' => __DIR__.'/vendor/doctrine-common/lib',
));
Usage
......@@ -106,8 +98,6 @@ and values are options::
'password' => 'my_password',
),
),
'db.dbal.class_path' => __DIR__.'/vendor/doctrine-dbal/lib',
'db.common.class_path' => __DIR__.'/vendor/doctrine-common/lib',
));
The first registered connection is the default and can simply be accessed as
......
......@@ -15,9 +15,6 @@ Parameters
* **monolog.logfile**: File where logs are written to.
* **monolog.class_path** (optional): Path to where the
Monolog library is located.
* **monolog.level** (optional): Level of logging defaults
to ``DEBUG``. Must be one of ``Logger::DEBUG``, ``Logger::INFO``,
``Logger::WARNING``, ``Logger::ERROR``. ``DEBUG`` will log
......@@ -47,8 +44,7 @@ Make sure you place a copy of *Monolog* in the ``vendor/monolog``
directory::
$app->register(new Silex\Provider\MonologServiceProvider(), array(
'monolog.logfile' => __DIR__.'/development.log',
'monolog.class_path' => __DIR__.'/vendor/monolog/src',
'monolog.logfile' => __DIR__.'/development.log',
));
.. note::
......
......@@ -23,9 +23,6 @@ Parameters
* **encryption**: SMTP encryption, defaults to null.
* **auth_mode**: SMTP authentication mode, defaults to null.
* **swiftmailer.class_path** (optional): Path to where the
Swift Mailer library is located.
Services
--------
......@@ -60,9 +57,7 @@ directory. Make sure you point the class path to ``/lib/classes``.
::
$app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
'swiftmailer.class_path' => __DIR__.'/vendor/swiftmailer/lib/classes',
));
$app->register(new Silex\Provider\SwiftmailerServiceProvider());
.. note::
......
......@@ -7,8 +7,7 @@ Symfony2 components and libraries.
Parameters
----------
* **symfony_bridges.class_path** (optional): Path to where
the Symfony2 Bridges are located.
none
Twig
----
......@@ -41,6 +40,4 @@ Make sure you place a copy of the Symfony2 Bridges in either
Then, register the provider via::
$app->register(new Silex\Provider\SymfonyBridgesServiceProvider(), array(
'symfony_bridges.class_path' => __DIR__.'/vendor/symfony/src',
));
$app->register(new Silex\Provider\SymfonyBridgesServiceProvider());
......@@ -16,9 +16,6 @@ Parameters
* **locale_fallback** (optional): Fallback locale for the translator. It will
be used when the current locale has no messages set.
* **translation.class_path** (optional): Path to where
the Symfony2 Translation component is located.
Services
--------
......@@ -41,8 +38,7 @@ Make sure you place a copy of the Symfony2 Translation component in
``vendor/symfony/src``. You can simply clone the whole Symfony2 into vendor::
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale_fallback' => 'en',
'translation.class_path' => __DIR__.'/vendor/symfony/src',
'locale_fallback' => 'en',
));
Usage
......@@ -95,11 +91,15 @@ YAML-based language files
Having your translations in PHP files can be inconvenient. This recipe will
show you how to load translations from external YAML files.
First you will need the ``Config`` and ``Yaml`` components from Symfony2. Also
make sure you register them with the autoloader. You can just clone the entire
Symfony2 repository into ``vendor/symfony``::
First, add the Symfony2 ``Config`` and ``Yaml`` components in your composer
file:
.. code-block:: json
$app['autoloader']->registerNamespace('Symfony', __DIR__.'/vendor/symfony/src');
"require": {
"symfony/config": "2.1.*",
"symfony/yaml": "2.1.*",
}
Next, you have to create the language mappings in YAML files. A naming you can
use is ``locales/en.yml``. Just do the mapping in this file as follows:
......@@ -132,11 +132,11 @@ That's all you need to load translations from YAML files.
XLIFF-based language files
~~~~~~~~~~~~~~~~~~~~~~~~~~
Just as you would do with YAML translation files, you first need to make
sure you have the ``Config`` component from Symfony2, and that it is
registered with the autoloader. See above for details.
Just as you would do with YAML translation files, you first need to add the
Symfony2 ``Config`` component as a dependency (see above for details).
Then, similarly, create XLIFF files in your locales directory and setup the ``translator.messages`` to map to them.
Then, similarly, create XLIFF files in your locales directory and setup the
``translator.messages`` to map to them.
Finally override the ``translator.loader`` to use a ``XliffFileLoader``::
......
......@@ -16,9 +16,6 @@ Parameters
* **twig.options** (optional): An associative array of twig
options. Check out the twig documentation for more information.
* **twig.class_path** (optional): Path to where the Twig
library is located.
* **twig.form.templates** (optional): An array of templates used to render
forms (only available when the ``FormServiceProvider`` is enabled).
......@@ -43,8 +40,7 @@ Make sure you place a copy of *Twig* in the ``vendor/twig``
directory::
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
'twig.class_path' => __DIR__.'/vendor/twig/lib',
'twig.path' => __DIR__.'/views',
));
.. note::
......
......@@ -8,8 +8,7 @@ standalone.
Parameters
----------
* **validator.class_path** (optional): Path to where
the Symfony2 Validator component is located.
none
Services
--------
......@@ -35,9 +34,7 @@ Registering
Make sure you place a copy of the Symfony2 Validator component in
``vendor/symfony/src``. You can simply clone the whole Symfony2 into vendor::
$app->register(new Silex\Provider\ValidatorServiceProvider(), array(
'validator.class_path' => __DIR__.'/vendor/symfony/src',
));
$app->register(new Silex\Provider\ValidatorServiceProvider());
Usage
-----
......
......@@ -195,19 +195,6 @@ of them.
This is only available when a request is being served, you can only access it
from within a controller, before filter, after filter or error handler.
* **autoloader**: This service provides you with a
`UniversalClassLoader
<http://api.symfony.com/master/Symfony/Component/ClassLoader/UniversalClassLoader.html>`_
that is already registered. You can register prefixes
and namespaces on it.
Example usage, autoloads Twig classes::
$app['autoloader']->registerPrefix('Twig_', $app['twig.class_path']);
For more information, check out the `Symfony2 autoloader documentation
<http://symfony.com/doc/current/components/class_loader.html>`_.
* **routes**: The `RouteCollection
<http://api.symfony.com/master/Symfony/Component/Routing/RouteCollection.html>`_
that is used internally. You can add, modify, read
......
......@@ -36,7 +36,6 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Silex\RedirectableUrlMatcher;
use Silex\ControllerResolver;
......@@ -56,13 +55,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{
$app = $this;
$this['autoloader'] = $this->share(function () {
$loader = new UniversalClassLoader();
$loader->register();
return $loader;
});
$this['routes'] = $this->share(function () {
return new RouteCollection();
});
......
......@@ -118,13 +118,5 @@ class DoctrineServiceProvider implements ServiceProviderInterface
return $dbs[$app['dbs.default']];
});
if (isset($app['db.dbal.class_path'])) {
$app['autoloader']->registerNamespace('Doctrine\\DBAL', $app['db.dbal.class_path']);
}
if (isset($app['db.common.class_path'])) {
$app['autoloader']->registerNamespace('Doctrine\\Common', $app['db.common.class_path']);
}
}
}
......@@ -51,9 +51,5 @@ class FormServiceProvider implements ServiceProviderInterface
return new DefaultCsrfProvider($app['form.secret']);
});
if (isset($app['form.class_path'])) {
$app['autoloader']->registerNamespace('Symfony\\Component\\Form', $app['form.class_path']);
}
}
}
......@@ -51,10 +51,6 @@ class MonologServiceProvider implements ServiceProviderInterface
};
}
if (isset($app['monolog.class_path'])) {
$app['autoloader']->registerNamespace('Monolog', $app['monolog.class_path']);
}
$app->before(function (Request $request) use ($app) {
$app['monolog']->addInfo('> '.$request->getMethod().' '.$request->getRequestUri());
});
......
......@@ -24,9 +24,5 @@ class SymfonyBridgesServiceProvider implements ServiceProviderInterface
public function register(Application $app)
{
$app['symfony_bridges'] = true;
if (isset($app['symfony_bridges.class_path'])) {
$app['autoloader']->registerNamespace('Symfony\\Bridge', $app['symfony_bridges.class_path']);
}
}
}
......@@ -49,9 +49,5 @@ class TranslationServiceProvider implements ServiceProviderInterface
$app['translator.message_selector'] = $app->share(function () {
return new MessageSelector();
});
if (isset($app['translation.class_path'])) {
$app['autoloader']->registerNamespace('Symfony\\Component\\Translation', $app['translation.class_path']);
}
}
}
......@@ -85,9 +85,5 @@ class TwigServiceProvider implements ServiceProviderInterface
$app['twig.loader.array'],
));
});
if (isset($app['twig.class_path'])) {
$app['autoloader']->registerPrefix('Twig_', $app['twig.class_path']);
}
}
}
......@@ -42,9 +42,5 @@ class ValidatorServiceProvider implements ServiceProviderInterface
$app['validator.validator_factory'] = $app->share(function () {
return new ConstraintValidatorFactory();
});
if (isset($app['validator.class_path'])) {
$app['autoloader']->registerNamespace('Symfony\\Component\\Validator', $app['validator.class_path']);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment