Skip to content

Commit

Permalink
Make phalcon self-healing when executing an external false command wh…
Browse files Browse the repository at this point in the history
…en .phalcon folder is missing
  • Loading branch information
ninjapanzer committed Jul 24, 2017
1 parent 61e8163 commit c9a0257
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
8 changes: 8 additions & 0 deletions phalcon.php
Expand Up @@ -37,6 +37,7 @@
use Phalcon\Commands\Builtin\Serve;
use Phalcon\Commands\Builtin\Console;
use Phalcon\Exception as PhalconException;
use Phalcon\Commands\DotPhalconMissingException;
use Phalcon\Events\Manager as EventsManager;

try {
Expand Down Expand Up @@ -73,6 +74,13 @@
}

$script->run();
} catch (DotPhalconMissingException $e) {
fwrite(STDERR, Color::info($e->getMessage() . " " . $e->scanPathMessage()));
if ($e->promptResolution()) {
$script->run();
} else {
exit(1);
}
} catch (PhalconException $e) {
fwrite(STDERR, Color::error($e->getMessage()) . PHP_EOL);
exit(1);
Expand Down
4 changes: 2 additions & 2 deletions scripts/Phalcon/Commands/CommandsListener.php
Expand Up @@ -36,7 +36,7 @@ class CommandsListener
* @param Command $command
*
* @return bool
* @throws CommandsException
* @throws DotPhalconMissingException
*/
public function beforeCommand(Event $event, Command $command)
{
Expand All @@ -55,7 +55,7 @@ public function beforeCommand(Event $event, Command $command)
if ($command->canBeExternal() == false) {
$path = $command->getOption('directory');
if (!file_exists($path.'.phalcon') || !is_dir($path.'.phalcon')) {
throw new CommandsException('This command should be invoked inside a Phalcon project directory.');
throw new DotPhalconMissingException();
}
}

Expand Down
66 changes: 66 additions & 0 deletions scripts/Phalcon/Commands/DotPhalconMissingException.php
@@ -0,0 +1,66 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Developer Tools |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2017 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Paul Scarrone <paul@savvysoftworks.com> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Commands;

use Phalcon\Exception;
use Phalcon\Generator\Stub;
use Phalcon\Script\Color;

/**
* .phalcon is missing Exception
* This is thrown when a CLI command is run without a .phalcon file
* In the CWD
* @package Phalcon\Commands
*/
class DotPhalconMissingException extends CommandsException
{
const DEFAULT_MESSAGE = "This command must be run inside a Phalcon project with a .phalcon directory.";
const RESOLUTION_PROMPT = "Shall I create the .phalcon directory now? (y/n)";

public function __construct (string $message = self::DEFAULT_MESSAGE , int $code = 0)
{
$this->message = $message;
$this->code = $code;
}

public function scanPathMessage()
{
return "One was not found at " . getcwd();
}

public function promptResolution() {
fwrite(STDOUT, Color::info(self::RESOLUTION_PROMPT));
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'y'){
echo "ABORTING!\n";
return false;
}
fclose($handle);
echo "\n";
echo "Retrying command...\n";
$this->resolve();
return true;
}

public function resolve() {
return mkdir(getcwd() . "/.phalcon");
}
}

0 comments on commit c9a0257

Please sign in to comment.