Commit 88e3eb5a authored by Sean Madsen's avatar Sean Madsen

refactor continued - docs:list now working

parent 03f502a8
......@@ -4,21 +4,35 @@ parameters:
# parameter_name: value
services:
library:
class: AppBundle\Model\Library
arguments: ['%kernel.root_dir%/config/books']
book.loader:
class: AppBundle\Utils\BookLoader
arguments: ['%kernel.root_dir%/config/books']
github.hook.processor:
class: AppBundle\Utils\GitHubHookProcessor
arguments: ['@publisher', '@book.loader']
publisher:
class: AppBundle\Utils\Publisher
arguments: ['@request_stack', '@publish.logger', '@filesystem', %kernel.root_dir%/config/books, %publisher_repos_dir%, %kernel.root_dir%/../web/static]
publish.logger:
class: Monolog\Logger
arguments: ['publish', ['@streamhandler']]
streamhandler:
class: Monolog\Handler\StreamHandler
arguments: [%kernel.logs_dir%/publish.log]
library:
class: AppBundle\Model\Library
arguments:
- %kernel.root_dir%/config/books
github.hook.processor:
class: AppBundle\Utils\GitHubHookProcessor
arguments:
- '@publisher'
- '@library'
publisher:
class: AppBundle\Utils\Publisher
arguments:
- '@request_stack'
- '@publish.logger'
- '@filesystem'
- %kernel.root_dir%/config/books
- %publisher_repos_dir%
- %kernel.root_dir%/../web/static
publish.logger:
class: Monolog\Logger
arguments:
- 'publish'
- ['@streamhandler']
streamhandler:
class: Monolog\Handler\StreamHandler
arguments:
- %kernel.logs_dir%/publish.log
......@@ -16,11 +16,11 @@ class DocsListCommand extends ContainerAwareCommand {
}
protected function execute(InputInterface $input, OutputInterface $output) {
/** @var \AppBundle\BookLoader $books */
$books = $this->getContainer()->get('book.loader');
/** @var \AppBundle\Model\Library $library */
$library = $this->getContainer()->get('library');
$table = new Table($output);
$table->setHeaders(array('book', 'lang', 'repo', 'branch'));
$table->addRows($books->findAsList());
$table->setHeaders(array('book', 'language', 'repo', 'branch'));
$table->addRows($library->booksAsTable());
$table->render();
}
......
......@@ -2,15 +2,10 @@
namespace AppBundle\Model;
class Language {
/**
* @var string $LOCALES_DIR the path to the symfony directory containing
* locale information in the form of json files.
* TODO: is there a better way to determine this
* path?
*/
const LOCALES_DIR = '../vendor/symfony/symfony/src/Symfony/Component/Intl/Resources/data/locales';
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use AppBundle\Utils\LocaleTools;
class Language extends ContainerAwareCommand {
/**
*
......@@ -32,7 +27,10 @@ class Language {
public $code;
/**
*
* Initialize a language with values in it. This function is separate from the
* constructor function only so that we have have a "language" service which
* gets the $localesDir passed in when the service is retrieved.
*
* @param string $code two letter language code
* @param array $yaml language data from a book's yaml file
*/
......@@ -53,29 +51,13 @@ class Language {
}
}
/**
* Returns the name of a language, in another language. For example, if
* $languageCode = 'en' and $localeCode = 'es' this function would answer the
* question "what word do Spanish-speaking people use to refer to English?"
*
* @param type $languageCode The language we're asking about
* @param type $localeCode The language in which we want our answer
*
* @return string
*/
private static function getLaguageNameInLocale($languageCode, $localeCode) {
$localesFiles = self::LOCALES_DIR . "/$localeCode.json";
$locales = json_decode(file_get_contents($localesFiles), TRUE);
return $locales['Names'][$languageCode];
}
/**
* The name of the language, in English (e.g. "Spanish")
*
* @return string
*/
public function englishName() {
return $this::getLaguageNameInLocale($this->code, 'en');
return LocaleTools::getLaguageNameInLocale($this->code, 'en');
}
/**
......@@ -84,7 +66,7 @@ class Language {
* @return string
*/
public function nativeName() {
return $this::getLaguageNameInLocale($this->code, $this->code);
return LocaleTools::getLaguageNameInLocale($this->code, $this->code);
}
public function descriptiveName() {
......
......@@ -5,17 +5,17 @@ namespace AppBundle\Model;
use Symfony\Component\Finder\Finder;
class Library {
/**
*
* @var array $books An array (without keys) of Book objects to represent all
* @var array $books An array (without keys) of Book objects to represent all
* the books in the system.
*/
public $books;
/**
* Build a new Library based on a directory of book conf files.
*
*
* @param type $configDir
*/
public function __construct($configDir) {
......@@ -31,10 +31,10 @@ class Library {
/**
* Compares 2 books, side by side, for the purpose of sorting an array of
* books with uasort()
*
*
* @param Book $a
* @param Book $b
* @return int - Negative when $a comes before $b.
* @return int - Negative when $a comes before $b.
* Zero when $a and $b have identical sort orders.
* Positive when $b comes before $a.
*/
......@@ -42,7 +42,7 @@ class Library {
$weightDiff = $a->weight - $b->weight;
return ($weightDiff != 0) ? $weightDiff : strnatcmp($a->name, $b->name);
}
/**
* Modifies $this->books by sorting the array of books correctly
*/
......@@ -51,4 +51,34 @@ class Library {
uasort($this->books, ['self', 'compareBooksBySortOrder']);
}
}
/**
* @return array Book data in 2-dimensional array format.
* Used for the docs:list command.
*
* Each item in the array contains keys:
* - book: string (ex: 'dev')
* - lang: string (ex: 'en')
* - repo: string (ex: 'https://example.com/dev.git')
* - branch: string (ex: 'master')
*/
public function booksAsTable() {
$rows = array();
foreach ($this->books as $book) {
foreach ($book->languages as $language) {
foreach ($language->versions as $version) {
$key = "$book->slug/$language->code/$version->branch";
$row = array(
'book' => $book->name,
'language' => $language->englishName(),
'repo' => $language->repo,
'branch' => $version->branch,
);
$rows[$key] = $row;
}
}
}
return $rows;
}
}
<?php
namespace AppBundle\Utils;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Parser;
class BookLoader {
/**
* @var string
*/
private $configDir;
/**
* @var array
*/
private $cache;
/**
* Books constructor.
* @param string $configDir
*/
public function __construct($configDir) {
$this->configDir = $configDir;
}
/**
* Compares 2 books, side by side, for the purpose of sorting an array of
* books with uasort()
* @param array $bookA
* @param array $bookB
* @return int
*/
// private function compareBooksBySortOrder($bookA, $bookB) {
// $aWeight = isset($bookA['weight']) ? $bookA['weight'] : 0;
// $bWeight = isset($bookB['weight']) ? $bookB['weight'] : 0;
// if ($aWeight == $bWeight) {
// return strnatcmp($bookA['name'], $bookB['name']);
// }
// return $aWeight - $bWeight;
// }
/**
* Modifies the internal book cache by sorting the array of books correctly
*/
// private function cacheSort() {
// if (isset($this->cache)) {
// uasort($this->cache, [$this, 'compareBooksBySortOrder']);
// }
// }
/**
* Modifies the book passed in by inserting new elements into the book's
* array structure that provide additional information about the book, such
* as the number of languages, etc.
*
* @param array &$book an array of properties which represent a book
*/
// private function addStatsToBook(&$book) {
// $this->addToBookDistinctVersions($book);
// $this->addToBookIsMultiVersion($book);
// $this->addToBookIsMultiLang($book);
// $this->addToBookStableLangs($book);
// $this->addtoBookIsMultiStableLang($book);
// }
/**
* Adds 'distinct_versions' element to a book which lists all the distinct
* versions. If 'stable' and 'latest' both point to 'master', then it will
* list only 'stable' in the distinct versions.
*
* @param array &$book an array of properties which represent a book
*
*/
// private function addToBookDistinctVersions(&$book) {
// foreach ($book['langs'] as &$lang) {
// $lang['distinct_versions']['latest'] = $lang['latest'];
// if (isset($lang['stable']) && $lang['stable'] != $lang['latest']) {
// $lang['distinct_versions']['stable'] = $lang['stable'];
// }
// if (isset($lang['history'])) {
// foreach ($lang['history'] as $version) {
// $key = (string) $version;
// $lang['distinct_versions'][$key] = $version;
// }
// }
// }
// }
/**
* Adds 'is_multi_version' to all language elements of a book to say
* whether the language has multiple versions that are different from one
* another
*
* @param array &$book an array of properties which represent a book
*/
// private function addToBookIsMultiVersion(&$book) {
// foreach ($book['langs'] as &$lang) {
// $lang['is_multi_version'] = count($lang['distinct_versions']) > 1 ? 1 : 0;
// }
// }
/**
* Adds 'is_multi_lang' element to a book to say whether the book has
* multiple languages.
*
* @param array &$book an array of properties which represent a book
*/
// private function addToBookIsMultiLang(&$book) {
// $book['is_multi_lang'] = count($book['langs']) > 1 ? 1 : 0;
// }
/**
* Adds 'stable_langs' element to a book -- an array of all the languages
* which have a stable version defined.
*
* @param array &$book an array of properties which represent a book
*/
// private function addToBookStableLangs(&$book) {
// foreach ($book['langs'] as $lang => $language) {
// if (isset($language['stable'])) {
// $book['stable_langs'][$lang] = $language;
// }
// }
// }
/**
* Adds 'is_multi_stable_lang' element to a book to say whether the book has
* multiple languages which have stable versions defined.
*
* @param array &$book an array of properties which represent a book
*/
// private function addToBookIsMultiStableLang(&$book) {
// if (!isset($book['stable_langs'])) {
// $this->addToBookStableLangs($book);
// }
// $book['is_multi_stable_lang'] = count($book['stable_langs']) > 1 ? 1 : 0;
// }
/**
* Find all the books
* Fills the private $cache variable with an array of books, as they are
* defined in the yaml config files
*
* @return array all books
*/
// public function find() {
// if ($this->cache === NULL) {
// $finder = new Finder();
// $yaml = new Parser();
// $books = array();
// foreach ($finder->in($this->configDir)
// ->name("*.yml") as $file) {
// $books[basename($file, '.yml')] = $yaml->parse(file_get_contents("$file"));
// }
// foreach ($books as &$book) {
// $this->addStatsToBook($book);
// }
// $this->cache = $books;
// $this->cacheSort();
// }
// return $this->cache;
// }
/**
* Get the list of books as a flat list of (book,lang,repo,branch) pairs.
*
* @return array
* Each item in the array contains keys:
* - book: string (ex: 'dev')
* - lang: string (ex: 'en')
* - repo: string (ex: 'https://example.com/dev.git')
* - branch: string (ex: 'master')
*/
public function findAsList() {
$rows = array();
foreach ($this->find() as $bookName => $book) {
foreach ($book['langs'] as $lang => $langSpec) {
foreach ($this->getBranches($book, $lang) as $branch) {
$key = "$bookName/$lang/$branch";
$row = array(
'book' => $bookName,
'lang' => $lang,
'repo' => $langSpec['repo'],
'branch' => $branch,
);
$rows[$key] = $row;
}
}
}
return $rows;
}
/**
* Get a list of all branches declared for this book.
*
* @param array $book
* @param string $lang
* @return array
* List of branch names which apply to this book.
*/
public function getBranches($book, $lang) {
$langSpec = $book['langs'][$lang];
$branches = array();
foreach (array('latest', 'stable', 'history') as $key) {
if (!isset($langSpec[$key])) {
continue;
}
elseif (is_array($langSpec[$key])) {
$branches = array_merge($branches, $langSpec[$key]);
}
else {
$branches[] = $langSpec[$key];
}
}
$branches = array_unique($branches);
sort($branches);
return $branches;
}
}
<?php
namespace AppBundle\Utils;
define('locales_dir', __DIR__ . '/../../../vendor/symfony/symfony/src/Symfony/Component/Intl/Resources/data/locales');
/**
* A set of helper tools for dealing with different locales
*
*/
class LocaleTools {
const LOCALES_DIR = locales_dir;
/**
* Returns the name of a language, in another language. For example, if
* $languageCode = 'en' and $localeCode = 'es' this function would answer the
* question "what word do Spanish-speaking people use to refer to English?"
*
* @param type $languageCode The language we're asking about
* @param type $localeCode The language in which we want our answer
*
* @return string
*/
public static function getLaguageNameInLocale($languageCode, $localeCode) {
$localesFiles = self::LOCALES_DIR . "/$localeCode.json";
$locales = json_decode(file_get_contents($localesFiles), TRUE);
return $locales['Names'][$languageCode];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment