Skip to content

Commit

Permalink
Convert to InteractiveBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmgx committed Feb 20, 2023
1 parent cc48781 commit 2d59bb9
Show file tree
Hide file tree
Showing 41 changed files with 1,032 additions and 531 deletions.
1 change: 0 additions & 1 deletion .github/FUNDING.yml

This file was deleted.

37 changes: 0 additions & 37 deletions .github/workflows/tests.yaml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dist
coverage.xml
bin
!bin/console
!bin/interactive*
.phpunit.result.cache
var
.idea
36 changes: 36 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('config')
->exclude('var')
->exclude('wordpress')
->exclude('public/bundles')
->exclude('public/build')
// exclude files generated by Symfony Flex recipes
->notPath('bin/interactive')
->notPath('public/index.php')
->notPath('node_modules')
;

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'linebreak_after_opening_tag' => true,
'mb_str_functions' => true,
'no_php4_constructor' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_strict' => true,
'phpdoc_order' => true,
'strict_comparison' => true,
'strict_param' => true,
'no_multiline_whitespace_around_double_arrow' => false,
'concat_space' => ['spacing' => 'one'],
])
->setFinder($finder)
->setCacheFile(__DIR__.'/var/.php_cs.cache')
;
7 changes: 0 additions & 7 deletions .scrutinizer.yml

This file was deleted.

177 changes: 84 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,124 +1,119 @@
# PsyshBundle
# InteractiveBundle

