├── LICENSE ├── README.md └── composer.json /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020-2025 Ben Ramsey 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

ramsey/composer-repl

2 | 3 |

4 | A REPL for PHP built into Composer. 5 |

6 | 7 |

8 | Source Code 9 | Download Package 10 | PHP Programming Language 11 | Read License 12 | Build Status 13 | Codecov Code Coverage 14 |

15 | 16 | ## About 17 | 18 | This [Composer](https://getcomposer.org) plugin provides the `composer repl` 19 | command. 20 | 21 | REPL stands for *read-eval-print loop*. It's a language shell that reads user 22 | input, evaluates the input using a programming language (in this case, PHP), and 23 | prints the output to the screen. Then, it returns to the read state (that's the 24 | *loop* part). 25 | 26 | [PsySH](https://psysh.org) is the REPL providing the magic behind 27 | ramsey/composer-repl. PsySH is a language shell for PHP. It's similar to 28 | [irb](https://github.com/ruby/irb) for Ruby, [IPython](https://ipython.org) for 29 | Python, and [JShell](https://docs.oracle.com/javase/9/tools/jshell.htm) for 30 | Java. In addition to acting as a language shell, PsySH can also function as an 31 | interactive debugger and development console. 32 | [Laravel Tinker](https://github.com/laravel/tinker), [Drush](https://www.drush.org) 33 | for Drupal, [WP-CLI shell](https://github.com/wp-cli/shell-command) 34 | for WordPress, [CakePHP console](https://book.cakephp.org/3/en/console-and-shells/repl.html), 35 | and [Yii shell](https://github.com/yiisoft/yii2-shell) are a few of the projects 36 | using PsySH. 37 | 38 | > [!TIP] 39 | > You may use this REPL without the Composer plugin functionality by requiring 40 | > [ramsey/composer-repl-lib](https://github.com/ramsey/composer-repl-lib) instead. 41 | 42 | This project adheres to a [code of conduct](CODE_OF_CONDUCT.md). 43 | By participating in this project and its community, you are expected to 44 | uphold this code. 45 | 46 | ## Installation 47 | 48 | Install this package as a development dependency using 49 | [Composer](https://getcomposer.org). 50 | 51 | ``` bash 52 | composer require --dev ramsey/composer-repl 53 | ``` 54 | 55 | ## Usage 56 | 57 | Open your terminal and type `composer repl`. You may also type `composer shell`, 58 | if you prefer. 59 | 60 | You'll see something similar to this: 61 | 62 | ``` 63 | Psy Shell v0.12.7 (PHP 8.4.4 — cli) by Justin Hileman 64 | ------------------------------------------------------------------------ 65 | Welcome to the development console (REPL). 66 | To learn more about what you can do in PsySH, type `help`. 67 | ------------------------------------------------------------------------ 68 | >>> 69 | ``` 70 | 71 | While in the dev console, you can do cool things like this: 72 | 73 | ``` php 74 | >>> $hello = 'Hello, world' 75 | => "Hello, world" 76 | 77 | >>> echo $hello 78 | Hello, world 79 | 80 | >>> foreach ([1, 2, 3] as $x) echo $x . "\n" 81 | 1 82 | 2 83 | 3 84 | 85 | >>> $date = new DateTimeImmutable(); 86 | => DateTimeImmutable @1598393282 {#6953 87 | date: 2020-08-25 22:08:02.643076 UTC (+00:00), 88 | } 89 | 90 | >>> $getDate = fn (DateTimeInterface $dt): DateTimeInterface => $dt; 91 | => Closure(DateTimeInterface $dt): DateTimeInterface {#6964 …3} 92 | 93 | >>> t assertInstanceOf(DateTimeInterface::class, $date); 94 | Test passed! 95 | 96 | >>> t assertSame($date, $getDate($date)) 97 | Test passed! 98 | 99 | >>> phpunit 100 | 101 | PHPUnit 12.0.6 by Sebastian Bergmann and contributors. 102 | 103 | Runtime: PHP 8.4.4 104 | Configuration: /path/to/ramsey/conventional-commits/phpunit.xml.dist 105 | 106 | ............................................................... 63 / 221 ( 28%) 107 | ............................................................... 126 / 221 ( 57%) 108 | ............................................................... 189 / 221 ( 85%) 109 | ................................ 221 / 221 (100%) 110 | 111 | Time: 00:00.064, Memory: 12.00 MB 112 | 113 | OK (221 tests, 484 assertions) 114 | ``` 115 | 116 | > [!IMPORTANT] 117 | > ✨🐘 This implementation of PsySH has Super ElePHPant Powers. 🐘✨ 118 | 119 | ## Environment Bootstrapping 120 | 121 | The power of this REPL comes in its ability to act as a tool in your local 122 | development environment. So, you might want to load parts of your environment 123 | (i.e., configuration, objects, etc.), so you can access these from within the 124 | REPL. 125 | 126 | You can do this by specifying any number of PHP scripts to include in 127 | `composer.json`, like this: 128 | 129 | ``` json 130 | { 131 | "extra": { 132 | "ramsey/composer-repl": { 133 | "includes": [ 134 | "repl.php", 135 | "tests/bootstrap.php" 136 | ] 137 | } 138 | } 139 | } 140 | ``` 141 | 142 | Any variables set or configuration loaded from these scripts is available to use 143 | from within the REPL. 144 | 145 | For example, if `repl.php` contains: 146 | 147 | ``` php 148 | >> ls 168 | Variables: $env, $foo, $phpunit 169 | 170 | >>> $foo 171 | => "bar" 172 | ``` 173 | 174 | ## Contributing 175 | 176 | Contributions are welcome! To contribute, please familiarize yourself with 177 | [CONTRIBUTING.md](CONTRIBUTING.md). 178 | 179 | ## Coordinated Disclosure 180 | 181 | Keeping user information safe and secure is a top priority, and we welcome the 182 | contribution of external security researchers. If you believe you've found a 183 | security issue in software that is maintained in this repository, please read 184 | [SECURITY.md](SECURITY.md) for instructions on submitting a vulnerability report. 185 | 186 | ## Copyright and License 187 | 188 | The ramsey/composer-repl plugin is copyright © [Ben Ramsey](https://benramsey.com) 189 | and licensed for use under the terms of the 190 | MIT License (MIT). Please see [LICENSE](LICENSE) for more information. 191 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ramsey/composer-repl", 3 | "description": "A REPL for PHP built into Composer.", 4 | "license": "MIT", 5 | "type": "composer-plugin", 6 | "keywords": [ 7 | "repl", 8 | "psysh", 9 | "shell" 10 | ], 11 | "authors": [ 12 | { 13 | "name": "Ben Ramsey", 14 | "email": "ben@benramsey.com", 15 | "homepage": "https://benramsey.com" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.1", 20 | "composer-plugin-api": "^2.0", 21 | "ramsey/composer-repl-lib": "^1.2" 22 | }, 23 | "require-dev": { 24 | "captainhook/captainhook": "^5.25", 25 | "captainhook/plugin-composer": "^5.3", 26 | "ergebnis/composer-normalize": "^2.45", 27 | "ramsey/conventional-commits": "^1.6", 28 | "roave/security-advisories": "dev-latest" 29 | }, 30 | "config": { 31 | "allow-plugins": { 32 | "captainhook/plugin-composer": true, 33 | "ergebnis/composer-normalize": true 34 | }, 35 | "sort-packages": true 36 | }, 37 | "extra": { 38 | "captainhook": { 39 | "force-install": true 40 | }, 41 | "class": "Ramsey\\Dev\\Repl\\Composer\\ReplPlugin", 42 | "ramsey/conventional-commits": { 43 | "configFile": "conventional-commits.json" 44 | } 45 | } 46 | } 47 | --------------------------------------------------------------------------------