This commit is contained in:
Simon Vieille 2015-11-28 09:05:33 +01:00
commit 522beb2a2b
10 changed files with 227 additions and 0 deletions

11
.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/app/bootstrap.php.cache
/app/cache/*
!app/cache/.gitkeep
/app/config/parameters.yml
/app/logs/*
!app/logs/.gitkeep
/app/phpunit.xml
/bin/
/composer.phar
/vendor/
/web/bundles/

7
src/.htaccess Normal file
View File

@ -0,0 +1,7 @@
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>

View File

@ -0,0 +1,11 @@
<?php
namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AppBundle extends Bundle
{
}

View File

@ -0,0 +1,42 @@
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
* @Template()
*/
public function indexAction(Request $request)
{
return [];
}
/**
* @Route("/add/{id}", name="add_prestation")
*/
public function addAction(Request $request, $id)
{
$this->get('cart')
->addPrestation($id)
->update();
return new RedirectResponse($this->generateUrl('homepage'));
}
/**
* @Route("/cart", name="cart")
* @Template()
*/
public function cartAction(Request $request)
{
return ['cart' => $this->get('cart')];
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace AppBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\Config\FileLocator;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace AppBundle\Model;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* Class Cart
* @author Simon Vieille <simon@deblan.fr>
*/
class Cart
{
protected $prestations = [];
protected $session;
public function __construct(Session $session)
{
$this->session = $session;
$sessionCart = $this->session->get('cart');
if ($sessionCart !== null) {
$this->setPrestations($sessionCart->getPrestations());
}
}
public function addPrestation($id)
{
if (!in_array($id, $this->prestations)) {
$this->prestations[] = $id;
}
return $this;
}
public function getPrestations()
{
return $this->prestations;
}
public function setPrestations(array $prestations)
{
$this->prestations = $prestations;
return $this;
}
public function update()
{
$this->session->set('cart', $this);
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="cart.class">AppBundle\Model\Cart</parameter>
</parameters>
<services>
<service id="cart" class="%cart.class%">
<argument type="service" id="session"></argument>
</service>
</services>
</container>

View File

@ -0,0 +1,20 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul>
{% for item in cart.prestations %}
<li>
Prestation {{ item }}
</li>
{% endfor %}
</ul>
<p>
<a href="{{ path('homepage') }}">Accueil</a>
</p>
</body>
</html>

View File

@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul>
<li>
<a href="{{ path('add_prestation', {id: 1}) }}">Ajouter prestation 1</a>
</li>
<li>
<a href="{{ path('add_prestation', {id: 2}) }}">Ajouter prestation 2</a>
</li>
<li>
<a href="{{ path('add_prestation', {id: 3}) }}">Ajouter prestation 3</a>
</li>
<li>
<a href="{{ path('add_prestation', {id: 4}) }}">Ajouter prestation 4</a>
</li>
<li>
<a href="{{ path('add_prestation', {id: 5}) }}">Ajouter prestation 5</a>
</li>
</ul>
<p>
<a href="{{ path('cart') }}">Afficher le panier</a>
</p>
</body>
</html>

View File

@ -0,0 +1,18 @@
<?php
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertContains('Welcome to Symfony', $crawler->filter('#container h1')->text());
}
}