Commit 37863755 authored by Sean Madsen's avatar Sean Madsen

format code to pass civilint

parent af957011
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
class AppCache extends HttpCache class AppCache extends HttpCache {
{
} }
...@@ -3,68 +3,60 @@ ...@@ -3,68 +3,60 @@
use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel class AppKernel extends Kernel {
{
public function registerBundles() public function registerBundles() {
{ $bundles = [
$bundles = [ new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(), new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(), new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(), new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(), new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new AppBundle\AppBundle(),
new AppBundle\AppBundle(), ];
];
if (in_array($this->getEnvironment(), ['dev', 'test'], TRUE)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) { return $bundles;
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle(); }
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles; public function getRootDir() {
} return __DIR__;
}
public function getRootDir() public function getCacheDir() {
{ return dirname(__DIR__) . '/var/cache/' . $this->getEnvironment();
return __DIR__; }
}
public function getCacheDir() public function getLogDir() {
{ return dirname(__DIR__) . '/var/logs';
return dirname(__DIR__).'/var/cache/'.$this->getEnvironment(); }
}
public function getLogDir() public function registerContainerConfiguration(LoaderInterface $loader) {
{ $loader->load($this->getRootDir() . '/config/config_' . $this->getEnvironment() . '.yml');
return dirname(__DIR__).'/var/logs'; }
}
public function registerContainerConfiguration(LoaderInterface $loader) protected function buildContainer() {
{ $container = parent::buildContainer();
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml'); if ($container->hasParameter('mkdocs_path')) {
$_ENV['PATH'] = $container->getParameter('mkdocs_path')
. PATH_SEPARATOR . getenv('PATH');
putenv("PATH=" . $_ENV['PATH']);
} }
if (!$container->hasParameter('publisher_repos_dir')) {
protected function buildContainer() // This isn't really a good place to put it because it gets deleted
{ // whenever you clear the cache.
$container = parent::buildContainer(); $container->setParameter('publisher_repos_dir', $container->getParameter('kernel.cache_dir') . '/repos'
if ($container->hasParameter('mkdocs_path')) { );
$_ENV['PATH'] = $container->getParameter('mkdocs_path')
. PATH_SEPARATOR . getenv('PATH');
putenv("PATH=" . $_ENV['PATH']);
}
if (!$container->hasParameter('publisher_repos_dir')) {
// This isn't really a good place to put it because it gets deleted
// whenever you clear the cache.
$container->setParameter('publisher_repos_dir',
$container->getParameter('kernel.cache_dir') . '/repos'
);
}
return $container;
} }
return $container;
}
} }
...@@ -6,7 +6,7 @@ use Composer\Autoload\ClassLoader; ...@@ -6,7 +6,7 @@ use Composer\Autoload\ClassLoader;
/** /**
* @var ClassLoader $loader * @var ClassLoader $loader
*/ */
$loader = require __DIR__.'/../vendor/autoload.php'; $loader = require __DIR__ . '/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']); AnnotationRegistry::registerLoader([$loader, 'loadClass']);
......
...@@ -4,6 +4,5 @@ namespace AppBundle; ...@@ -4,6 +4,5 @@ namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle class AppBundle extends Bundle {
{
} }
...@@ -9,23 +9,19 @@ use Symfony\Component\Console\Input\InputInterface; ...@@ -9,23 +9,19 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class DocsListCommand extends ContainerAwareCommand class DocsListCommand extends ContainerAwareCommand {
{
protected function configure()
{
$this
->setName('docs:list')
->setDescription('List available books');
}
protected function execute(InputInterface $input, OutputInterface $output) protected function configure() {
{ $this->setName('docs:list')->setDescription('List available books');
/** @var \AppBundle\BookLoader $books */ }
$books = $this->getContainer()->get('book.loader');
$table = new Table($output); protected function execute(InputInterface $input, OutputInterface $output) {
$table->setHeaders(array('book', 'lang', 'repo', 'branch')); /** @var \AppBundle\BookLoader $books */
$table->addRows($books->findAsList()); $books = $this->getContainer()->get('book.loader');
$table->render(); $table = new Table($output);
} $table->setHeaders(array('book', 'lang', 'repo', 'branch'));
$table->addRows($books->findAsList());
$table->render();
}
} }
...@@ -8,47 +8,56 @@ use Symfony\Component\Console\Input\InputInterface; ...@@ -8,47 +8,56 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class DocsPublishCommand extends ContainerAwareCommand class DocsPublishCommand extends ContainerAwareCommand {
{
protected function configure()
{
$this
->setName('docs:publish')
->setDescription('...')
->addArgument('paths', InputArgument::IS_ARRAY,
'One or more book expressions ("book/lang/branch"). (Default: all)');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var \AppBundle\BookLoader $books */
$books = $this->getContainer()->get('book.loader');
/** @var \AppBundle\Utils\Publisher $publisher */
$publisher = $this->getContainer()->get('publisher');
$rows = $books->findAsList(); protected function configure() {
$this
->setName('docs:publish')
->setDescription('...')
->addArgument(
'paths',
InputArgument::IS_ARRAY,
'One or more book expressions ("book/lang/branch"). (Default: all)');
}
if (empty($rows)) { protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln("<error>No books found</error>"); /** @var \AppBundle\BookLoader $books */
} $books = $this->getContainer()->get('book.loader');
$rowKeys = $input->getArgument('paths') ? $input->getArgument('paths') : array_keys($rows); /** @var \AppBundle\Utils\Publisher $publisher */
$publisher = $this->getContainer()->get('publisher');
foreach ($rowKeys as $rowKey) { $rows = $books->findAsList();
$row = $rows[$rowKey];
$output->writeln("");
$output->writeln(sprintf("Publish [%s/%s/%s] (from %s)",
$row['book'], $row['lang'], $row['branch'], $row['repo']));
$publisher->publish($row['book'], $row['lang'], $row['branch']); if (empty($rows)) {
$output->writeln("<error>No books found</error>");
}
foreach ($publisher->getMessages() as $message) { if ($input->getArgument('paths')) {
$output->writeln($message['label'] . ': ' . $message['content']); $rowKeys = $input->getArgument('paths');
} }
$publisher->clearMessages(); else {
$rowKeys = array_keys($rows);
}
} foreach ($rowKeys as $rowKey) {
$row = $rows[$rowKey];
$output->writeln("");
$output->writeln(sprintf(
"Publish [%s/%s/%s] (from %s)",
$row['book'],
$row['lang'],
$row['branch'],
$row['repo'])
);
$publisher->publish($row['book'], $row['lang'], $row['branch']);
foreach ($publisher->getMessages() as $message) {
$output->writeln($message['label'] . ': ' . $message['content']);
}
$publisher->clearMessages();
} }
}
} }
...@@ -16,83 +16,93 @@ use Symfony\Component\Console\Output\OutputInterface; ...@@ -16,83 +16,93 @@ use Symfony\Component\Console\Output\OutputInterface;
* This is a dumb wrapper around `mkdocs serve`. It basically just cd's into the * This is a dumb wrapper around `mkdocs serve`. It basically just cd's into the
* appropriate folder and then passes-through the args. * appropriate folder and then passes-through the args.
*/ */
class DocsServeCommand extends ContainerAwareCommand class DocsServeCommand extends ContainerAwareCommand {
{
protected function configure() protected function configure() {
{ $this->setName('docs:serve');
$this
->setName('docs:serve') $this->setDescription('Run the builtin docs development server');
->setDescription('Run the builtin docs development server')
->addOption('dev-addr', 'a', InputOption::VALUE_OPTIONAL, $this->addOption('dev-addr', 'a', InputOption::VALUE_OPTIONAL,
'IP address and port to serve documentation') 'IP address and port to serve documentation');
->addOption('strict', 's', InputOption::VALUE_NONE,
'Enable strict mode. This will cause MkDocs to abort the build on any warnings.') $this->addOption('strict', 's', InputOption::VALUE_NONE,
->addOption('theme', 't', InputOption::VALUE_OPTIONAL, 'Enable strict mode. This will cause MkDocs to abort the build on any'
'The theme to use when building your documentation [cosmo|cyborg|readthedocs|yeti|journal|bootstrap|readable|united|simplex|flatly|spacelab|amelia|cerulean|slate|mkdocs') . 'warnings.');
->addOption('livereload', null, InputOption::VALUE_NONE,
'Enable the live reloading in the development server.') $this->addOption('theme', 't', InputOption::VALUE_OPTIONAL,
->addOption('no-livereload', null, InputOption::VALUE_NONE, 'The theme to use when building your documentation (e.g. readthedocs)');
'Enable the live reloading in the development server.')
->addOption('quiet', 'q', InputOption::VALUE_NONE, $this->addOption('livereload', NULL, InputOption::VALUE_NONE,
'Silence warnings')// ->addOption('verbose', 'v', InputOption::VALUE_NONE, 'Enable verbose output') 'Enable the live reloading in the development server.');
->addArgument('[book/lang/[branch]]', InputArgument::REQUIRED,
'A book expression (e.g. "dev/en/master" or "user/fr/4.6"). The [book] and [lang] are mandatory. If [branch] is specified, it will be checked out.') $this->addOption('no-livereload', NULL, InputOption::VALUE_NONE,
; 'Disable the live reloading in the development server.');
$this->addOption('quiet', 'q', InputOption::VALUE_NONE,
'Silence warnings');
$this->addArgument('[book/lang/[branch]]', InputArgument::REQUIRED,
'A book expression (e.g. "dev/en/master" or "user/fr/4.6"). '
. 'The [book] and [lang] are mandatory. '
. 'If [branch] is specified, it will be checked out.');
}
protected function execute(InputInterface $input, OutputInterface $output) {
if (preg_match(';^(\w+)/(\w+)?$;', $input->getArgument('[book/lang/[branch]]'), $matches)) {
$book = $matches[1];
$lang = $matches[2];
$branch = NULL;
}
elseif (preg_match(';^(\w+)/(\w+)/(\w+)$;', $input->getArgument('[book/lang/[branch]]'), $matches)) {
$book = $matches[1];
$lang = $matches[2];
$branch = $matches[3];
}
else {
$output->writeln("<error>Malformed [book/lang/branch]</error>");
return 1;
}
$repoDir = $this->getContainer()->getParameter('publisher_repos_dir') . "/$book/$lang";
if (file_exists($repoDir)) {
$output->writeln("<info>Found git repo for $book/$lang in \"$repoDir\"</info>");
}
else {
$output->writeln("<error>Failed to find git repo for $book/$lang in \"$repoDir\".</error>");
$output->writeln("<error>Perhaps you should run \"docs:publish\" first?</error>");
return 1;
}
$command = 'mkdocs serve';
foreach (array('dev-addr', 'theme') as $option) {
if ($input->getOption($option)) {
$command .= " --{$option} " . escapeshellarg($input->getOption($option));
}
}
foreach (array('strict', 'livereload', 'no-livereload', 'quiet') as $option) {
if ($input->getOption($option)) {
$command .= " --{$option}";
}
} }
protected function execute(InputInterface $input, OutputInterface $output) if ($branch) {
{ $output->writeln("<info>Check out branch \"$branch\"</info>");
if (preg_match(';^(\w+)/(\w+)?$;', $input->getArgument('[book/lang/[branch]]'), $matches)) { self::passthruOK($repoDir, "git checkout $branch");
$book = $matches[1];
$lang = $matches[2];
$branch = NULL;
} elseif (preg_match(';^(\w+)/(\w+)/(\w+)$;', $input->getArgument('[book/lang/[branch]]'), $matches)) {
$book = $matches[1];
$lang = $matches[2];
$branch = $matches[3];
} else {
$output->writeln("<error>Malformed [book/lang/branch]</error>");
return 1;
}
$repoDir = $this->getContainer()->getParameter('publisher_repos_dir') . "/$book/$lang";
if (file_exists($repoDir)) {
$output->writeln("<info>Found git repo for $book/$lang in \"$repoDir\"</info>");
} else {
$output->writeln("<error>Failed to find git repo for $book/$lang in \"$repoDir\".</error>");
$output->writeln("<error>Perhaps you should run \"docs:publish\" first?</error>");
return 1;
}
$command = 'mkdocs serve';
foreach (array('dev-addr', 'theme') as $option) {
if ($input->getOption($option)) {
$command .= " --{$option} " . escapeshellarg($input->getOption($option));
}
}
foreach (array('strict', 'livereload', 'no-livereload', 'quiet') as $option) {
if ($input->getOption($option) ) {
$command .= " --{$option}";
}
}
if ($branch) {
$output->writeln("<info>Check out branch \"$branch\"</info>");
self::passthruOK($repoDir, "git checkout $branch");
}
$output->writeln("<info>Launch \"$command\"</info>");
self::passthruOK($repoDir, $command);
} }
$output->writeln("<info>Launch \"$command\"</info>");
self::passthruOK($repoDir, $command);
}
protected function passthruOK($newcwd, $command) { protected function passthruOK($newcwd, $command) {
$oldcwd = getcwd(); $oldcwd = getcwd();
chdir($newcwd); chdir($newcwd);
passthru($command, $return); passthru($command, $return);
chdir($oldcwd); chdir($oldcwd);
if ($return) { if ($return) {
throw new \RuntimeException("Received error from command ($command)"); throw new \RuntimeException("Received error from command ($command)");
}
} }
}
} }
<?php <?php
// //
// namespace AppBundle\Controller; // namespace AppBundle\Controller;
// //
// use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; // use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
// use Symfony\Bundle\FrameworkBundle\Controller\Controller; // use Symfony\Bundle\FrameworkBundle\Controller\Controller;
// use Symfony\Component\HttpFoundation\Request; // use Symfony\Component\HttpFoundation\Request;
// //
// class DefaultController extends Controller // class DefaultController extends Controller
// { // {
// /** // /**
......
...@@ -10,75 +10,70 @@ use Symfony\Component\Finder\Finder; ...@@ -10,75 +10,70 @@ use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Parser;
//@TODO Make the errors that will occur if the slugs don't pass the regexp more obvious to the user /**
* @TODO Make the errors that will occur if the slugs don't pass the regexp
* more obvious to the user
*/
class PublishController extends Controller {
class PublishController extends Controller /**
{ * @Route("/admin/publish")
/** */
* @Route("/admin/publish") public function PublishInstructionsAction() {
*/ return $this->render('AppBundle:Publish:publish.html.twig');
public function PublishInstructionsAction() }
{
return $this->render('AppBundle:Publish:publish.html.twig'); /**
} * @Route("/admin/publish/{book}/{lang}/{branch}", requirements={
/** * "lang":"[[:alpha:]]{2}",
* @Route("/admin/publish/{book}/{lang}/{branch}", requirements={ * "book":"[[:alpha:]\-]+",
* "lang":"[[:alpha:]]{2}", * "branch":"[[:alnum:]\-\.]+",
* "book":"[[:alpha:]\-]+", * })
* "branch":"[[:alnum:]\-\.]+", */
* }) public function PublishAction(Request $request, $lang, $book, $branch) {
*/ $publisher = $this->get('publisher');
public function PublishAction(Request $request, $lang, $book, $branch) $publisher->publish($book, $lang, $branch);
{ $content['messages'] = $publisher->getMessages();
return $this->render('AppBundle:Publish:publish.html.twig', $content);
}
$publisher = $this->get('publisher');
$publisher->publish($book, $lang, $branch); /**
$content['messages'] = $publisher->getMessages(); * @Route("/admin/listen")
return $this->render('AppBundle:Publish:publish.html.twig', $content); */
} public function ListenAction(Request $request) {
$body = $request->getContent();
$event = $request->headers->get('X-GitHub-Event');
/** $payload = json_decode($body);
* @Route("/admin/listen")
*/
public function ListenAction(Request $request)
{
$body = $request->getContent();
$event = $request->headers->get('X-GitHub-Event');
$payload = json_decode($body);
$books = $this->get('book.loader')->find(); $books = $this->get('book.loader')->find();
$processor = $this->get('github.hook.processor'); $processor = $this->get('github.hook.processor');
$processor->process($event, $payload); $processor->process($event, $payload);
if(!$processor->published){ if (!$processor->published) {
return new Response('Something went wrong during publishing.', 200); // @TODO Add more appropriate error code return new Response('Something went wrong during publishing.', 200);
} // @TODO Add more appropriate error code
$messages = $processor->getMessages(); }
$subject = $processor->getSubject(); $messages = $processor->getMessages();
$recipients = $processor->getRecipients(); $subject = $processor->getSubject();
$recipients = $processor->getRecipients();
$mail = \Swift_Message::newInstance()
$mail = \Swift_Message::newInstance()
->setSubject("[CiviCRM docs] $subject") ->setSubject("[CiviCRM docs] $subject")
->setFrom('no-reply@civicrm.org') ->setFrom('no-reply@civicrm.org')
->setTo($recipients) ->setTo($recipients)
->setBody( ->setBody(
$this->renderView( $this->renderView(
'Emails/notify.html.twig', array( 'Emails/notify.html.twig',
'branch' => $processor->publisher->branch, array(
'book' => $processor->publisher->book, 'branch' => $processor->publisher->branch,
'lang' => $processor->publisher->lang, 'book' => $processor->publisher->book,
'messages' => $processor->publisher->getMessages() 'lang' => $processor->publisher->lang,
) 'messages' => $processor->publisher->getMessages(),
), )
'text/html' ), 'text/html'
); );
$this->get('mailer')->send($mail); $this->get('mailer')->send($mail);
return new Response($subject, 200); return new Response($subject, 200);
}
}
} }
...@@ -8,55 +8,60 @@ use Symfony\Component\Yaml\Parser; ...@@ -8,55 +8,60 @@ use Symfony\Component\Yaml\Parser;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
use Symfony\Component\Intl\ResourceBundle\RegionBundle; use Symfony\Component\Intl\ResourceBundle\RegionBundle;
class ReadController extends Controller class ReadController extends Controller {
{
/** /**
* @Route("/{path}") * @Route("/{path}")
*/ */
public function RedirectAction($path) public function RedirectAction($path) {
{ //If path is the recognised name of a book, then redirect to the
//If path is the recognised name of a book, then redirect to the stable path of the book, else redirect to the home page. //stable path of the book, else redirect to the home page.
$k = $this->get('kernel'); $k = $this->get('kernel');
$fs = $this->get('filesystem'); $fs = $this->get('filesystem');
$bookConfigFile = $k->getRootDir()."/config/books/{$path}.yml"; $bookConfigFile = $k->getRootDir() . "/config/books/{$path}.yml";
if (!$fs->exists($bookConfigFile)) { if (!$fs->exists($bookConfigFile)) {
return $this->redirect("/"); return $this->redirect("/");
}
return $this->redirect("{$path}/en/stable");
}
// $k = $this->get('kernel');
// if(substr( $path, -1)=='/'){
// return new Response(file_get_contents($k->getCacheDir()."/{$path}index.html"));
// }else{
// return new Response(file_get_contents($k->getCacheDir()."/$path"));
// }
// //return $this->render('AppBundle:Read:read.html.twig');
/**
* @Route("/")
*/
public function HomeAction()
{
$k = $this->get('kernel');
// This seems like a kind of hacky way to find the locales, but not sure what a better way would be.
$locales = json_decode(file_get_contents($k->getRootDir().'/../vendor/symfony/symfony/src/Symfony/Component/Intl/Resources/data/locales/en.json'), true);
$finder = new Finder();
$yaml = new Parser();
$books = $this->get('book.loader')->find();
return $this->render('AppBundle:Read:home.html.twig', array(
'books' => $books,
'locales' => $locales['Names'])
);
} }
return $this->redirect("{$path}/en/stable");
}
// $k = $this->get('kernel');
// if(substr( $path, -1)=='/'){
// return new Response(file_get_contents($k->getCacheDir()."/{$path}index.html"));
// }else{
// return new Response(file_get_contents($k->getCacheDir()."/$path"));
// }
// //return $this->render('AppBundle:Read:read.html.twig');
/**
* @Route("/")
*/
public function HomeAction() {
$k = $this->get('kernel');
// This seems like a kind of hacky way to find the locales, but not
// sure what a better way would be.
$locales = json_decode(file_get_contents(
$k->getRootDir()
. '/../vendor/symfony/symfony/src/Symfony/Component/Intl/Resources/data/locales/en.json'
), TRUE);
$finder = new Finder();
$yaml = new Parser();
$books = $this->get('book.loader')->find();
return $this->render('AppBundle:Read:home.html.twig', array(
'books' => $books,
'locales' => $locales['Names'])
);
}
// $k = $this->get('kernel');
// if(substr( $path, -1)=='/'){
// return new Response(file_get_contents($k->getCacheDir()."/{$path}index.html"));
// }else{
// return new Response(file_get_contents($k->getCacheDir()."/$path"));
// }
// //return $this->render('AppBundle:Read:rea