Initial commit
This commit is contained in:
220
src/Helper/ExportHelper.php
Normal file
220
src/Helper/ExportHelper.php
Normal file
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 magdev
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author magdev
|
||||
* @copyright 2018 Marco Grätsch
|
||||
* @package magdev/dossier
|
||||
* @license http://opensource.org/licenses/MIT MIT License
|
||||
*/
|
||||
|
||||
namespace Magdev\Dossier\Helper;
|
||||
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Exception\RuntimeException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* Export helper class
|
||||
*
|
||||
* @author magdev
|
||||
*/
|
||||
class ExportHelper extends Helper
|
||||
{
|
||||
const JSON = 'json';
|
||||
const YAML = 'yaml';
|
||||
const PHP = 'php';
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see \Symfony\Component\Console\Helper\HelperInterface::getName()
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'export';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an assoziatve array to a tablerow-style array
|
||||
*
|
||||
* @param array $source
|
||||
* @return array
|
||||
*/
|
||||
public function arrayToTableRow(array $source): array
|
||||
{
|
||||
return array(array_values($source));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert an array of assoziatve arrays to a tablerows-style array
|
||||
*
|
||||
* @param array $source
|
||||
* @return array
|
||||
*/
|
||||
public function arrayToTableRows(array $source): array
|
||||
{
|
||||
$rows = array();
|
||||
foreach ($source as $values) {
|
||||
$rows[] = $this->arrayToTableRow($values);
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert tablerow-style array to an assoziatve array
|
||||
*
|
||||
* @param array $source
|
||||
* @return array
|
||||
*/
|
||||
public function tableRowToArray(array $source, array $keys): array
|
||||
{
|
||||
return array_combine($keys, array_shift($source));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert an array of tablerow-style arrays to an array of assoziatve arrays
|
||||
*
|
||||
* @param array $source
|
||||
* @return array
|
||||
*/
|
||||
public function tableRowsToArray(array $source, array $keys): array
|
||||
{
|
||||
$array = array();
|
||||
foreach ($source as $row) {
|
||||
$array[] = $this->tableRowToArray($row, $keys);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if an array is a tablerow-style array
|
||||
*
|
||||
* @param array $array
|
||||
* @return bool
|
||||
*/
|
||||
public function isTableRowStyle(array $source): bool
|
||||
{
|
||||
return isset($source[0]) === true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Export data to a given format
|
||||
*
|
||||
* @param array $source
|
||||
* @param string $type (json|yaml)
|
||||
* @param string $file
|
||||
* @throws \Symfony\Component\Console\Exception\RuntimeException
|
||||
* @return bool
|
||||
*/
|
||||
public function export(array $source, string $type, string $file): bool
|
||||
{
|
||||
$code = $this->convert($source, $type);
|
||||
|
||||
if ($bytes = file_put_contents($file, $code) === false) {
|
||||
throw new RuntimeException('Error while writing target: '.$file);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert data to a given format
|
||||
*
|
||||
* @param array $source
|
||||
* @param string $type
|
||||
* @throws \Symfony\Component\Console\Exception\RuntimeException
|
||||
* @return string
|
||||
*/
|
||||
public function convert(array $source, string $type): string
|
||||
{
|
||||
switch ($type) {
|
||||
case self::JSON: return $this->toJSON($source);
|
||||
case self::YAML: return $this->toYAML($source);
|
||||
case self::PHP: return $this->toPHP($source);
|
||||
|
||||
default:
|
||||
throw new RuntimeException('Invalid export type: '.strtoupper($type));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert an assoziatve array to JSON
|
||||
*
|
||||
* @param array $source
|
||||
* @param bool $pretty
|
||||
* @return string
|
||||
*/
|
||||
public function toJSON(array $source, bool $pretty = true): string
|
||||
{
|
||||
if ($pretty) {
|
||||
return json_encode($source, JSON_FORCE_OBJECT|JSON_PRETTY_PRINT);
|
||||
}
|
||||
return json_encode($source, JSON_FORCE_OBJECT);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert an assoziatve array to YAML
|
||||
*
|
||||
* @param array $source
|
||||
* @param int $depth
|
||||
* @param int $indent
|
||||
* @return string
|
||||
*/
|
||||
public function toYAML(array $source, int $depth = 3, int $indent = 2): string
|
||||
{
|
||||
return Yaml::dump($source, $depth, $indent);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert values to PHP sourcecode
|
||||
*
|
||||
* @param mixed $source
|
||||
* @param bool $includeReturn
|
||||
* @param bool $includeTags
|
||||
* @return string
|
||||
*/
|
||||
public function toPHP($source, bool $includeReturn = false, bool $includeTags = false): string
|
||||
{
|
||||
$code = '';
|
||||
if ($includeTags) {
|
||||
$code .= '<?php'.PHP_EOL;
|
||||
}
|
||||
if ($includeReturn) {
|
||||
$code .= 'return ';
|
||||
}
|
||||
$code .= var_export($source, true);
|
||||
if ($includeReturn || $includeTags) {
|
||||
$code .= ';'.PHP_EOL;
|
||||
}
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
106
src/Helper/OutputHelper.php
Normal file
106
src/Helper/OutputHelper.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 magdev
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author magdev
|
||||
* @copyright 2018 Marco Grätsch
|
||||
* @package magdev/dossier
|
||||
* @license http://opensource.org/licenses/MIT MIT License
|
||||
*/
|
||||
|
||||
namespace Magdev\Dossier\Helper;
|
||||
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Magdev\Dossier\Style\DossierStyle;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
|
||||
class OutputHelper extends Helper
|
||||
{
|
||||
/**
|
||||
* IO style object
|
||||
* @var \Magdev\Dossier\Style\DossierStyle
|
||||
*/
|
||||
private $io = null;
|
||||
|
||||
private $appName = '';
|
||||
private $appVersion = '';
|
||||
|
||||
public function __construct(string $appName = '', string $appVersion = '')
|
||||
{
|
||||
$this->appName = $appName;
|
||||
$this->appVersion = $appVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see \Symfony\Component\Console\Helper\HelperInterface::getName()
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'output';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the style helper for dossier
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
||||
* @return \Magdev\Dossier\Style\DossierStyle
|
||||
*/
|
||||
public function getIoStyle(InputInterface $input, OutputInterface $output): DossierStyle
|
||||
{
|
||||
if (!$this->io) {
|
||||
$this->addOutputStyles($input, $output);
|
||||
|
||||
$this->io = new DossierStyle($input, $output);
|
||||
$this->io->setAppName($this->appName)
|
||||
->setAppVersion($this->appVersion);
|
||||
}
|
||||
return $this->io;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add some output styles to the output object
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
||||
* @return \Magdev\Dossier\Helper\OutputHelper
|
||||
*/
|
||||
protected function addOutputStyles(InputInterface $input, OutputInterface $output): OutputHelper
|
||||
{
|
||||
$formatter = $output->getFormatter();
|
||||
$formatter->setStyle('cmd', new OutputFormatterStyle('white', 'blue', array('bold')));
|
||||
$formatter->setStyle('option', new OutputFormatterStyle('cyan', null, array('bold')));
|
||||
|
||||
$formatter->setStyle('lightmagenta', new OutputFormatterStyle('magenta', null, array('bold')));
|
||||
$formatter->setStyle('lightgreen', new OutputFormatterStyle('green', null, array('bold')));
|
||||
$formatter->setStyle('lightred', new OutputFormatterStyle('red', null, array('bold')));
|
||||
$formatter->setStyle('lightyellow', new OutputFormatterStyle('yellow', null, array('bold')));
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
94
src/Helper/SectionManagerHelper.php
Normal file
94
src/Helper/SectionManagerHelper.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 magdev
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author magdev
|
||||
* @copyright 2018 Marco Grätsch
|
||||
* @package magdev/dossier
|
||||
* @license http://opensource.org/licenses/MIT MIT License
|
||||
*/
|
||||
|
||||
namespace Magdev\Dossier\Helper;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Magdev\Dossier\Service\ConfigService;
|
||||
|
||||
/**
|
||||
* Section manager
|
||||
*
|
||||
* @author magdev
|
||||
*/
|
||||
class SectionManagerHelper extends Helper
|
||||
{
|
||||
/**
|
||||
* Configuration service
|
||||
* @var \Magdev\Dossier\Service\ConfigService
|
||||
*/
|
||||
protected $config = null;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Magdev\Dossier\Service\ConfigService $config
|
||||
*/
|
||||
public function __construct(ConfigService $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see \Symfony\Component\Console\Helper\HelperInterface::getName()
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'section_manager';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an array with section visibility settings
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input
|
||||
* @return array
|
||||
*/
|
||||
public function getDisabledSections(InputInterface $input): array
|
||||
{
|
||||
return array(
|
||||
'cover' => (bool) $input->getOption('no-cover') || $this->config->get('disable.cover', false),
|
||||
'intro' => (bool) $input->getOption('no-intro') || $this->config->get('disable.intro', false),
|
||||
'resume' => (bool) $input->getOption('no-resume') || $this->config->get('disable.resume', false),
|
||||
'cv' => (bool) $input->getOption('no-cv') || $this->config->get('disable.cv', false),
|
||||
'certs' => (bool) $input->getOption('no-certs') || $this->config->get('disable.certs', false),
|
||||
'quotes' => (bool) $input->getOption('no-quotes') || $this->config->get('disable.quotes', false),
|
||||
'toc' => (bool) $input->getOption('no-toc') || $this->config->get('disable.toc', false),
|
||||
'projects' => (bool) $input->getOption('no-projects') || $this->config->get('disable.projects', false),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user