Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / front.php
Created June 2, 2012 03:19
The first version of our front controller for Simplex. - http://www.symfonycentral.com
<?php
/* Import the autoloader and classes from external namespaces */
require_once __DIR__.'/../src/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/* Build the request and response objects using HttpFoundation */
$request = Request::createFromGlobals();
$response = new Response();
@coreymcmahon
coreymcmahon / routing.php
Created June 2, 2012 03:28
Using the Symfony 2 Routing component to match URLs - http://www.symfonycentral.com
<?php
// ...
/* Define the routes for our application */
$routes = new Routing\RouteCollection();
$routes->add('hello', new Routing\Route('/hello/{name}', array('name' => 'World')));
$routes->add('bye', new Routing\Route('/bye'));
/* Set up the context and establish whether this request matches a route */
@coreymcmahon
coreymcmahon / routing.php
Created June 2, 2012 03:43
Using the Symfony 2 Routing component with an in-line Controller - http://www.symfonycentral.com
<?php
// ...
$routes->add('hello', new Routing\Route('/hello/{name}', array(
'name' => 'World',
'_controller' => function ($request) {
return render_template($request);
}
)));
@coreymcmahon
coreymcmahon / front.php
Created June 2, 2012 04:26
Our new front controller using HTTP Foundation - http://www.symfonycentral.com
<?php
// ...
$request = Request::createFromGlobals();
$routes->add('leap_year', new Routing\Route('/is_leap_year/{year}', array(
'year' => null,
'_controller' => 'LeapYearController::indexAction',
)));
@coreymcmahon
coreymcmahon / Framework.php
Created June 14, 2012 10:41
Using namespacing in the Simplex framework - http://www.symfonycentral.com
<?php
namespace Simplex;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
class Framework
@coreymcmahon
coreymcmahon / LeapYearController.php
Created June 14, 2012 10:44
Defining a controller in the Calendar\Controller namespace - http://www.symfonycentral.com
<?php
namespace Calendar\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Calendar\Model\LeapYear;
class LeapYearController
{
// ... controller code in here
@coreymcmahon
coreymcmahon / LeapYear.php
Created June 14, 2012 10:45
Defining a model in the Calendar\Model namespace - http://www.symfonycentral.com
<?php
namespace Calendar\Model;
class LeapYear
{
// ... code for function isLeapYear in here
}
@coreymcmahon
coreymcmahon / Framework.php
Created June 14, 2012 10:48
Using interfaces so that our Framework calls are mockable for testing purposes - http://www.symfonycentral.com
<?php
namespace Simplex;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
class Framework
{
public function __construct(UrlMatcherInterface $matcher, ControllerResolverInterface $resolver)
{
@coreymcmahon
coreymcmahon / FrameworkTest.php
Created June 14, 2012 10:54
Example of a Unit test for our PHP framework, Simplex - http://www.symfonycentral.com
<?php
namespace Simplex\Tests;
use Simplex\Framework;
class FrameworkTest extends \PHPUnit_Framework_TestCase
{
public function testNotFoundHandling()
{
$this->assertEquals(/* test condition in here */);
@coreymcmahon
coreymcmahon / front.php
Created June 14, 2012 11:04
Defining an event listener that adds a HTML comment to the Response body - http://www.symfonycentral.com
<?php
require_once __DIR__.'/../vendor/.composer/autoload.php';
// ... etc
use Symfony\Component\EventDispatcher\EventDispatcher;
$dispatcher = new EventDispatcher();
$dispatcher->addListener('response', function (Simplex\ResponseEvent $event) {
$response = $event->getResponse();