Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 41116da

Browse filesBrowse files
committed
feature #4081 [Components][ClassLoader] documentation for the ClassMapGenerator class (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [Components][ClassLoader] documentation for the ClassMapGenerator class | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#2471) | Applies to | all | Fixed tickets | #1134 Commits ------- 2ebe6c3 documentation for the ClassMapGenerator class
2 parents 12d0b82 + 2ebe6c3 commit 41116da
Copy full SHA for 41116da

File tree

Expand file treeCollapse file tree

3 files changed

+128
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+128
-1
lines changed
+125Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

‎components/class_loader/index.rst

Copy file name to clipboardExpand all lines: components/class_loader/index.rst
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ ClassLoader
33

44
.. toctree::
55
:maxdepth: 2
6-
6+
77
introduction
88
class_loader
99
map_class_loader
1010
cache_class_loader
1111
debug_class_loader
12+
class_map_generator

‎components/map.rst.inc

Copy file name to clipboardExpand all lines: components/map.rst.inc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* :doc:`/components/class_loader/map_class_loader`
88
* :doc:`/components/class_loader/cache_class_loader`
99
* :doc:`/components/class_loader/debug_class_loader`
10+
* :doc:`/components/class_loader/class_map_generator`
1011

1112
* :doc:`/components/config/index`
1213

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.