Exemplo n.º 1
0
 /**
  * The entry method.
  *
  * @return  int
  */
 public function main()
 {
     $server = '127.0.0.1:8889';
     while (false !== ($c = $this->getOption($v))) {
         switch ($c) {
             case 's':
                 $server = $v;
                 break;
             case 'h':
             case '?':
                 return $this->usage();
             case '__ambiguous':
                 $this->resolveOptionAmbiguity($v);
                 break;
         }
     }
     $readline = new Console\Readline();
     $client = new Websocket\Client(new Socket\Client('tcp://' . $server));
     $client->setHost('localhost');
     $client->connect();
     do {
         $line = $readline->readLine('> ');
         if (false === $line || 'quit' === $line) {
             break;
         }
         $client->send($line);
     } while (true);
     $client->close();
     return;
 }
Exemplo n.º 2
0
 /**
  * The entry method.
  *
  * @return  int
  */
 public function main()
 {
     while (false !== ($c = $this->getOption($v))) {
         switch ($c) {
             case 'h':
             case '?':
                 return $this->usage();
             case '__ambiguous':
                 $this->resolveOptionAmbiguity($v);
                 break;
         }
     }
     $this->parser->listInputs($expression);
     $compiler = Compiler\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp'));
     $visitor = new Math\Visitor\Arithmetic();
     $dump = new Compiler\Visitor\Dump();
     if (null !== $expression) {
         $ast = $compiler->parse($expression);
         echo $expression . ' = ' . $visitor->visit($ast), "\n";
         return;
     }
     $readline = new Console\Readline();
     $readline->setAutocompleter(new Console\Readline\Autocompleter\Word(array_merge(array_keys($visitor->getConstants()->getArrayCopy()), array_keys($visitor->getFunctions()->getArrayCopy()))));
     $handle = null;
     $expression = 'h';
     do {
         switch ($expression) {
             case 'h':
             case 'help':
                 echo 'Usage:', "\n", '    h[elp]       to print this help;', "\n", '    c[onstants]  to print available constants;', "\n", '    f[unctions]  to print available functions;', "\n", '    e[xpression] to print the current expression;', "\n", '    d[ump]       to dump the tree of the expression;', "\n", '    q[uit]       to quit.', "\n";
                 break;
             case 'c':
             case 'constants':
                 echo implode(', ', array_keys($visitor->getConstants()->getArrayCopy())), "\n";
                 break;
             case 'f':
             case 'functions':
                 echo implode(', ', array_keys($visitor->getFunctions()->getArrayCopy())), "\n";
                 break;
             case 'e':
             case 'expression':
                 echo $handle, "\n";
                 break;
             case 'd':
             case 'dump':
                 if (null === $handle) {
                     echo 'Type a valid expression before (“> 39 + 3”).', "\n";
                 } else {
                     echo $dump->visit($compiler->parse($handle)), "\n";
                 }
                 break;
             case 'q':
             case 'quit':
                 break 2;
             default:
                 if (null === $expression) {
                     break;
                 }
                 try {
                     echo $visitor->visit($compiler->parse($expression)), "\n";
                 } catch (Compiler\Exception $e) {
                     echo $e->getMessage(), "\n";
                     break;
                 }
                 $handle = $expression;
                 break;
         }
     } while (false !== ($expression = $readline->readLine('> ')));
     return;
 }
