|
| 1 | +.. index:: |
| 2 | + single: Autoloading; Class Map Generator |
| 3 | + single: ClassLoader; Class Map Generator |
| 4 | + |
| 5 | +The Class Map Generator |
| 6 | +======================= |
| 7 | + |
| 8 | +Loading a class usually is an easy task given the `PSR-0`_ and `PSR-4`_ standards. |
| 9 | +Thanks to the Symfony ClassLoader component or the autoloading mechanism provided |
| 10 | +by Composer, you don't have to map your class names to actual PHP files manually. |
| 11 | +Nowadays, PHP libraries usually come with autoloading support through Composer. |
| 12 | + |
| 13 | +But from time to time you may have to use a third-party library that comes |
| 14 | +without any autoloading support and therefore forces you to load each class |
| 15 | +manually. For example, imagine a library with the following directory structure: |
| 16 | + |
| 17 | +.. code-block:: text |
| 18 | +
|
| 19 | + library/ |
| 20 | + ├── bar/ |
| 21 | + │ ├── baz/ |
| 22 | + │ │ └── Boo.php |
| 23 | + │ └── Foo.php |
| 24 | + └── foo/ |
| 25 | + ├── bar/ |
| 26 | + │ └── Foo.php |
| 27 | + └── Bar.php |
| 28 | +
|
| 29 | +These files contain the following classes: |
| 30 | + |
| 31 | +=========================== ================ |
| 32 | +File Class name |
| 33 | +=========================== ================ |
| 34 | +``library/bar/baz/Boo.php`` ``Acme\Bar\Baz`` |
| 35 | +--------------------------- ---------------- |
| 36 | +``library/bar/Foo.php`` ``Acme\Bar`` |
| 37 | +--------------------------- ---------------- |
| 38 | +``library/foo/bar/Foo.php`` ``Acme\Foo\Bar`` |
| 39 | +--------------------------- ---------------- |
| 40 | +``library/foo/Bar.php`` ``Acme\Foo`` |
| 41 | +=========================== ================ |
| 42 | + |
| 43 | +To make your life easier, the ClassLoader component comes with a |
| 44 | +:class:`Symfony\\Component\\ClassLoader\\ClassMapGenerator` class that makes |
| 45 | +it possible to create a map of class names to files. |
| 46 | + |
| 47 | +Generating a Class Map |
| 48 | +---------------------- |
| 49 | + |
| 50 | +To generate the class map, simply pass the root directory of your class files |
| 51 | +to the :method:`Symfony\\Component\\ClassLoader\\ClassMapGenerator::createMap`` |
| 52 | +method:: |
| 53 | + |
| 54 | + use Symfony\Component\ClassLoader\ClassMapGenerator; |
| 55 | + |
| 56 | + print_r(ClassMapGenerator::createMap(__DIR__.'/library')); |
| 57 | + |
| 58 | +Given the files and class from the table above, you should see an output like |
| 59 | +this: |
| 60 | + |
| 61 | +.. code-block:: text |
| 62 | +
|
| 63 | + Array |
| 64 | + ( |
| 65 | + [Acme\Foo] => /var/www/library/foo/Bar.php |
| 66 | + [Acme\Foo\Bar] => /var/www/library/foo/bar/Foo.php |
| 67 | + [Acme\Bar\Baz] => /var/www/library/bar/baz/Boo.php |
| 68 | + [Acme\Bar] => /var/www/library/bar/Foo.php |
| 69 | + ) |
| 70 | +
|
| 71 | +Dumping the Class Map |
| 72 | +--------------------- |
| 73 | + |
| 74 | +Writing the class map to the console output is not really sufficient when |
| 75 | +it comes to autoloading. Luckily, the ``ClassMapGenerator`` provides the |
| 76 | +:method:`Symfony\\Component\\ClassLoader\\ClassMapGenerator::dump` method |
| 77 | +to save the generated class map to the filesystem:: |
| 78 | + |
| 79 | + use Symfony\Component\ClassLoader\ClassMapGenerator; |
| 80 | + |
| 81 | + ClassMapGenerator::dump(__DIR__.'/library', __DIR__.'/class_map.php'); |
| 82 | + |
| 83 | +This call to ``dump()`` generates the class map and writes it to the ``class_map.php`` |
| 84 | +file in the same directory with the following contents:: |
| 85 | + |
| 86 | + <?php return array ( |
| 87 | + 'Acme\\Foo' => '/var/www/library/foo/Bar.php', |
| 88 | + 'Acme\\Foo\\Bar' => '/var/www/library/foo/bar/Foo.php', |
| 89 | + 'Acme\\Bar\\Baz' => '/var/www/library/bar/baz/Boo.php', |
| 90 | + 'Acme\\Bar' => '/var/www/library/bar/Foo.php', |
| 91 | + ); |
| 92 | + |
| 93 | +Instead of loading each file manually, you'll only have to register the generated |
| 94 | +class map with, for example, the :class:`Symfony\\Component\\ClassLoader\\MapClassLoader`:: |
| 95 | + |
| 96 | + use Symfony\Component\ClassLoader\MapClassLoader; |
| 97 | + |
| 98 | + $mapping = include __DIR__.'/class_map.php'; |
| 99 | + $loader = new MapClassLoader($mapping); |
| 100 | + $loader->register(); |
| 101 | + |
| 102 | + // you can now use the classes: |
| 103 | + use Acme\Foo; |
| 104 | + |
| 105 | + $foo = new Foo(); |
| 106 | + |
| 107 | + // ... |
| 108 | + |
| 109 | +.. note:: |
| 110 | + |
| 111 | + The example assumes that you already have autoloading working (e.g. |
| 112 | + through `Composer`_ or one of the other class loaders from the ClassLoader |
| 113 | + component. |
| 114 | + |
| 115 | +Besides dumping the class map for one directory, you can also pass an array |
| 116 | +of directories for which to generate the class map (the result actually is |
| 117 | +the same as in the example above):: |
| 118 | + |
| 119 | + use Symfony\Component\ClassLoader\ClassMapGenerator; |
| 120 | + |
| 121 | + ClassMapGenerator::dump(array(__DIR__.'/library/bar', __DIR__.'/library/foo'), __DIR__.'/class_map.php'); |
| 122 | + |
| 123 | +.. _`PSR-0`: http://www.php-fig.org/psr/psr-0 |
| 124 | +.. _`PSR-4`: http://www.php-fig.org/psr/psr-4 |
| 125 | +.. _`Composer`: http://getcomposer.org |
0 commit comments