Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading namespaced classes with Symfony 1.4's autoloader?

How to register namespaces (with PHP 5.3) in the Symfony 1.4 for the autoloader class feature (like the Symfony 2.0)?

like image 500
Wagner Pinheiro Avatar asked Jun 06 '11 15:06

Wagner Pinheiro


1 Answers

You can use Autoloader from Symfony2 in Symfony 1.4 framework.

1. Copy Symfony2 classloaders to vendor directory of your Symfony 1.4 sandbox project:

SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/UniversalClassLoader.php

SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php

2. Modify your SF_ROOT_DIR/config/ProjectConfiguration.class.php file as follows:

require_once dirname(__FILE__) . '/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
require_once dirname(__FILE__) . '/../lib/autoload/sfClassLoader.class.php';
sfCoreAutoload::register();

class ProjectConfiguration extends sfProjectConfiguration {

    public function setup() {
        $this->namespacesClassLoader();
        $this->enablePlugins('sfDoctrinePlugin');
    }

    public function namespacesClassLoader() {
       if (extension_loaded('apc')) {
           $loader = new ApcUniversalClassLoader('S2A');
       } else {
           $loader = new UniversalClassLoader();
       }
       $loader->registerNamespaces(array(
          'Pohon' => __DIR__ . '/../lib/vendor/Pohon/src'));
       $loader->register();
    }

}

3. Register desired namespaces:
eg. I want to load class:

Pohon\Tools\String\Utils\Slugify.

Filename must be:

SF_ROOT_DIR/lib/vendor/Pohon/src/Pohon/Tools/String/Utils/Slugify.php

and registered namespace as follows:

Pohon => SF_ROOT_DIR/lib/vendor/Pohon/src

like image 135
Jan Míšek Avatar answered Sep 18 '22 14:09

Jan Míšek