Development

Changeset 1251

You must first sign up to be able to contribute.

Changeset 1251

Show
Ignore:
Timestamp:
04/24/06 11:56:00 (3 years ago)
Author:
francois
Message:

documented third party autoloading

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/doc/book/content/configuration_practice.txt

    r1193 r1251  
    292292Each autoloading rule has a label, both for visual organization and ability to override it.  
    293293 
    294 If you want to autoload classes stored somewhere else in your file structure, you need to create a new `autoload.yml` in your `myproject/apps/myapp/config/` folder and edit it. You can either override existing rules or add new ones. 
     294If you want to autoload classes stored somewhere else in your file structure, you need to create a new `autoload.yml` in your `myproject/apps/myapp/config/` folder and edit it. You can either override existing rules or add new ones. The [custom extension chapter](custom_helper.txt) will tell you more about it. 
    295295 
    296296### `php.yml`: PHP configuration                           {#phpyml} 
  • trunk/doc/book/content/custom_helper.txt

    r971 r1251  
    103103    [php] 
    104104    $id = sfContext::getInstance()->getRequest()->getParameter('id'); 
     105 
     106Class autoloading 
     107----------------- 
     108 
     109The autoloading feature allows you to create an new object without requiring the file that contains its class definition by hand: 
     110 
     111    [php] 
     112    $obj = new MyClass(); 
     113    // If MyClass is autoloaded, this will work without a require 
     114     
     115The autoloading is configured to work with the `lib/` directories described above, according to the default `autoload.yml` configuration file (found in `$pear_data_dir/symfony/config/autoload.yml`): 
     116 
     117    autoload: 
     118     
     119      symfony_core: 
     120        name:           symfony core classes 
     121        ext:            .class.php 
     122        path:           %SF_SYMFONY_LIB_DIR% 
     123        recursive:      on 
     124        exclude:        [vendor] 
     125     
     126      symfony_orm: 
     127        name:           symfony orm classes 
     128        files: 
     129          Propel:       %SF_SYMFONY_LIB_DIR%/addon/propel/sfPropelAutoload.php 
     130          Criteria:     %SF_SYMFONY_LIB_DIR%/vendor/propel/util/Criteria.php 
     131          SQLException: %SF_SYMFONY_LIB_DIR%/vendor/creole/SQLException.php 
     132          DatabaseMap:  %SF_SYMFONY_LIB_DIR%/vendor/propel/map/DatabaseMap.php 
     133     
     134      symfony_plugins: 
     135        name:           symfony plugins 
     136        ext:            .class.php 
     137        path:           %SF_PLUGIN_DIR% 
     138        recursive:      on 
     139     
     140      project: 
     141        name:           project classes 
     142        ext:            .class.php 
     143        path:           %SF_LIB_DIR% 
     144        recursive:      on 
     145        exclude:        [model, plugins, symfony] 
     146     
     147      model: 
     148        name:           project model classes 
     149        ext:            .php 
     150        path:           %SF_MODEL_LIB_DIR% 
     151     
     152      application: 
     153        name:           application classes 
     154        ext:            .class.php 
     155        path:           %SF_APP_LIB_DIR% 
     156 
     157If you want to add other places for symfony to look into, create your own `autoload.yml` file in an application `config/` directory and add in new rules. Each autoloading rule has a label, both for visual organization and ability to override it. 
     158 
     159For instance, if you added your own `MyClass` class in `/usr/local/mycustomclasses/`, you could add: 
     160 
     161      mycustomclasses: 
     162        name:           classes that I developed myself 
     163        ext:            .php 
     164        path:           /usr/local/mycustomclasses/ 
     165        recursive:      on 
     166 
     167Third-party libraries 
     168--------------------- 
     169 
     170If you need a functionality provided by a third-party class, and if you don't want to copy this class in one of the symfony `lib/` dirs, you will probably install it outside of the usual places where symfony looks for files. In that case, using this class will imply a manual `require` in your code, unless you use the symfony **bridge** to take advantage of the autoloading. 
     171 
     172Symfony doesn't (yet) provide tools for everything. If you need a PDF generator, an API to Google Maps or a PHP implementation of the Lucene search engine, you will probably need a few libraries from the [Zend Framework](http://framework.zend.com/). If you want to manipulate images directly in PHP, connect to a POP3 account to read emails, or design a console interface for Linux, you will choose the libraries from [eZcomponents](http://ez.no/products/ez_components). 
     173 
     174Fortunately, if you define the right settings, the components from both these libraries will work out of the box in symfony. 
     175 
     176The first think that you need to declare (unless you installed the third party libraries via PEAR) is the path to the root directory of the libraries. This is to be done in the application `settings.yml`: 
     177 
     178    .settings: 
     179      zend_lib_dir:   /usr/local/zend/library/ 
     180      ez_lib_dir:     /usr/local/ezcomponents/ 
     181       
     182Then, extend the autoload routine by telling which library to look at when the autoloading fails with symfony: 
     183 
     184    .settings: 
     185      autoloading_functions: [[sfZendFrameworkBridge, autoload], [sfEzComponentsBridge, autoload]] 
     186       
     187What will happen when you create a new object of an unloaded class is: 
     188 
     1891. The symfony autoloading function (`Symfony::autoload()`) first looks for a class in the paths declared in the `autoload.yml` 
     1902. If none is found, then the callback functions declared in the `sf_autoloading_functions` setting will be called one after the other, until one of them returns `true`: 
     191    1. `sfZendFrameworkBridge::autoload()` 
     192    2. `sfEzComponentsBridge::autoload()` 
     1933. If these also return `false`, then symfony will throw an exception saying that the class doesn't exist. 
     194 
     195Note that this setting is distinct from the rules defined in the `autoload.yml`. The `autoloading_functions` setting specifies bridge classes, and the `autoload.yml` specifies paths and rules for searching. 
     196 
     197The available bridges are stored in the `$pear_php_dir/symfony/addon/bridge/` directory. 
     198 
     199Plug-ins 
     200-------- 
     201 
     202Symfony can also be extended through plug-ins. To see how to install, use or create your own plu-in, refer to the [related chapter](plugin.txt). 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.