Exemplo n.º 3
0
 /**
  * The entry method.
  *
  * @return  int
  */
 public function main()
 {
     while (false !== ($c = $this->getOption($v))) {
         switch ($c) {
             case 'h':
             case '?':
                 return $this->usage();
             case '__ambiguous':
                 $this->resolveOptionAmbiguity($v);
                 break;
         }
     }
     Realdom::setDefaultSampler(new Math\Sampler\Random());
     $compiler = Compiler\Llk::load(new File\Read('hoa://Library/Praspel/Grammar.pp'));
     $interpreter = new Praspel\Visitor\Interpreter();
     $dump = new Praspel\Visitor\Compiler();
     $interpreter->visit($compiler->parse('@requires;'));
     $words = [];
     from('Hoathis or Hoa')->foreachImport('Realdom.*', function ($classname) use(&$words) {
         $class = new \ReflectionClass($classname);
         if ($class->isSubclassOf('\\Hoa\\Realdom')) {
             $words[] = $classname::NAME;
         }
         return;
     });
     $readline = new Console\Readline();
     $readline->setAutocompleter(new Console\Readline\Autocompleter\Word($words));
     $expression = '.h';
     do {
         try {
             if ('.' === $expression[0]) {
                 @(list($expression, $tail) = explode(' ', $expression));
             }
             switch ($expression) {
                 case '.h':
                 case '.help':
                     echo 'Usage:', "\n", '    .h[elp]      to print this help;', "\n", '    .c[lear]     to clear the screen;', "\n", '    .v[ariables] to print all variables;', "\n", '    .s[ample]    to sample a value of a variable;', "\n", '    .u[nset]     to unset a variable;', "\n", '    .d[ump]      to dump the tree of the expression;', "\n", '    .q[uit]      to quit.', "\n";
                     break;
                 case '.c':
                 case '.clear':
                     Console\Cursor::clear('↕');
                     break;
                 case '.v':
                 case '.variables':
                     foreach ($interpreter->getClause() as $variable) {
                         echo $variable->getName(), ': ', $variable->getHeld()->toPraspel(), "\n";
                     }
                     break;
                 case '.s':
                 case '.sample':
                     if (null === $tail) {
                         echo 'You must precise a variable name.', "\n";
                         break;
                     }
                     $_clause = $interpreter->getClause();
                     if (!isset($_clause[$tail])) {
                         echo 'Variable ', $tail, ' does not exist.', "\n";
                         break;
                     }
                     $_variable = $_clause[$tail];
                     var_export($_variable->sample());
                     echo "\n";
                     $_variable->reset();
                     break;
                 case '.u':
                 case '.unset':
                     if (null === $tail) {
                         echo 'You must precise a variable name.', "\n";
                         break;
                     }
                     $_clause = $interpreter->getClause();
                     unset($_clause[$tail]);
                     break;
                 case '.d':
                 case '.dump':
                     echo $dump->visit($interpreter->getRoot());
                     break;
                 case '.q':
                 case '.quit':
                     break 2;
                 default:
                     if (null === $expression) {
                         break;
                     }
                     $interpreter->visit($compiler->parse($expression, 'expression'));
                     break;
             }
         } catch (\Exception $e) {
             echo $e->getMessage(), "\n";
         }
         echo "\n";
     } while (false !== ($expression = $readline->readLine('> ')));
     return;
 }
