Skip to content

Commit

Permalink
Merge pull request #1951 from TheBrightPath/feature/pass-command-to-b…
Browse files Browse the repository at this point in the history
…ootstrap

Provide $input, $output, and $context variable to bootstrap script
  • Loading branch information
dereuromark authored Feb 23, 2021
2 parents 9c44c27 + 9640748 commit 14de58e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/en/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,15 @@ setting External Variables to modify the config will not work because the config
paths:
bootstrap: 'phinx-bootstrap.php'
Within the bootstrap script, the following variables will be available:

.. code-block:: php
/**
* @var string $filename The file name as provided by the configuration
* @var string $filePath The absolute, real path to the file
* @var \Symfony\Component\Console\Input\InputInterface $input The executing command's input object
* @var \Symfony\Component\Console\Output\OutputInterface $output The executing command's output object
* @var \Phinx\Console\Command\AbstractCommand $context the executing command object
*/
2 changes: 1 addition & 1 deletion src/Phinx/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function bootstrap(InputInterface $input, OutputInterface $output)

if ($bootstrap = $this->getConfig()->getBootstrapFile()) {
$output->writeln('<info>using bootstrap</info> ' . Util::relativePath($bootstrap) . ' ');
Util::loadPhpFile($bootstrap);
Util::loadPhpFile($bootstrap, $input, $output, $this);
}

// report the paths
Expand Down
10 changes: 9 additions & 1 deletion src/Phinx/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use DateTime;
use DateTimeZone;
use Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Util
{
Expand Down Expand Up @@ -235,12 +237,15 @@ public static function glob($path)
* Takes the path to a php file and attempts to include it if readable
*
* @param string $filename Filename
* @param \Symfony\Component\Console\Input\InputInterface|null $input Input
* @param \Symfony\Component\Console\Output\OutputInterface|null $output Output
* @param \Phinx\Console\Command\AbstractCommand|mixed|null $context Context
*
* @throws \Exception
*
* @return string
*/
public static function loadPhpFile($filename)
public static function loadPhpFile($filename, ?InputInterface $input = null, ?OutputInterface $output = null, $context = null)
{
$filePath = realpath($filename);
if (!file_exists($filePath)) {
Expand All @@ -258,6 +263,9 @@ public static function loadPhpFile($filename)
throw new Exception(sprintf("Cannot open file %s \n", $filename));
}

// prevent this to be propagated to the included file
unset($isReadable);

include_once $filePath;

return $filePath;
Expand Down

0 comments on commit 14de58e

Please sign in to comment.