Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Misc update (#55)
Browse files Browse the repository at this point in the history
- Fix some CS
- Add types
- Copy the necessary test container services from `test.xml` instead of prepending the framework bundle test config
- Make `psysh.shell` private
- Inject the Psysh application instead of application child class
- Add a few missing `@private` tags
  • Loading branch information
theofidry authored Mar 26, 2020
1 parent b06dd86 commit 2dc4446
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 137 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,14 @@ public function registerBundles()
## Usage

```bash
# Symfony > 3.0
php bin/console psysh

# Symfony < 3.0
php app/console psysh

# Symfony > 4.0
bin/console psysh
```

or

```php
use function Fidry\PsyshBundle\psysh
use function psysh

class X
{
Expand Down
31 changes: 23 additions & 8 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env php
<?php
<?php declare(strict_types=1);

/*
* This file is part of the PsyshBundle package.
Expand All @@ -10,8 +10,12 @@
* file that was distributed with this source code.
*/

// Debug only
if (false === in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
echo 'Warning: The console should be invoked via the CLI version of PHP, not the '.PHP_SAPI.' SAPI'.PHP_EOL;
}

set_time_limit(0);

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

use Fidry\PsyshBundle\Functional\AppKernel;
Expand All @@ -20,13 +24,24 @@ use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\ErrorHandler\Debug;

$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';

$env = $input->getParameterOption(
['--env', '-e'],
getenv('APP_ENV') ?: 'dev'
);

$debug = (getenv('APP_DEBUG') !== '0')
&& !$input->hasParameterOption(['--no-debug', ''])
&& $env !== 'prod'
;

if ($debug) {
Debug::enable();
}
umask(0000);

$kernel = new AppKernel($env, $debug);
if (class_exists(Debug::class)) {
Debug::enable();
}
}

$application = new Application($kernel);
$application = new Application(new AppKernel($env, $debug));
$application->run($input);
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.4/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
stopOnFailure="false"
bootstrap="vendor/autoload.php">
Expand Down
17 changes: 13 additions & 4 deletions resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="psysh.config" class="Psy\Configuration">
<service id="Psy\Configuration">
<call method="setHistoryFile">
<argument>%kernel.cache_dir%/psysh_history</argument>
</call>
</service>

<service id="psysh.shell" class="Psy\Shell" public="true">
<argument type="service" id="psysh.config" />
<service id="psysh.shell" class="Psy\Shell">
<argument type="service" id="Psy\Configuration" />
</service>

<service id="psysh.command.shell_command" class="Fidry\PsyshBundle\Command\PsyshCommand" public="true">
Expand All @@ -22,9 +22,18 @@

<service id="psysh.facade" class="Fidry\PsyshBundle\PsyshFacade" public="true">
<call method="setContainer">
<argument type="service" id="service_container" />
<argument type="service" id="test.service_container" />
</call>
</service>

<service id="test.service_container" class="Symfony\Bundle\FrameworkBundle\Test\TestContainer" public="true">
<argument type="service" id="kernel" />
<argument>test.private_services_locator</argument>
</service>

<service id="test.private_services_locator" class="Symfony\Component\DependencyInjection\ServiceLocator" public="true">
<argument type="collection" />
</service>
</services>

</container>
28 changes: 8 additions & 20 deletions src/Command/PsyshCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of the PsyshBundle package.
Expand All @@ -11,7 +11,7 @@

namespace Fidry\PsyshBundle\Command;

use Psy\Shell;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -22,34 +22,22 @@
*/
final class PsyshCommand extends Command
{
/**
* @var Shell
*/
private $shell;

/**
* @param Shell $shell
*/
public function __construct(Shell $shell)
private $psysh;

public function __construct(Application $psysh)
{
parent::__construct();

$this->shell = $shell;
$this->psysh = $psysh;
}

/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this->setDescription('Start PsySH for Symfony');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
return (int) $this->shell->run();
return (int) $this->psysh->run($input, $output);
}
}
12 changes: 7 additions & 5 deletions src/DependencyInjection/Compiler/AddPsyshCommandPass.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of the PsyshBundle package.
Expand All @@ -11,7 +11,6 @@

namespace Fidry\PsyshBundle\DependencyInjection\Compiler;

use Psy\Command\Command as PsyshCommand;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -25,17 +24,20 @@
*/
final class AddPsyshCommandPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->has('psysh.shell')) {
return;
}

$commands = [];

foreach ($container->findTaggedServiceIds('psysh.command') as $id => $attributes) {
// Workaround to avoid Psysh commands to be registered as regular console commands
// (conflict with service autoconfiguration as Psysh commands inherit from \Symfony\Component\Console\Command\Command as well
// Note that this compiler pass must run with a higher priority than AddConsoleCommandPass to be efficient.
// (conflict with service autoconfiguration as Psysh commands inherit from
// \Symfony\Component\Console\Command\Command as well
// Note that this compiler pass must run with a higher priority than
// AddConsoleCommandPass to be efficient.
$container->findDefinition($id)->clearTag('console.command');
$commands[] = new Reference($id);
}
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of the PsyshBundle package.
Expand All @@ -19,7 +19,7 @@
*/
final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('psysh');
$rootNode = $treeBuilder->getRootNode();
Expand Down
64 changes: 40 additions & 24 deletions src/DependencyInjection/PsyshExtension.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of the PsyshBundle package.
Expand All @@ -14,53 +14,69 @@
use Psy\Command\Command;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
use function array_merge;
use function is_string;
use function sprintf;
use function strpos;
use function substr;

/**
* This is the class that loads and manages your bundle configuration.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*
* @author Théo FIDRY <[email protected]>
*
* @private
*/
final class PsyshExtension extends Extension implements PrependExtensionInterface
final class PsyshExtension extends Extension
{
public function prepend(ContainerBuilder $container)
{
if (class_exists(TestContainer::class)) {
$container->prependExtensionConfig('framework', ['test' => true]);
}
}
private const CONFIG_DIR = __DIR__.'/../../resources/config';

/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../../resources/config'));
$loader = new Loader\XmlFileLoader($container, new FileLocator(self::CONFIG_DIR));
$loader->load('services.xml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$config = $this->processConfiguration(new Configuration(), $configs);

foreach ($config['variables'] as $name => &$value) {
if (is_string($value) && $value[0] === '@') {
if (is_string($value) && strpos($value, '@') === 0) {
$value = new Reference(substr($value, 1));
}
}
$containerId = class_exists(TestContainer::class) ? 'test.service_container' : 'service_container';
$container->findDefinition('psysh.shell')
->addMethodCall('setScopeVariables', [$config['variables'] + [
'container' => new Reference($containerId),
'kernel' => new Reference('kernel'),
'self' => new Reference('psysh.shell'),
'parameters' => new Expression(sprintf("service('%s').getParameterBag().all()", $containerId))
]]);

$container->registerForAutoconfiguration(Command::class)->addTag('psysh.command');
$containerId = 'test.service_container';

$container
->findDefinition('psysh.shell')
->addMethodCall(
'setScopeVariables',
[array_merge(
$config['variables'],
[
'container' => new Reference($containerId),
'kernel' => new Reference('kernel'),
'self' => new Reference('psysh.shell'),
'parameters' => new Expression(sprintf(
"service('%s').getParameterBag().all()",
$containerId
))
]
)]
)
;

$container
->registerForAutoconfiguration(Command::class)
->addTag('psysh.command')
;
}
}
16 changes: 11 additions & 5 deletions src/PsyshBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,35 @@
namespace Fidry\PsyshBundle;

use Fidry\PsyshBundle\DependencyInjection\Compiler\AddPsyshCommandPass;
use Psy\Command\Command;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* @author Adrian PALMER <[email protected]>
* @author Théo FIDRY <[email protected]>
*
* @private
*/
final class PsyshBundle extends Bundle
{
public function boot()
public function boot(): void
{
parent::boot();

$this->container->get('psysh.facade');
}

public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
parent::build($container);

// Ensure that AddPsyshCommandPass runs before AddConsoleCommandPass to avoid autoconfiguration conflicts.
$container->addCompilerPass(new AddPsyshCommandPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
// Ensures that AddPsyshCommandPass runs before AddConsoleCommandPass to avoid
// autoconfiguration conflicts.
$container->addCompilerPass(
new AddPsyshCommandPass(),
PassConfig::TYPE_BEFORE_OPTIMIZATION,
10
);
}
}
Loading

0 comments on commit 2dc4446

Please sign in to comment.