Exemplo n.º 4
0
 /**
  * Main method.
  *
  * @return int
  */
 function main()
 {
     $verbose = !(Console::isDirect(STDOUT) || !OS_WIN);
     while (false !== ($c = $this->getOption($v))) {
         switch ($c) {
             case '__ambiguous':
                 $this->resolveOptionAmbiguity($v);
                 break;
             case 'v':
                 $verbose = $v;
                 break;
             case 'h':
             case '?':
             default:
                 return $this->usage();
         }
     }
     if (true === Installer::isInstalled()) {
         echo 'The application is already installed.', "\n";
         return 1;
     }
     $oldTitle = Window::getTitle();
     Window::setTitle('Installation of sabre/katana');
     $form = ['baseUrl' => '/', 'email' => null, 'password' => null, 'database' => ['driver' => 'sqlite', 'host' => '', 'port' => '', 'name' => '', 'username' => '', 'password' => '']];
     $readline = new Console\Readline();
     if (true === $verbose) {
         $windowWidth = Window::getSize()['x'];
         $labelMaxWidth = 35;
         $inputMaxWidth = $windowWidth - $labelMaxWidth;
         $numberOfSteps = 5;
         $input = function ($default = '') use($inputMaxWidth) {
             return Text::colorize($default . str_repeat(' ', $inputMaxWidth - mb_strlen($default)), 'foreground(black) background(#cccccc)');
         };
         $resetInput = function ($default = '') use($input, $labelMaxWidth) {
             Cursor::move('→', $labelMaxWidth);
             echo $input($default);
             Cursor::move('LEFT');
             Cursor::move('→', $labelMaxWidth);
             Cursor::colorize('foreground(black) background(#cccccc)');
         };
         echo Text::colorize('Installation of sabre/' . "\n" . Welcome::LOGO, 'foreground(yellow)'), "\n\n", static::getBaseURLInfo(), "\n\n", 'Choose the base URL:               ', $input('/'), "\n", 'Your administrator login:          '******'Choose the administrator password: '******'Choose the administrator email:    ', $input(), "\n", 'Choose the database driver:        ', '🔘 SQLite ⚪️ MySQL', "\n";
         Window::scroll('↑', 10);
         Cursor::move('↑', 10);
         Cursor::move('↑', $numberOfSteps);
         Cursor::move('→', $labelMaxWidth);
         // Disable arrow up and down.
         $no_echo = function ($readline) {
             return $readline::STATE_NO_ECHO;
         };
         $readline->addMapping("", $no_echo);
         $readline->addMapping("", $no_echo);
         $step = function ($index, $label, callable $validator, $errorMessage, $default = '') use($numberOfSteps, &$readline, $resetInput, $labelMaxWidth) {
             Cursor::colorize('foreground(black) background(#cccccc)');
             do {
                 $out = $readline->readLine();
                 if (empty($out)) {
                     $out = $default;
                 }
                 $valid = $validator($out);
                 if (true !== $valid) {
                     Cursor::move('↑');
                     $resetInput($default);
                     Cursor::save();
                     Cursor::move('LEFT');
                     Cursor::move('↓', $numberOfSteps - $index + 1);
                     list($title, $message) = explode("\n", $errorMessage);
                     Cursor::colorize('foreground(white) background(red)');
                     echo $title, "\n";
                     Cursor::colorize('foreground(red) background(normal)');
                     echo $message;
                     Cursor::restore();
                 } else {
                     Cursor::save();
                     Cursor::move('LEFT');
                     Cursor::move('↓', $numberOfSteps - $index - 1);
                     Cursor::colorize('normal');
                     Cursor::clear('↓');
                     Cursor::restore();
                 }
             } while (true !== $valid);
             if ($numberOfSteps !== $index + 1) {
                 Cursor::move('→', $labelMaxWidth);
             }
             Cursor::colorize('normal');
             return $out;
         };
         $progress = function ($percent, $message) use($windowWidth) {
             static $margin = 4;
             $barWidth = $windowWidth - $margin * 2;
             Cursor::move('LEFT');
             Cursor::move('↑', 1);
             Cursor::clear('↓');
             if ($percent <= 0) {
                 $color = '#c74844';
             } elseif ($percent <= 25) {
                 $color = '#cb9a3d';
             } elseif ($percent <= 50) {
                 $color = '#dcb11e';
             } elseif ($percent <= 75) {
                 $color = '#aed633';
             } else {
                 $color = '#54b455';
             }
             echo str_repeat(' ', $margin);
             Cursor::colorize('foreground(' . $color . ') background(' . $color . ')');
             echo str_repeat('|', $percent * $barWidth / 100);
             Cursor::move('LEFT ↓');
             Cursor::colorize('background(normal)');
             echo str_repeat(' ', $margin) . $message;
             Cursor::colorize('normal');
             sleep(1);
         };
     } else {
         echo 'Installation of sabre/' . "\n" . Welcome::LOGO, "\n\n", static::getBaseURLInfo(), "\n\n";
         $step = function ($index, $label, callable $validator, $errorMessage, $default = '') use(&$readline) {
             do {
                 echo $label;
                 if (!empty($default)) {
                     echo ' [default: ', $default, ']';
                 }
                 $out = $readline->readLine(': ');
                 if (empty($out)) {
                     $out = $default;
                 }
                 $valid = $validator($out);
                 if (true !== $valid) {
                     echo $errorMessage, "\n";
                 }
             } while (true !== $valid);
             return $out;
         };
         $progress = function ($percent, $message) {
             echo $message, "\n";
         };
     }
     $form['baseUrl'] = $step(0, 'Choose the base URL', function ($baseUrl) use($verbose) {
         $valid = Installer::checkBaseUrl($baseUrl);
         if (true === $valid && true === $verbose) {
             Cursor::move('↓');
         }
         return $valid;
     }, 'Base URL must start and end by a slash' . "\n" . 'Check the Section “The base URL” on http://sabre.io/dav/gettingstarted/.', '/');
     if (false === $verbose) {
         echo 'Your administrator login: '******'password'] = $step(1, 'Choose the administrator password', function ($administratorPassword) {
         return Installer::checkPassword($administratorPassword . $administratorPassword);
     }, 'Password must not be empty' . "\n" . 'An empty password is not a password anymore!');
     $readline = $oldReadline;
     $form['email'] = $step(2, 'Choose the administrator email', function ($administratorEmail) {
         return Installer::checkEmail($administratorEmail . $administratorEmail);
     }, 'Email is invalid' . "\n" . 'The given email seems invalid.');
     $databaseDriver =& $form['database']['driver'];
     if (true === $verbose) {
         $radioReadline = new Console\Readline\Password();
         $radioReadline->addMapping('\\e[D', function () use($labelMaxWidth, &$databaseDriver) {
             $databaseDriver = 'sqlite';
             Cursor::save();
             Cursor::move('LEFT');
             Cursor::move('→', $labelMaxWidth);
             Cursor::clear('→');
             echo '🔘 SQLite ⚪️ MySQL';
             Cursor::restore();
         });
         $radioReadline->addMapping('\\e[C', function () use($labelMaxWidth, &$databaseDriver) {
             $databaseDriver = 'mysql';
             Cursor::save();
             Cursor::move('LEFT');
             Cursor::move('→', $labelMaxWidth);
             Cursor::clear('→');
             echo '⚪️ SQLite 🔘 MySQL';
             Cursor::restore();
         });
         Cursor::hide();
         $radioReadline->readLine();
         Cursor::show();
         unset($databaseDriver);
         if ('mysql' === $form['database']['driver']) {
             echo 'Choose MySQL host:                 ', $input(), "\n", 'Choose MySQL port:                 ', $input('3306'), "\n", 'Choose MySQL username:             '******'Choose MySQL password:             '******'Choose MySQL database name:        ', $input(), "\n";
             Window::scroll('↑', 10);
             Cursor::move('↑', 10);
             $numberOfSteps = 5;
             Cursor::move('↑', $numberOfSteps);
             Cursor::move('→', $labelMaxWidth);
             Cursor::colorize('foreground(black) background(#cccccc)');
         }
     } else {
         $form['database']['driver'] = $step(3, 'Choose the database driver (sqlite or mysql)', function ($databaseDriver) {
             return in_array($databaseDriver, ['sqlite', 'mysql']);
         }, 'Database driver is invalid' . "\n" . 'Database driver must be `sqlite` or `mysql`', 'sqlite');
     }
     if ('mysql' === $form['database']['driver']) {
         $form['database']['host'] = $step(0, 'Choose MySQL host', function () {
             return true;
         }, '');
         $form['database']['port'] = $step(1, 'Choose MySQL port', function ($port) {
             return false !== filter_var($port, FILTER_VALIDATE_INT);
         }, 'Port is invalid' . "\n" . 'Port must be an integer.', '3306');
         $form['database']['username'] = $step(2, 'Choose MySQL username', function () {
             return true;
         }, '');
         $oldReadline = $readline;
         $readline = new Console\Readline\Password();
         $form['database']['password'] = $step(3, 'Choose MySQL password', function () {
             return true;
         }, '');
         $readline = $oldReadline;
         $form['database']['name'] = $step(3, 'Choose MySQL database name', function () {
             return true;
         }, '');
     }
     $readline->readLine("\n" . 'Ready to install? (Enter to continue, Ctrl-C to abort)');
     echo "\n\n";
     try {
         $progress(5, 'Create configuration file…');
         $configuration = Installer::createConfigurationFile(Server::CONFIGURATION_FILE, ['baseUrl' => $form['baseUrl'], 'database' => $form['database']]);
         $progress(25, 'Configuration file created 👍!');
         $progress(30, 'Create the database…');
         $database = Installer::createDatabase($configuration);
         $progress(50, 'Database created 👍!');
         $progress(55, 'Create administrator profile…');
         Installer::createAdministratorProfile($configuration, $database, $form['email'], $form['password']);
         $progress(75, 'Administrator profile created 👍!');
         $progress(100, 'sabre/katana is ready!');
     } catch (\Exception $e) {
         $progress(-1, 'An error occured: ' . $e->getMessage());
         if (null !== ($previous = $e->getPrevious())) {
             echo 'Underlying error: ' . $previous->getMessage();
         }
         echo "\n", 'You are probably likely to run: ' . '`make uninstall` before trying again.', "\n";
         return 2;
     }
     list($dirname) = Uri\split($form['baseUrl']);
     echo "\n\n", 'The administration interface will be found at this path: ', '<your website>', $dirname, '/admin.php.', "\n";
     Window::setTitle($oldTitle);
 }
Exemplo n.º 5
0
 /**
  * Read, edit, bind… a line from STDIN.
  *
  * @param   string  $prefix    Prefix.
  * @return  string
  */
 public function readLine($prefix = null)
 {
     static $_rl = null;
     if (null === $_rl) {
         $_rl = new Console\Readline();
     }
     return $_rl->readLine($prefix);
 }