Skip to content

Commit

Permalink
Import Acme demo bundle from symfony standard edition
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Mar 20, 2014
1 parent 7e5be79 commit 9e806a9
Show file tree
Hide file tree
Showing 31 changed files with 775 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sensio\Bundle\DistributionBundle\Composer;

use Symfony\Component\ClassLoader\ClassCollectionLoader;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\PhpExecutableFinder;
use Composer\Script\CommandEvent;
Expand Down Expand Up @@ -128,6 +129,34 @@ public static function installRequirementsFile(CommandEvent $event)
}
}

public static function installAcmeDemoBundle(CommandEvent $event)
{
if (!$event->getIO()->ask('Would you like to install Acme demo bundle? [yes/NO] ', false)) {
return;
}

$options = self::getOptions($event);
$appDir = $options['symfony-app-dir'];

$kernelFile = $appDir.'/AppKernel.php';
$rootDir = __DIR__ . '/../../../../../../..';

$fs = new Filesystem();
$fs->mirror(__DIR__.'/../Resources/skeleton/acme-demo-bundle', $rootDir.'/src', null, array('override'));

$ref = '$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();';
$bundleDeclaration = "\$bundles[] = new Acme\\DemoBundle\\AcmeDemoBundle();";
$content = file_get_contents($kernelFile);

if (false === strpos($content, $bundleDeclaration)) {
$updatedContent = str_replace($ref, $bundleDeclaration."\n ".$ref, $content);
if ($content === $updatedContent) {
throw new \RuntimeException('Unable to patch %s.', $kernelFile);
}
$fs->dumpFile($kernelFile, $updatedContent);
}
}

public static function doBuildBootstrap($appDir)
{
$file = $appDir.'/bootstrap.php.cache';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Acme\DemoBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeDemoBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Acme\DemoBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Hello World command for demo purposes.
*
* You could also extend from Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
* to get access to the container via $this->getContainer().
*
* @author Tobias Schultze <http://tobion.de>
*/
class HelloWorldCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('acme:hello')
->setDescription('Hello World example command')
->addArgument('who', InputArgument::OPTIONAL, 'Who to greet.', 'World')
->setHelp(<<<EOF
The <info>%command.name%</info> command greets somebody or everybody:
<info>php %command.full_name%</info>
The optional argument specifies who to greet:
<info>php %command.full_name%</info> Fabien
EOF
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf('Hello <comment>%s</comment>!', $input->getArgument('who')));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Acme\DemoBundle\Form\ContactType;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DemoController extends Controller
{
/**
* @Route("/", name="_demo")
* @Template()
*/
public function indexAction()
{
return array();
}

/**
* @Route("/hello/{name}", name="_demo_hello")
* @Template()
*/
public function helloAction($name)
{
return array('name' => $name);
}

/**
* @Route("/contact", name="_demo_contact")
* @Template()
*/
public function contactAction(Request $request)
{
$form = $this->createForm(new ContactType());
$form->handleRequest($request);

if ($form->isValid()) {
$mailer = $this->get('mailer');

// .. setup a message and send it
// http://symfony.com/doc/current/cookbook/email.html

$request->getSession()->getFlashBag()->set('notice', 'Message sent!');

return new RedirectResponse($this->generateUrl('_demo'));
}

return array('form' => $form->createView());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

/**
* @Route("/demo/secured")
*/
class SecuredController extends Controller
{
/**
* @Route("/login", name="_demo_login")
* @Template()
*/
public function loginAction(Request $request)
{
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}

return array(
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
);
}

/**
* @Route("/login_check", name="_security_check")
*/
public function securityCheckAction()
{
// The security layer will intercept this request
}

/**
* @Route("/logout", name="_demo_logout")
*/
public function logoutAction()
{
// The security layer will intercept this request
}

/**
* @Route("/hello", defaults={"name"="World"}),
* @Route("/hello/{name}", name="_demo_secured_hello")
* @Template()
*/
public function helloAction($name)
{
return array('name' => $name);
}

/**
* @Route("/hello/admin/{name}", name="_demo_secured_hello_admin")
* @Security("is_granted('ROLE_ADMIN')")
* @Template()
*/
public function helloadminAction($name)
{
return array('name' => $name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class WelcomeController extends Controller
{
public function indexAction()
{
/*
* The action's view can be rendered using render() method
* or @Template annotation as demonstrated in DemoController.
*
*/

return $this->render('AcmeDemoBundle:Welcome:index.html.twig');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Acme\DemoBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;

class AcmeDemoExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}

public function getAlias()
{
return 'acme_demo';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Acme\DemoBundle\EventListener;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Acme\DemoBundle\Twig\Extension\DemoExtension;

class ControllerListener
{
protected $extension;

public function __construct(DemoExtension $extension)
{
$this->extension = $extension;
}

public function onKernelController(FilterControllerEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$this->extension->setController($event->getController());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', 'email');
$builder->add('message', 'textarea');
}

public function getName()
{
return 'contact';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }

_demo_secured:
resource: "@AcmeDemoBundle/Controller/SecuredController.php"
type: annotation

_demo:
resource: "@AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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">

<services>
<service id="twig.extension.acme.demo" class="Acme\DemoBundle\Twig\Extension\DemoExtension" public="false">
<tag name="twig.extension" />
<argument type="service" id="twig.loader" />
</service>

<service id="acme.demo.listener" class="Acme\DemoBundle\EventListener\ControllerListener">
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" />
<argument type="service" id="twig.extension.acme.demo" />
</service>
</services>
</container>
Loading

0 comments on commit 9e806a9

Please sign in to comment.