Skip to content

Commit

Permalink
Starting some console commands
Browse files Browse the repository at this point in the history
  • Loading branch information
justinrainbow committed Jul 22, 2012
1 parent 75d3b57 commit f0ab355
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 3 deletions.
7 changes: 7 additions & 0 deletions bin/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env php -d phar.readonly=0
<?php

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

$compiler = new Presque\Compiler();
$compiler->compile();
9 changes: 9 additions & 0 deletions bin/presque
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env php
<?php

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

use Presque\Console\Application;

$application = new Application();
$application->run();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"require-dev": {
"monolog/monolog": "1.*",
"mockery/mockery": "*",
"symfony/console": "2.1.*@dev"
"symfony/console": "2.1.*@dev",
"symfony/finder" : "2.1.*@dev"
},
"suggest": {
"symfony/console": "Needed in order to use the presque command"
Expand Down
17 changes: 15 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<filter>
<whitelist>
<directory>./src/Presque/</directory>
<exclude>
<file>../src/Presque/Compiler.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
50 changes: 50 additions & 0 deletions src/Presque/Command/QueueJobCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Presque\Job;

class QueueJobCommand extends Command
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('queue')
->setDescription('Adds a job to the queue')
->setDefinition(array(

))
->setHelp(<<<EOT
The <info>%command.name%</info> command adds a new job to the queue.
<info>%command.full_name%</info> queue [job] [args]
EOT
);
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{

}
}
50 changes: 50 additions & 0 deletions src/Presque/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Presque\Job;

class WorkerCommand extends Command
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('worker')
->setDescription('Starts a new Presque worker')
->setDefinition(array(

))
->setHelp(<<<EOT
The <info>%command.name%</info> command starts a new Presque worker
<info>%command.full_name%</info> queue [job] [args]
EOT
);
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{

}
}
172 changes: 172 additions & 0 deletions src/Presque/Compiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque;

use Symfony\Component\Finder\Finder;

/**
* The Compiler class compiles Presque into a phar.
*
* Based _heavily_ on the the Composer compiler class, just customized to
* be used with Presque.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @link https://github.com/composer/composer/blob/master/src/Composer/Compiler.php
*/
class Compiler
{
/**
* Compiles satis into a single phar file
*
* @param string $pharFile The full path to the file to create
*
* @throws \RuntimeException
*/
public function compile($pharFile = 'presque.phar')
{
if (file_exists($pharFile)) {
unlink($pharFile);
}

$phar = new \Phar($pharFile, 0, basename($pharFile));
$phar->setSignatureAlgorithm(\Phar::SHA1);

$phar->startBuffering();

$finders = array();

$finder = new Finder();
$finder->files()
->ignoreVCS(true)
->name('*.php')
->notName('Compiler.php')
->in(__DIR__)
;
$finders[] = $finder;

$vendorDir = __DIR__.'/../../vendor/';

$finders[] = Finder::create()
->files()
->ignoreVCS(true)
->name('*.php')
->notName('*Test.php')
->in(array(
$vendorDir.'symfony/event-dispatcher',
$vendorDir.'symfony/console',
$vendorDir.'predis/predis/lib',
$vendorDir.'monolog/monolog/src',
$vendorDir.'composer',
));

$this->addFile($phar, $vendorDir.'autoload.php');

foreach ($finders as $finder) {
foreach ($finder as $file) {
$this->addFile($phar, $file);
}
}

$this->addConsole($phar);

// Stubs
$phar->setStub($this->getStub());

$phar->stopBuffering();

$phar->compressFiles(\Phar::GZ);

$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);

unset($phar);
}

private function addFile($phar, $file, $strip = true)
{
$path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', is_string($file) ? realpath($file) : $file->getRealPath());

$content = file_get_contents($file);
if ($strip) {
$content = $this->stripWhitespace($content);
} elseif ('LICENSE' === basename($file)) {
$content = "\n".$content."\n";
}

$phar->addFromString($path, $content);
}

private function addConsole($phar)
{
$content = file_get_contents(__DIR__.'/../../bin/presque');
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);

$phar->addFromString('bin/presque', $content);
}

/**
* Removes whitespace from a PHP source string while preserving line numbers.
*
* @param string $source A PHP string
* @return string The PHP string with the whitespace removed
*/
private function stripWhitespace($source)
{
if (!function_exists('token_get_all')) {
return $source;
}

$output = '';
foreach (token_get_all($source) as $token) {
if (is_string($token)) {
$output .= $token;
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$output .= str_repeat("\n", substr_count($token[1], "\n"));
} elseif (T_WHITESPACE === $token[0]) {
// reduce wide spaces
$whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
// normalize newlines to \n
$whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
// trim leading spaces
$whitespace = preg_replace('{\n +}', "\n", $whitespace);
$output .= $whitespace;
} else {
$output .= $token[1];
}
}

return $output;
}

private function getStub()
{
return <<<'EOF'
#!/usr/bin/env php
<?php
/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Phar::mapPhar('presque.phar');
require 'phar://presque.phar/bin/presque';
__HALT_COMPILER();
EOF;
}
}
Loading

0 comments on commit f0ab355

Please sign in to comment.