Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Added command to remove AcmeDemoBundle #412

Closed
wants to merge 3 commits into from
Closed
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
18 changes: 18 additions & 0 deletions UPGRADE-2.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,21 @@ Functional Tests
even better, you can just enable it for the very next request by calling
``$client->enableProfiler()`` when you need the profiler in a test (that
speeds up functional tests quite a bit).

Routing
----------------
* The default place for Acme DemoBundle routes has been changed to Acme
DemoBundle directory. You can find them in
```Acme\DemoBundle\Resources\config\routing.yml```

Command
----------------
* A new demo command has been added to the AcmeDemoBundle that will help you
remove the bundle once you want to start working on a clean copy of
Symfony2 Standard. You can call it like this:
```app/console demo:self-remove``` and type ```y``` when prompted to remove
the bundle in order to confirm the action. Else the command will display a
list of actions that it will do when running in ```live``` mode.
Make sure that the user executing the command has the needed privilages to
delete the files and directories mentioned while in dry-run mode.

15 changes: 3 additions & 12 deletions app/config/routing_dev.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
_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
_acme_demo:
resource: "@AcmeDemoBundle/Resources/config/routing.yml"
prefix: /

_wdt:
resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
Expand Down
143 changes: 143 additions & 0 deletions src/Acme/DemoBundle/Command/SelfRemoveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace Acme\DemoBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;

class SelfRemoveCommand extends ContainerAwareCommand
{

protected function configure()
{
$this
->setName('demo:self-remove')
->setDescription('Remove the Acme vendor and DemoBundle from default Symfony Standard distribution')
->setHelp(
<<<EOF
The <info>%command.name%</info> <error>removes</error> the AcmeBundle shipped with Symfony2 Standard.

Since this operation is not reversible you will be asked if you are sure that
you want to delete the bundle.

If you choose <info>N</info> then the system will run this in dry-run mode.
If you choose <info>a</info> then the system will abort this command.
If you choose <info>y</info> then the system will proceed and remove the AcmeDemoBundle
EOF
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
// Check if we have the confirmation from the user in order to actually delete this
$dialog = $this->getHelperSet()->get('dialog');

$routeRemovalWarning = 'Confirm removal of AcmeDemoBundle? [ayN] ';
$confirm = $dialog->ask($output, $routeRemovalWarning, 'n');

switch (strtolower($confirm)) {
case 'a':
$output->writeln("Aborting operation");

return;
case 'y':
$confirm = true;
break;
default:
$confirm = false;
}

// Get the path to our Vendor/Bundle directory
$path = realpath(__DIR__ . '/../../');

if ($confirm) {
$message = 'Removing: ';
} else {
$message = 'Dry-run: ';
}

$output->writeln(sprintf("%s Removing AcmeDemoBundle directory (%s)", $message, $path));

if ($confirm) {
$fs = new Filesystem();

try {
$fs->remove($path);
} catch (IOException $e) {
$output->writeln("<error>An error occured while removing the AcmeDemoBundle directory</error>");
}
}

// Next step is to remove the routes to the bundle

// Get the Kernel dir path
$kernelDirPath = $this->getContainer()->get('kernel')->getRootDir();

// Change the routes file
$routesFile = $kernelDirPath . '/config/routing_dev.yml';
$output->writeln('Changing the contents of the routes file ... ' . $routesFile);

// Remove just the routes that are related to Acme/DemoBundle
if ($confirm) {
$routesContents = file($routesFile);
$routesCount = count($routesContents);

$foundMatch = false;
$i = 0;
while ($i < $routesCount) {
if ($routesContents[$i] == "_acme_demo:\n") {
$foundMatch = true;
unset($routesContents[$i]);
$i++;
continue;
}

if ($foundMatch) {
if (strpos($routesContents[$i], "\t") !== false ||
strpos($routesContents[$i], " ") !== false
) {
unset($routesContents[$i]);
} else {
break;
}
}

$i++;
}

// Restore the file contents
$routesContents = implode('', $routesContents);
file_put_contents($routesFile, $routesContents);
}

// The final step is to change the AppKernel Contents

// Get the kernel file
$kernelFile = $kernelDirPath . '/AppKernel.php';
$output->writeln('Changing the contents of AppKernel ... ' . $kernelFile);

// Get the contents
$kernelContents = file_get_contents($kernelFile);

// Remove the AcmeDemoBundle
$kernelContents = preg_replace(
'/\s+\\$bundles\[\]\s+=\s+new Acme\\\\DemoBundle\\\\AcmeDemoBundle\(\);/m',
'',
$kernelContents
);

// Check if we can save the new kernel file
if ($confirm) {
file_put_contents($kernelFile, $kernelContents);
}

$output->writeln('');

// Finish
$output->writeln('Done');
}
}
12 changes: 12 additions & 0 deletions src/Acme/DemoBundle/Resources/config/routing.yml
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