Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codestyle update #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: composer
directory: "/"
schedule:
interval: daily
time: "04:00"
open-pull-requests-limit: 10
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"nette",
"heureka",
"zbozi",
"merchant"
"merchant"
],
"license": [
"MIT"
Expand All @@ -17,8 +17,8 @@
],
"require": {
"php": ">=5.6",
"nette/di": "~2.4.0",
"nette/utils": ">=2.4.0",
"nette/di": "^3.0",
"nette/utils": "3.1.6",
"latte/latte": ">=2.4.0",
"kdyby/console": ">=2.6.0",
"sergiors/importing": "1.0.1"
Expand Down
43 changes: 19 additions & 24 deletions src/Command/FeedCommand.php
Original file line number Diff line number Diff line change
@@ -1,66 +1,61 @@
<?php

declare(strict_types=1);

namespace Mk\Feed\Command;

use Symfony\Component\Console\Command\Command,
Symfony\Component\Console\Input\InputInterface,
Symfony\Component\Console\Output\OutputInterface,
Symfony\Component\Console\Input\InputOption;

use Nette,
Mk;
use Nette\DI\Container;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class FeedCommand extends Command {
final class FeedCommand extends Command
{
private Container $container;

/** @var \Nette\DI\Container */
private $container;
/** @var mixed[] */
private array $config;

/** @var array */
private $config;

public function __construct(array $config = array(), Nette\DI\Container $container)
public function __construct(array $config, Container $container)
{
parent::__construct();

$this->container = $container;
$this->config = $config;
}

protected function configure()

protected function configure(): void
{
$this->setName('Feed:export')
->setDescription('Export product feed')
->addOption('show', 's', InputOption::VALUE_NONE, 'Print available exports')
->addOption('feed', 'f', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL);
}


protected function execute(InputInterface $input, OutputInterface $output)
{
$show = $input->getOption('show');
$feeds = $input->getOption('feed');

if ($show) {
if ($input->getOption('show')) {
$output->writeln('Available exports:');

foreach ($this->config['exports'] as $k => $v) {
if ($v) {
$output->writeln('- ' . $k);
}
}
}

$feeds = $feeds ?: array_keys($this->config['exports']);
if (count($feeds)) {
if (\count($feeds = $input->getOption('feed') ?: array_keys($this->config['exports'])) > 0) {
foreach ($feeds as $feed) {
if (!isset($this->config['exports'][$feed]) || !$this->config['exports'][$feed]) {
$output->writeln('Generator for ' . $feed . ' doesn\'t exist');
}

$generator = $this->container->getService('feed.' . $feed);

$generator->save($feed . '.xml');
$output->writeln('Feed ' . $feed . ' done');
}
}
}
}
}
72 changes: 34 additions & 38 deletions src/DI/FeedExtension.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
<?php

declare(strict_types=1);

namespace Mk\Feed\DI;

use Nette;

/**
* Class FeedExtension
* @author Martin Knor <[email protected]>
* @package Mk\Feed\DI
*/
class FeedExtension extends Nette\DI\CompilerExtension {
/** @var array */
private $defaults = array(
'exportsDir' => '%wwwDir%',
'exports' => array()
);

public function loadConfiguration()
{
parent::loadConfiguration();

$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);

$builder->addDefinition($this->prefix('storage'))
->setClass('\Mk\Feed\Storage', array($config['exportsDir']));

foreach ($config['exports'] as $export => $class) {
if (!class_exists($class)) {
}
$builder->addDefinition($this->prefix($export))
->setClass($class);

}

if (class_exists('\Symfony\Component\Console\Command\Command')) {
$builder->addDefinition($this->prefix('command'))
->setClass('Mk\Feed\Command\FeedCommand', array($config))
->addTag('kdyby.console.command');
}
}

use Mk\Feed\Command\FeedCommand;
use Mk\Feed\Storage;
use Nette\DI\CompilerExtension;
use Symfony\Component\Console\Command\Command;

final class FeedExtension extends CompilerExtension
{
/** @var string[]|mixed[] */
private array $defaults = [
'exportsDir' => '%wwwDir%',
'exports' => [],
];


public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);

$builder->addDefinition($this->prefix('storage'))
->setFactory(Storage::class, [$config['exportsDir']]);

foreach ($config['exports'] as $export => $class) {
$builder->addDefinition($this->prefix($export))
->setFactory($class);
}
if (class_exists(Command::class)) {
$builder->addDefinition($this->prefix('command'))
->setFactory(FeedCommand::class, [$config])
->addTag('kdyby.console.command');
}
}
}
187 changes: 79 additions & 108 deletions src/Generators/BaseGenerator.php
Original file line number Diff line number Diff line change
@@ -1,115 +1,86 @@
<?php

declare(strict_types=1);

namespace Mk\Feed\Generators;


use Latte\Engine;
use Mk\Feed\Storage;
use Nette\SmartObject;
use Mk\Feed\FileEmptyException;
use Mk\Feed\ItemIncompletedException;
use Mk\Feed\Storage;

abstract class BaseGenerator implements IGenerator
{
private bool $prepared = false; // if some products added

/** @var resource|bool|null temp file */
private $handle;

private Storage $storage;


public function __construct(Storage $storage)
{
$this->storage = $storage;
}


public function addItem(IItem $item)
{
if (!$this->prepared) {
$this->prepare();
}
if (!$item->validate()) {
throw new ItemIncompletedException('Item is not complete');
}

$latte = new Engine;
$xmlItem = $latte->renderToString($this->getTemplate('item'), ['item' => $item]);
fwrite($this->handle, $xmlItem);
}


/** Generate file by addItem from db for example. */
abstract public function generate(): void;


public function save(string $filename): void
{
$this->generate();
if (!$this->prepared) {
throw new FileEmptyException('File has not any items');
}

$this->prepareTemplate('footer');

$size = ftell($this->handle);
rewind($this->handle);
$this->storage->save($filename, fread($this->handle, $size));

fclose($this->handle);

$this->prepared = false;
}


abstract protected function getTemplate(string $name): string;


protected function prepare(): void
{
$this->handle = tmpfile();
$this->prepareTemplate('header');
$this->prepared = true;
}


/**
* Class BaseGenerator
* @author Martin Knor <[email protected]>
* @package Mk\Feed\Generators
*/
abstract class BaseGenerator implements IGenerator {

use SmartObject;

/** @var bool true if some products added */
private $prepared = false;

/** @var resource|bool|null temp file */
private $handle;

/** @var \Mk\Feed\Storage */
private $storage;

/**
* BaseGenerator constructor.
* @param \Mk\Feed\Storage $storage
*/
public function __construct(Storage $storage)
{
$this->storage = $storage;
}

/**
* @param $name
* @return string path to template
*/
abstract protected function getTemplate($name);


/**
* Prepare temp file
*/
protected function prepare()
{
$this->handle = tmpfile();
$this->prepareTemplate('header');
$this->prepared = true;
}

/**
* @param \Mk\Feed\Generators\IItem $item
* @throws \Exception
* @throws \Throwable
*/
public function addItem(IItem $item)
{
if (!$this->prepared) {
$this->prepare();
}

if (!$item->validate()) {
throw new ItemIncompletedException('Item is not complete');
}

$latte = new Engine;
$xmlItem = $latte->renderToString($this->getTemplate('item'), array('item' => $item));
fwrite($this->handle, $xmlItem);
}

/**
* Generate file by addItem from db for example
* @return void
*/
abstract function generate();

/**
* @param $filename
* @return void
*/
public function save($filename)
{
$this->generate();

if (!$this->prepared) {
throw new FileEmptyException('File has not any items');
}

$this->prepareTemplate('footer');

$size = ftell($this->handle);
rewind($this->handle);
$this->storage->save($filename, fread($this->handle, $size));

fclose($this->handle);

$this->prepared = false;
}

/**
* @param $template
*/
protected function prepareTemplate($template)
{
$file = $this->getTemplate($template);
$footerHandle = fopen('safe://' . $file, 'r');
$footer = fread($footerHandle, filesize($file));
fclose($footerHandle);
fwrite($this->handle, $footer);
}

}
protected function prepareTemplate(string $template): void
{
$file = $this->getTemplate($template);
$footerHandle = fopen('safe://' . $file, 'rb');
$footer = fread($footerHandle, filesize($file));
fclose($footerHandle);
fwrite($this->handle, $footer);
}
}
Loading