[![Package version](http://img.shields.io/packagist/v/theofidry/psysh.svg?style=flat-square)](https://packagist.org/packages/theofidry/psysh-bundle)
[![Build Status](https://img.shields.io/travis/theofidry/PsyshBundle.svg?branch=master&style=flat-square)](https://travis-ci.org/theofidry/PsyshBundle?branch=master)
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/theofidry/PsyshBundle.svg?style=flat-square)](https://scrutinizer-ci.com/g/theofidry/PsyshBundle/?branch=master)
[![License](https://img.shields.io/badge/license-MIT-red.svg?style=flat-square)](LICENSE)
A bundle to use the php REPL [PsySH][1] with [Symfony][2], learn more at [psysh.org][1].

A bundle to use the php REPL [PsySH][1] with [Symfony][2]. Learn more at [psysh.org][1] and check out the [Interactive Debugging in PHP talk from OSCON](https://presentate.com/bobthecow/talks/php-for-pirates) on Presentate.
What this Bundle do exactly?
- Boots [PsySH][1] with your application dependencies
- Exposes helpers commands to ease the use of your application

What does it do exactly?
* Loads [PsySH][1] with the application dependencies
* Gives access to the following variables:

| Variable | Description |
|-----------------------|--------------------------------------|
| `$container` | Instance of Symfony ServiceContainer |
| `$kernel` | Instance of Symfony Kernel |
| `$parameters` | Instance of Symfony parameters |
## Quick Look

Aside from that it's the plain old [PsySH][1]! You can also [customize it](#customize-psysh) to add your own variables.
To start:

```bash
$ bin/console interactive
- or -
$ bin/interactive
```

## Documentation
### `service` helper

1. [Install](#install)
1. [Usage](#usage)
1. [PsySH as a debugger](doc/debugger.md)
1. [Reflect like a boss](doc/reflect.md)
1. [PsySH for breakpoints](doc/breakpoint.md)
1. [Customize PsySH](#customize-psysh)
1. [Credits](#credits)
`service $your_variable_name = the_service_identifier`

You don't have to put the full service name, the helper will do its best to find out.

```
service $repo = BlogPostRepo
var_dump($repo)
object(App\Repository\BlogPostRepository) { ... }
```

## Install

You can use [Composer](https://getcomposer.org/) to install the bundle to your project:
### `instance` helper

```bash
composer require --dev theofidry/psysh-bundle
```
`instance $your_variable_name = the_class_name`

Then, enable the bundle by updating your `app/AppKernel.php` file to enable the bundle:
(not needed on symfony 5, bundle is automaticaly registred in `config/bundles.php`)
```php
<?php
// app/AppKernel.php
You don't have to put the full class name, the helper will do its best to find out.

public function registerBundles()
{
//...
```
instance $post = BlogPost
var_dump($post)
object(App\Entity\BlogPost) { ... }
```

if (in_array($this->getEnvironment(), ['dev', 'test'])) {
//...
$bundles[] = new Fidry\PsyshBundle\PsyshBundle();
}
### Full Example

return $bundles;
}
```
$ bin/console interactive
This is an enhanced interactive PHP prompt for your Symfony project!
## Usage
You can use those command to get class instances and services easily:
- instance $i = YourClass will give you an instance of the class
- service $s = ServiceName will give you the service
```bash
# Symfony > 4.0
bin/console psysh
```
if any of those fail, you will have a prompt with a list of entries that could match,
pass *null* to be prompted from all
Psy Shell v0.11.12 (PHP 8.1.12 — cli) by Justin Hileman
> instance $post = blogPost
FOUND Class "\App\Entity\BlogPost" found for identifier: blogPost
- $post = resolved_class('\App\Entity\BlogPost');
= App\Entity\BlogPost {#8136}
or
> $post->setTitle('My Title')->setDate(new \DateTime())->setContent('Content');
= App\Entity\BlogPost {#8136}
```php
use function psysh
> service $repo = blogpostRepository
class X
{
function foo()
{
psysh(get_defined_vars(), $this); // Debug with the current context
}
}
FOUND Service "App\Repository\BlogPostRepository" found for identifier: blogpostRepository
- $repo = resolved_service('App\Repository\BlogPostRepository');
= App\Repository\BlogPostRepository {#8212}
> $repo->save($post, true);
= null
>
```

![PsySH Shell](doc/images/shell.png)
That's it! You now have a new entry in your database.

[Go further](#documentation).
Aside from that it's the plain old [PsySH][1]!
You can also [customize it](#customize-psysh) to add your own commandes and variables.


## Customize PsySH
## Install

### Adding a custom command
Adding a custom command for PsySH is as simple as defining a service with `psysh.command` tag!
You should use [Composer](https://getcomposer.org/) to install the bundle to your project:

```yaml
services:
my_psysh_command:
class: Acme\Shell\MyCommand
tags:
- { name: psysh.command }
```bash
$ composer require --dev jrmgx/interactive-bundle
```

Or even simpler if you use Symfony 3.3+:

```yaml
services:
_defaults:
autoconfigure: true
autowire: true
public: false
## Usage

Acme\Shell\MyCommand: ~
```bash
$ bin/console interactive
- or -
$ bin/interactive
```

> PsyshBundle provides autoconfiguration for custom Psysh command services, as long as they inherit from
> `Psy\Command\ReflectingCommand` or `Psy\Command\Command`.
## Customize PsySH

### Adding a custom command

Adding a custom command for PsySH is as simple as inheriting from `Psy\Command\Command`.

### Adding custom variables

It is possible to add custom variables to the shell via configuration.
Variables can be of any type, container parameters references (e.g. `%kernel.debug%`) or even services
(prefixed with `@`, e.g. `"@my_service"`).

```yaml
# app/config/config_dev.yml
# config/psysh.yml

psysh:
variables:
Expand All @@ -128,27 +123,23 @@ psysh:
debug: "%kernel.debug%"
```

Now if you run `php app/console psysh` and then `ls`, you will see the variables `$foo`, `$router`, `$some` and `$debug`,
in addition to already defined variables:
Now if you run `bin/interactive` and then `ls`,
you will see the variables `$foo`, `$router`, `$some` and `$debug`.

```
>>> ls
> ls
Variables: $foo, $router, $some, $debug...
```

Default variables are:
- `$container` (the service container)
- `$kernel`
- `$parameters` (all container parameters)
- `$self` (the PsySH shell itself)


## Credits

This bundle is developed by [Théo FIDRY](https://github.com/theofidry). This project has been made possible thanks to:
This bundle is developed by [Jerome Gangneux](https://jerome.gangneux.net)

This project has been made possible thanks to:

* [Justin Hileman](https://github.com/bobthecow): author of [PsySH][1] and [all the contributors of the PsySH project](https://github.com/bobthecow/psysh/graphs/contributors)
* [Adrian Palmer](https://github.com/navitronic): gave the lead for porting [PsySH][1] on [Symfony][2]
- [Théo FIDRY](https://github.com/theofidry): main author of this Bundle before the fork ([found here](https://github.com/theofidry/PsyshBundle))
- [Justin Hileman](https://github.com/bobthecow): author of [PsySH][1] and [all the contributors of the PsySH project](https://github.com/bobthecow/psysh/graphs/contributors)
- [Adrian Palmer](https://github.com/navitronic): gave the lead for porting [PsySH][1] on [Symfony][2]


[1]: https://psysh.org/
Expand Down
4 changes: 2 additions & 2 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<?php declare(strict_types=1);

/*
* This file is part of the PsyshBundle package.
* This file is part of the InteractiveBundle package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
Expand All @@ -18,7 +18,7 @@ set_time_limit(0);

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

use Fidry\PsyshBundle\Functional\AppKernel;
use Jrmgx\InteractiveBundle\Functional\AppKernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\ErrorHandler\Debug;
Expand Down
50 changes: 50 additions & 0 deletions bin/interactive
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

/*
* This file is part of the InteractiveBundle package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

if (false === in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
echo 'Warning: The interactive shell 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 Jrmgx\InteractiveBundle\Functional\AppKernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\ErrorHandler\Debug;

$input = new ArgvInput();
$output = new ConsoleOutput();

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

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

if ($debug) {
umask(0000);

if (class_exists(Debug::class)) {
Debug::enable();
}
}

$application = new Application(new AppKernel($env, $debug));
$application->setDefaultCommand('interactive');
$application->doRun($input, $output);
Loading

0 comments on commit 2d59bb9

Please sign in to comment.