Skip to content

Commit

Permalink
Replaced usage of OutputInterface by LoggerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jan 2, 2015
1 parent 9c9817a commit bdacf9d
Show file tree
Hide file tree
Showing 35 changed files with 163 additions and 131 deletions.
9 changes: 9 additions & 0 deletions bin/couscous
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Couscous\Application\ContainerFactory;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\ConsoleOutput;

if (version_compare(phpversion(), '5.4', '<')) {
die('You must use PHP >= 5.4 in order to use Couscous. Please upgrade your PHP version.');
Expand All @@ -22,9 +23,17 @@ if (isset($include)) {
require_once __DIR__ . '/../../../autoload.php';
}

$output = new ConsoleOutput();

$factory = new ContainerFactory();
$container = $factory->createContainer();

// Set the logger
$logger = $container->make('Symfony\Component\Console\Logger\ConsoleLogger', [
'output' => $output,
]);
$container->set('Psr\Log\LoggerInterface', $logger);

/** @var Application $application */
$application = $container->get('application');
$application->run();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"erusev/parsedown-extra": "~0.2.0",
"phine/phar": "~1.0",
"mnapoli/front-yaml": "~1.0",
"mnapoli/php-di": "~4.4"
"mnapoli/php-di": "~4.4",
"psr/log": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.3"
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Cli/PreviewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private function startWebServer(InputInterface $input, OutputInterface $output,
$process = $builder->getProcess();
$process->start();

$output->writeln(sprintf("Server running on <info>%s</info>", $input->getArgument('address')));
$output->writeln(sprintf("Server running on <comment>%s</comment>", $input->getArgument('address')));
}

private function isSupported()
Expand Down
4 changes: 2 additions & 2 deletions src/Application/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Couscous\Application;

use DI\Container;
use DI\ContainerBuilder;
use Interop\Container\ContainerInterface;

/**
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class ContainerFactory
{
/**
* @return ContainerInterface
* @return Container
*/
public function createContainer()
{
Expand Down
17 changes: 16 additions & 1 deletion src/Application/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use Interop\Container\ContainerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\OutputInterface;

return [

Expand Down Expand Up @@ -37,7 +39,7 @@
}),

'application' => DI\factory(function (ContainerInterface $c) {
$application = new Application('Couscous', '1.0-dev');
$application = new Application('Couscous');

$application->add($c->get('Couscous\Application\Cli\GenerateCommand'));
$application->add($c->get('Couscous\Application\Cli\PreviewCommand'));
Expand All @@ -52,4 +54,17 @@
'Mni\FrontYAML\Markdown\MarkdownParser' => DI\object('Mni\FrontYAML\Bridge\Parsedown\ParsedownParser')
->constructor(DI\link('ParsedownExtra')),

'Symfony\Component\Console\Logger\ConsoleLogger' => DI\object()
->constructorParameter('verbosityLevelMap', [
// Custom verbosity map
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE,
LogLevel::DEBUG => OutputInterface::VERBOSITY_VERY_VERBOSE,
]),

];
23 changes: 16 additions & 7 deletions src/Module/Bower/Step/RunBowerInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Couscous\CommandRunner\CommandRunner;
use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
Expand All @@ -25,27 +25,36 @@ class RunBowerInstall implements Step
*/
private $commandRunner;

public function __construct(Filesystem $filesystem, CommandRunner $commandRunner)
{
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(
Filesystem $filesystem,
CommandRunner $commandRunner,
LoggerInterface $logger
) {
$this->filesystem = $filesystem;
$this->commandRunner = $commandRunner;
$this->logger = $logger;
}

public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
if ($repository->regenerate || !$this->hasBowerJson($repository)) {
if ($repository->regenerate || ! $this->hasBowerJson($repository)) {
return;
}

$output->writeln('Executing <info>bower install</info>');
$this->logger->notice('Executing "bower install"');

$result = $this->commandRunner->run(sprintf(
'cd "%s" && bower install',
$repository->metadata['template.directory']
));

if ($result) {
$output->writeln($result);
$this->logger->info($result);
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/Module/Config/Step/LoadConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
Expand All @@ -28,18 +28,24 @@ class LoadConfig implements Step
*/
private $yamlParser;

public function __construct(Filesystem $filesystem, Parser $yamlParser)
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(Filesystem $filesystem, Parser $yamlParser, LoggerInterface $logger)
{
$this->filesystem = $filesystem;
$this->yamlParser = $yamlParser;
$this->logger = $logger;
}

public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
$filename = $repository->sourceDirectory . '/' . self::FILENAME;

if (! $this->filesystem->exists($filename)) {
$output->writeln("<comment>No couscous.yml configuration file found, using default config</comment>");
$this->logger->notice('No couscous.yml configuration file found, using default config');
return;
}

Expand Down
4 changes: 1 addition & 3 deletions src/Module/Config/Step/OverrideBaseUrlForPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Couscous\Module\Config\Step;

use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Override the baseUrl if we are in preview.
Expand All @@ -13,7 +11,7 @@
*/
class OverrideBaseUrlForPreview implements \Couscous\Step
{
public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
if ($repository->metadata['preview'] === true) {
$repository->metadata['baseUrl'] = '';
Expand Down
3 changes: 1 addition & 2 deletions src/Module/Config/Step/SetDefaultConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Set the default config.
Expand All @@ -19,7 +18,7 @@ class SetDefaultConfig implements Step
],
];

public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
$repository->metadata->setMany($this->defaultConfig);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Module/Core/Step/ClearTargetDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

Expand All @@ -25,7 +24,7 @@ public function __construct(Filesystem $filesystem)
$this->filesystem = $filesystem;
}

public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
$files = new Finder();
$files->in($repository->targetDirectory);
Expand Down
26 changes: 15 additions & 11 deletions src/Module/Core/Step/WriteFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
Expand All @@ -19,28 +19,32 @@ class WriteFiles implements Step
*/
private $filesystem;

public function __construct(Filesystem $filesystem)
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(Filesystem $filesystem, LoggerInterface $logger)
{
$this->filesystem = $filesystem;
$this->logger = $logger;
}

public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
foreach ($repository->getFiles() as $file) {
$targetFilename = $repository->targetDirectory . '/' . $file->relativeFilename;

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln("Writing $targetFilename");
}

if ($this->filesystem->exists($targetFilename)) {
$output->writeln(sprintf(
"<comment>Skipping '%s' because a file with the same name already exists</comment>",
$file->relativeFilename
));
$this->logger->info(
"Skipping '{file}' because a file with the same name already exists",
['file' => $file->relativeFilename]
);
continue;
}

$this->logger->debug('Writing {file}', ['file' => $targetFilename]);

$this->filesystem->dumpFile($targetFilename, $file->getContent());
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Module/Markdown/Step/LoadMarkdownFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
use Couscous\Module\Markdown\Model\MarkdownFile;
use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\SplFileInfo;

/**
* Loads Markdown files in memory.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class LoadMarkdownFiles implements \Couscous\Step
class LoadMarkdownFiles implements Step
{
public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
$files = $repository->sourceFiles();
$files->name('*.md');
Expand Down
5 changes: 2 additions & 3 deletions src/Module/Markdown/Step/ParseMarkdownFrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
use Couscous\Model\Repository;
use Couscous\Step;
use Mni\FrontYAML\Parser;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Parse Markdown front matter to load file metadata.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class ParseMarkdownFrontMatter implements \Couscous\Step
class ParseMarkdownFrontMatter implements Step
{
/**
* @var Parser
Expand All @@ -25,7 +24,7 @@ public function __construct(Parser $markdownParser)
$this->markdownParser = $markdownParser;
}

public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
/** @var MarkdownFile[] $markdownFiles */
$markdownFiles = $repository->findFilesByType('Couscous\Module\Markdown\Model\MarkdownFile');
Expand Down
5 changes: 2 additions & 3 deletions src/Module/Markdown/Step/ProcessMarkdownFileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
use Couscous\Module\Markdown\Model\MarkdownFile;
use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Processes the name of Markdown files.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class ProcessMarkdownFileName implements \Couscous\Step
class ProcessMarkdownFileName implements Step
{
public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
/** @var MarkdownFile[] $markdownFiles */
$markdownFiles = $repository->findFilesByType('Couscous\Module\Markdown\Model\MarkdownFile');
Expand Down
5 changes: 2 additions & 3 deletions src/Module/Markdown/Step/RenderMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
use Couscous\Model\Repository;
use Couscous\Step;
use Mni\FrontYAML\Parser;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Turns Markdown to HTML.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class RenderMarkdown implements \Couscous\Step
class RenderMarkdown implements Step
{
/**
* @var Parser
Expand All @@ -26,7 +25,7 @@ public function __construct(Parser $markdownParser)
$this->markdownParser = $markdownParser;
}

public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
/** @var MarkdownFile[] $markdownFiles */
$markdownFiles = $repository->findFilesByType('Couscous\Module\Markdown\Model\MarkdownFile');
Expand Down
3 changes: 1 addition & 2 deletions src/Module/Markdown/Step/RewriteMarkdownLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Couscous\Module\Markdown\Model\MarkdownFile;
use Couscous\Model\Repository;
use Couscous\Step;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Rewrites links from *.md to *.html.
Expand All @@ -18,7 +17,7 @@ class RewriteMarkdownLinks implements Step
const MARKDOWN_LINK_REGEX = '#\[([^\]]+)\]\(([^\)]+)\.md\)#';
const REGEX_REPLACEMENT = '[$1]($2.html)';

public function __invoke(Repository $repository, OutputInterface $output)
public function __invoke(Repository $repository)
{
/** @var MarkdownFile[] $markdownFiles */
$markdownFiles = $repository->findFilesByType('Couscous\Module\Markdown\Model\MarkdownFile');
Expand Down
Loading

0 comments on commit bdacf9d

Please sign in to comment.