Skip to content

Quick review of 'create framework' tutorial #5570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ empty class, you might be tempted to move some code from the front controller
to it::

// example.com/src/Simplex/Framework.php

namespace Simplex;

use Symfony\Component\Routing;
Expand All @@ -33,7 +32,6 @@ to it::
The front controller code would become more concise::

// example.com/web/front.php

require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -94,7 +92,6 @@ container:
Create a new file to host the dependency injection container configuration::

// example.com/src/container.php

use Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Reference;

Expand Down Expand Up @@ -147,7 +144,6 @@ it in other object definitions.
The front controller is now only about wiring everything together::

// example.com/web/front.php

require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand All @@ -165,7 +161,6 @@ As all the objects are now created in the dependency injection container, the
framework code should be the previous simple version::

// example.com/src/Simplex/Framework.php

namespace Simplex;

use Symfony\Component\HttpKernel\HttpKernel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ To make it work, the framework must dispatch an event just before returning
the Response instance::

// example.com/src/Simplex/Framework.php

namespace Simplex;

use Symfony\Component\HttpFoundation\Request;
Expand All @@ -46,9 +45,9 @@ the Response instance::

class Framework
{
protected $matcher;
protected $resolver;
protected $dispatcher;
private $matcher;
private $resolver;
private $dispatcher;

public function __construct(EventDispatcher $dispatcher, UrlMatcherInterface $matcher, ControllerResolverInterface $resolver)
{
Expand Down Expand Up @@ -85,7 +84,6 @@ Each time the framework handles a Request, a ``ResponseEvent`` event is
now dispatched::

// example.com/src/Simplex/ResponseEvent.php

namespace Simplex;

use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -118,7 +116,6 @@ The last step is the creation of the dispatcher in the front controller and
the registration of a listener for the ``response`` event::

// example.com/web/front.php

require_once __DIR__.'/../vendor/autoload.php';

// ...
Expand Down Expand Up @@ -197,7 +194,6 @@ the priority to ``-255``::
Let's refactor the code a bit by moving the Google listener to its own class::

// example.com/src/Simplex/GoogleListener.php

namespace Simplex;

class GoogleListener
Expand All @@ -220,7 +216,6 @@ Let's refactor the code a bit by moving the Google listener to its own class::
And do the same with the other listener::

// example.com/src/Simplex/ContentLengthListener.php

namespace Simplex;

class ContentLengthListener
Expand Down Expand Up @@ -259,7 +254,6 @@ information to the dispatcher via the ``getSubscribedEvents()`` method. Have a
look at the new version of the ``GoogleListener``::

// example.com/src/Simplex/GoogleListener.php

namespace Simplex;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -277,7 +271,6 @@ look at the new version of the ``GoogleListener``::
And here is the new version of ``ContentLengthListener``::

// example.com/src/Simplex/ContentLengthListener.php

namespace Simplex;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ spice things up a little bit, let's go crazy and add another page that says
goodbye::

// framework/bye.php

require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand All @@ -26,7 +25,6 @@ The PHP way of doing the refactoring would probably be the creation of an
include file::

// framework/init.php

require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand All @@ -38,7 +36,6 @@ include file::
Let's see it in action::

// framework/index.php

require_once __DIR__.'/init.php';

$input = $request->get('name', 'World');
Expand All @@ -49,7 +46,6 @@ Let's see it in action::
And for the "Goodbye" page::

// framework/bye.php

require_once __DIR__.'/init.php';

$response->setContent('Goodbye!');
Expand All @@ -76,7 +72,6 @@ routing all client requests to a single PHP script.
Such a script might look like the following::

// framework/front.php

require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand All @@ -103,7 +98,6 @@ Such a script might look like the following::
And here is for instance the new ``hello.php`` script::

// framework/hello.php

$input = $request->get('name', 'World');
$response->setContent(sprintf('Hello %s', htmlspecialchars($input, ENT_QUOTES, 'UTF-8')));

Expand Down Expand Up @@ -194,15 +188,13 @@ the ``setContent()`` directly from the front controller script::
And the ``hello.php`` script can now be converted to a template::

<!-- example.com/src/pages/hello.php -->

<?php $name = $request->get('name', 'World') ?>

Hello <?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>

We have the first version of our framework::

// example.com/web/front.php

require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Even if the "application" we wrote in the previous chapter was simple enough,
it suffers from a few problems::

// framework/index.php

$input = $_GET['name'];

printf('Hello %s', $input);
Expand All @@ -27,7 +26,6 @@ First, if the ``name`` query parameter is not defined in the URL query string,
you will get a PHP warning; so let's fix it::

// framework/index.php

$input = isset($_GET['name']) ? $_GET['name'] : 'World';

printf('Hello %s', $input);
Expand Down Expand Up @@ -60,7 +58,6 @@ snippet of PHP code is not natural and feels ugly. Here is a tentative PHPUnit
unit test for the above code::

// framework/test.php

class IndexTest extends \PHPUnit_Framework_TestCase
{
public function testHello()
Expand Down Expand Up @@ -147,7 +144,6 @@ Now, let's rewrite our application by using the ``Request`` and the
``Response`` classes::

// framework/index.php

require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -227,7 +223,7 @@ With the ``Response`` class, you can easily tweak the response::

.. tip::

To debug a Response, cast it to a string; it will return the HTTP
To debug a response, cast it to a string; it will return the HTTP
representation of the response (headers and content).

Last but not the least, these classes, like every other class in the Symfony
Expand All @@ -239,7 +235,7 @@ framework?

Even something as simple as getting the client IP address can be insecure::

if ($myIp == $_SERVER['REMOTE_ADDR']) {
if ($myIp === $_SERVER['REMOTE_ADDR']) {
// the client is a known one, so give it some more privilege
}

Expand All @@ -248,7 +244,7 @@ production servers; at this point, you will have to change your code to make
it work on both your development machine (where you don't have a proxy) and
your servers::

if ($myIp == $_SERVER['HTTP_X_FORWARDED_FOR'] || $myIp == $_SERVER['REMOTE_ADDR']) {
if ($myIp === $_SERVER['HTTP_X_FORWARDED_FOR'] || $myIp === $_SERVER['REMOTE_ADDR']) {
// the client is a known one, so give it some more privilege
}

Expand All @@ -258,7 +254,7 @@ chained proxies)::

$request = Request::createFromGlobals();

if ($myIp == $request->getClientIp()) {
if ($myIp === $request->getClientIp()) {
// the client is a known one, so give it some more privilege
}

Expand All @@ -271,7 +267,7 @@ explicitly trust your reverse proxies by calling ``setTrustedProxies()``::

Request::setTrustedProxies(array('10.0.0.1'));

if ($myIp == $request->getClientIp(true)) {
if ($myIp === $request->getClientIp(true)) {
// the client is a known one, so give it some more privilege
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ a Request object. All controller resolvers implement the following interface::

namespace Symfony\Component\HttpKernel\Controller;

// ...
interface ControllerResolverInterface
{
function getController(Request $request);
Expand Down Expand Up @@ -151,7 +152,6 @@ method is not defined, an argument has no matching attribute, ...).
Let's conclude with the new version of our framework::

// example.com/web/front.php

require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ feedback when a problem arises.
Here is the new framework code::

// example.com/src/Simplex/Framework.php

namespace Simplex;

use Symfony\Component\HttpKernel\HttpKernel;
Expand All @@ -35,7 +34,6 @@ Here is the new framework code::
And the new front controller::

// example.com/web/front.php

require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -79,13 +77,14 @@ thrown ``Exception`` instance to ease exception manipulation and display. It
can take any valid controller as an exception handler, so you can create an
ErrorController class instead of using a Closure::

$listener = new HttpKernel\EventListener\ExceptionListener('Calendar\\Controller\\ErrorController::exceptionAction');
$listener = new HttpKernel\EventListener\ExceptionListener(
'Calendar\\Controller\\ErrorController::exceptionAction'
);
$dispatcher->addSubscriber($listener);

The error controller reads as follows::

// example.com/src/Calendar/Controller/ErrorController.php

namespace Calendar\Controller;

use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -148,7 +147,6 @@ is to convert the controller return value to a proper Response instance, but
only if needed::

// example.com/src/Simplex/StringResponseListener.php

namespace Simplex;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down
Loading