8000 Merge branch '2.3' into 2.4 · symfony/symfony-docs@1f14a0e · GitHub
[go: up one dir, main page]

Skip to content

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 1f14a0e

Browse files
committed
Merge branch '2.3' into 2.4
* 2.3: Changes foobar.net in example.com [#4081] Tiny tweak Update introduction.rst documentation for the ClassMapGenerator class fix code block fix a headline improve linking between proxy config sections
2 parents fb387a8 + 7bb4f34 commit 1f14a0e

File tree

7 files changed

+141
-13
lines changed

7 files changed

+141
-13
lines changed

book/http_cache.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ The caching kernel will immediately act as a reverse proxy - caching responses
163163
from your application and returning them to the client.
164164

165165
Now that you're using a "proxy", you'll need to configure ``127.0.0.1`` under
166-
the ``trusted_proxies`` configuration (see :ref:`reference <reference-framework-trusted-proxies>`).
166+
the ``trusted_proxies`` configuration (see :ref:`the reference <reference-framework-trusted-proxies>`).
167167
Without this, the client's IP address and a few other things won't report correctly.
168168

169169
.. tip::

book/service_container.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ you need it::
8282
use Acme\HelloBundle\Mailer;
8383

8484
$mailer = new Mailer('sendmail');
85-
$mailer->send('ryan@foobar.net', ...);
85+
$mailer->send('ryan@example.com', ...);
8686

8787
This is easy enough. The imaginary ``Mailer`` class allows you to configure
8888
the method used to deliver the email messages (e.g. ``sendmail``, ``smtp``, etc).
Lines 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

Lines 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/console/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ method::
460460
$command = $application->find('demo:greet');
461461
$commandTester = new CommandTester($command);
462462
$commandTester->execute(
463-
array('command' => $command->getName(), 'name' => 'Fabien')
463+
array('command' => $command->getName(), 'name' => 'Fabien', '--iterations' => 5)
464464
);
465465

466466
$this->assertRegExp('/Fabien/', $commandTester->getDisplay());

components/map.rst.inc

Lines 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

cookbook/request/load_balancer_reverse_proxy.rst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
How to Configure Symfony to Work behind a Load Balancer or Reverse Proxy
2-
========================================================================
1+
How to Configure Symfony to Work behind a Load Balancer or a Reverse Proxy
2+
==========================================================================
33

44
When you deploy your application, you may be behind a load balancer (e.g.
55
an AWS Elastic Load Balancer) or a reverse proxy (e.g. Varnish for
@@ -60,7 +60,8 @@ and which reverse proxy IP addresses will be doing this type of thing:
6060
6161
In this example, you're saying that your reverse proxy (or proxies) has
6262
the IP address ``192.0.0.1`` or matches the range of IP addresses that use
63-
the CIDR notation ``10.0.0.0/8``. For more details, see :ref:`reference-framework-trusted-proxies`.
63+
the CIDR notation ``10.0.0.0/8``. For more details, see the
64+
:ref:`framework.trusted_proxies <reference-framework-trusted-proxies>` option.
6465

6566
That's it! Symfony will now look for the correct ``X-Forwarded-*`` headers
6667
to get information like the client's IP address, host, port and whether or
@@ -80,13 +81,13 @@ In this case, you'll need to - *very carefully* - trust *all* proxies.
8081
proxies, configure Symfony to *always* trust incoming request. This is
8182
done inside of your front controller::
8283

83-
// web/app.php
84-
// ...
84+
// web/app.php
8585

86-
Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR')));
86+
// ...
87+
Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR')));
8788

88-
$response = $kernel->handle($request);
89-
// ...
89+
$response = $kernel->handle($request);
90+
// ...
9091

9192
That's it! It's critical that you prevent traffic from all non-trusted sources.
9293
If you allow outside traffic, they could "spoof" their true IP address and
@@ -97,7 +98,7 @@ My Reverse Proxy Uses Non-Standard (not X-Forwarded) Headers
9798

9899
Most reverse proxies store information on specific ``X-Forwarded-*`` headers.
99100
But if your reverse proxy uses non-standard header names, you can configure
100-
these (:doc:`see reference </components/http_foundation/trusting_proxies>`.
101+
these (see ":doc:`/components/http_foundation/trusting_proxies`").
101102
The code for doing this will need to live in your front controller (e.g. ``web/app.php``).
102103

103104
.. _`security groups`: http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/using-elb-security-groups.html

0 commit comments

Comments
 (0)
0