Skip to content

Commit

Permalink
Update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk committed Jul 14, 2020
1 parent 882ad84 commit dadd6ab
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 78 deletions.
22 changes: 20 additions & 2 deletions src/Command/AddMassMediaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace Sonata\MediaBundle\Command;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Sonata\Doctrine\Model\ManagerInterface;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -22,11 +25,26 @@
*/
class AddMassMediaCommand extends BaseCommand
{
/**
* @var Registry|null
*/
protected $doctrine;

/**
* @var string[]
*/
protected $setters;

public function __construct(ManagerInterface $mediaManager, Pool $pool, ?Registry $doctrine = null)
{
$this->doctrine = $doctrine;

parent::__construct($mediaManager, $pool);
}

/**
* {@inheritdoc}
*/
public function configure()
{
$this->setName('sonata:media:add-multiple')
Expand Down Expand Up @@ -104,8 +122,8 @@ protected function insertMedia(array $data, OutputInterface $output)

protected function optimize()
{
if ($this->getContainer()->has('doctrine')) {
$this->getContainer()->get('doctrine')->getManager()->getUnitOfWork()->clear();
if (null === $this->doctrine) {
$this->doctrine->getManager()->getUnitOfWork()->clear();
}
}
}
32 changes: 22 additions & 10 deletions src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,40 @@

use Sonata\Doctrine\Model\ManagerInterface;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;

/**
* This command can be used to re-generate the thumbnails for all uploaded medias.
*
* Useful if you have existing media content and added new formats.
*/
abstract class BaseCommand extends ContainerAwareCommand
abstract class BaseCommand extends Command
{
/**
* @return ManagerInterface
* @var ManagerInterface
*/
public function getMediaManager()
{
return $this->getContainer()->get('sonata.media.manager.media');
}
private $mediaManager;

/**
* @return Pool
* @var Pool
*/
public function getMediaPool()
private $pool;

public function __construct(ManagerInterface $mediaManager, Pool $pool)
{
$this->mediaManager = $mediaManager;
$this->pool = $pool;

parent::__construct();
}

public function getMediaManager(): ManagerInterface
{
return $this->mediaManager;
}

public function getMediaPool(): Pool
{
return $this->getContainer()->get('sonata.media.pool');
return $this->pool;
}
}
27 changes: 6 additions & 21 deletions src/Command/CleanMediaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Sonata\MediaBundle\Provider\FileProvider;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -29,35 +28,23 @@
/**
* @final since sonata-project/media-bundle 3.21.0
*/
class CleanMediaCommand extends Command
class CleanMediaCommand extends BaseCommand
{
/**
* @var MediaProviderInterface[]|null
*/
private $providers;

/**
* @var Pool
*/
private $mediaPool;

/**
* @var Local
*/
private $filesystemLocal;

/**
* @var MediaManagerInterface
*/
private $mediaManager;

public function __construct(Local $filesystemLocal, Pool $mediaPool, MediaManagerInterface $mediaManager)
{
parent::__construct();
parent::__construct($mediaManager, $mediaPool);

$this->filesystemLocal = $filesystemLocal;
$this->mediaPool = $mediaPool;
$this->mediaManager = $mediaManager;
}

public function configure()
Expand All @@ -78,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln(sprintf('<info>Scanning upload directory: %s</info>', $baseDirectory));

foreach ($this->mediaPool->getContexts() as $contextName => $context) {
foreach ($this->getMediaPool()->getContexts() as $contextName => $context) {
if (!$filesystem->exists($baseDirectory.'/'.$contextName)) {
$output->writeln(sprintf("<info>'%s' does not exist</info>", $baseDirectory.'/'.$contextName));

Expand Down Expand Up @@ -122,7 +109,7 @@ private function getProviders(): array
if (!$this->providers) {
$this->providers = [];

foreach ($this->mediaPool->getProviders() as $provider) {
foreach ($this->getMediaPool()->getProviders() as $provider) {
if ($provider instanceof FileProvider) {
$this->providers[] = $provider->getName();
}
Expand All @@ -134,18 +121,16 @@ private function getProviders(): array

private function mediaExists(string $filename, ?string $context = null): bool
{
$mediaManager = $this->mediaManager;

$fileParts = explode('_', $filename);

if (\count($fileParts) > 1 && 'thumb' === $fileParts[0]) {
return null !== $mediaManager->findOneBy([
return null !== $this->getMediaManager()->findOneBy([
'id' => $fileParts[1],
'context' => $context,
]);
}

return \count($mediaManager->findBy([
return \count($this->getMediaManager()->findBy([
'providerReference' => $filename,
'providerName' => $this->getProviders(),
])) > 0;
Expand Down
51 changes: 37 additions & 14 deletions src/Command/FixMediaContextCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,42 @@
namespace Sonata\MediaBundle\Command;

use Sonata\ClassificationBundle\Model\ContextInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Sonata\ClassificationBundle\Model\ContextManagerInterface;
use Sonata\MediaBundle\Model\CategoryManagerInterface;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @final since sonata-project/media-bundle 3.21.0
*/
class FixMediaContextCommand extends ContainerAwareCommand
class FixMediaContextCommand extends Command
{
/**
* @var Pool
*/
private $mediaPool;

/**
* @var CategoryManagerInterface|null
*/
private $categoryManager;

/**
* @var ContextManagerInterface|null
*/
private $contextManager;

public function __construct(Pool $mediaPool, ?CategoryManagerInterface $categoryManager = null, ?ContextManagerInterface $contextManager = null)
{
$this->mediaPool = $mediaPool;
$this->categoryManager = $categoryManager;
$this->contextManager = $contextManager;

parent::__construct();
}

public function configure()
{
$this->setName('sonata:media:fix-media-context');
Expand All @@ -31,41 +58,37 @@ public function configure()

public function execute(InputInterface $input, OutputInterface $output)
{
if (!$this->getContainer()->has('sonata.media.manager.category')) {
if (null === $this->categoryManager || null === $this->contextManager) {
throw new \LogicException('The classification feature is disabled.');
}

$pool = $this->getContainer()->get('sonata.media.pool');
$contextManager = $this->getContainer()->get('sonata.classification.manager.context');
$categoryManager = $this->getContainer()->get('sonata.media.manager.category');

foreach ($pool->getContexts() as $context => $contextAttrs) {
foreach ($this->mediaPool->getContexts() as $context => $contextAttrs) {
/** @var ContextInterface $defaultContext */
$defaultContext = $contextManager->findOneBy([
$defaultContext = $this->contextManager->findOneBy([
'id' => $context,
]);

if (!$defaultContext) {
$output->writeln(sprintf(" > default context for '%s' is missing, creating one", $context));
$defaultContext = $contextManager->create();
$defaultContext = $this->contextManager->create();
$defaultContext->setId($context);
$defaultContext->setName(ucfirst($context));
$defaultContext->setEnabled(true);

$contextManager->save($defaultContext);
$this->contextManager->save($defaultContext);
}

$defaultCategory = $categoryManager->getRootCategory($defaultContext);
$defaultCategory = $this->categoryManager->getRootCategory($defaultContext);

if (!$defaultCategory) {
$output->writeln(sprintf(" > default category for '%s' is missing, creating one", $context));
$defaultCategory = $categoryManager->create();
$defaultCategory = $this->categoryManager->create();
$defaultCategory->setContext($defaultContext);
$defaultCategory->setName(ucfirst($context));
$defaultCategory->setEnabled(true);
$defaultCategory->setPosition(0);

$categoryManager->save($defaultCategory);
$this->categoryManager->save($defaultCategory);
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/Command/MigrateToJsonTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace Sonata\MediaBundle\Command;

use Doctrine\ORM\EntityManager;
use Sonata\Doctrine\Model\ManagerInterface;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -22,6 +25,15 @@
*/
class MigrateToJsonTypeCommand extends BaseCommand
{
private $entityManager;

public function __construct(ManagerInterface $mediaManager, Pool $pool, EntityManager $entityManager)
{
$this->entityManager = $entityManager;

parent::__construct($mediaManager, $pool);
}

public function configure()
{
$this->setName('sonata:media:migrate-json');
Expand All @@ -37,7 +49,7 @@ public function execute(InputInterface $input, OutputInterface $output)
$table = $input->getOption('table');
$column = $input->getOption('column');
$columnId = $input->getOption('column_id');
$connection = $this->getContainer()->get('doctrine.orm.entity_manager')->getConnection();
$connection = $this->entityManager->getConnection();
$medias = $connection->fetchAll("SELECT * FROM $table");

foreach ($medias as $media) {
Expand Down
3 changes: 1 addition & 2 deletions src/Command/RemoveThumbsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
* This command can be used to re-generate the thumbnails for all uploaded medias.
*
* Useful if you have existing media content and added new formats.
*/
/**
*
* @final since sonata-project/media-bundle 3.21.0
*/
class RemoveThumbsCommand extends BaseCommand
Expand Down
3 changes: 1 addition & 2 deletions src/Command/SyncThumbsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
* This command can be used to re-generate the thumbnails for all uploaded medias.
*
* Useful if you have existing media content and added new formats.
*/
/**
*
* @final since sonata-project/media-bundle 3.21.0
*/
class SyncThumbsCommand extends BaseCommand
Expand Down
2 changes: 1 addition & 1 deletion src/Model/CategoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class CategoryManager implements CategoryManagerInterface
*/
private $categoryManager;

public function __construct(ManagerInterface $categoryManager)
public function __construct(?ManagerInterface $categoryManager = null)
{
$this->categoryManager = $categoryManager;
}
Expand Down
19 changes: 19 additions & 0 deletions src/Resources/config/command.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
<services>
<service id="Sonata\MediaBundle\Command\AddMassMediaCommand" class="Sonata\MediaBundle\Command\AddMassMediaCommand" public="true">
<tag name="console.command"/>
<argument type="service" id="sonata.media.manager.media"/>
<argument type="service" id="sonata.media.pool"/>
<argument type="service" id="doctrine"/>
</service>
<service id="Sonata\MediaBundle\Command\AddMediaCommand" class="Sonata\MediaBundle\Command\AddMediaCommand" public="true">
<tag name="console.command"/>
<argument type="service" id="sonata.media.manager.media"/>
<argument type="service" id="sonata.media.pool"/>
</service>
<service id="Sonata\MediaBundle\Command\CleanMediaCommand" class="Sonata\MediaBundle\Command\CleanMediaCommand" public="true">
<argument type="service" id="sonata.media.adapter.filesystem.local"/>
Expand All @@ -15,21 +20,35 @@
</service>
<service id="Sonata\MediaBundle\Command\FixMediaContextCommand" class="Sonata\MediaBundle\Command\FixMediaContextCommand" public="true">
<tag name="console.command"/>
<argument type="service" id="sonata.media.pool"/>
<argument type="service" id="sonata.media.manager.category" on-invalid="ignore"/>
<argument type="service" id="sonata.classification.manager.context" on-invalid="ignore"/>
</service>
<service id="Sonata\MediaBundle\Command\MigrateToJsonTypeCommand" class="Sonata\MediaBundle\Command\MigrateToJsonTypeCommand" public="true">
<tag name="console.command"/>
<argument type="service" id="sonata.media.manager.media"/>
<argument type="service" id="sonata.media.pool"/>
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>
<service id="Sonata\MediaBundle\Command\RefreshMetadataCommand" class="Sonata\MediaBundle\Command\RefreshMetadataCommand" public="true">
<tag name="console.command"/>
<argument type="service" id="sonata.media.manager.media"/>
<argument type="service" id="sonata.media.pool"/>
</service>
<service id="Sonata\MediaBundle\Command\RemoveThumbsCommand" class="Sonata\MediaBundle\Command\RemoveThumbsCommand" public="true">
<tag name="console.command"/>
<argument type="service" id="sonata.media.manager.media"/>
<argument type="service" id="sonata.media.pool"/>
</service>
<service id="Sonata\MediaBundle\Command\SyncThumbsCommand" class="Sonata\MediaBundle\Command\SyncThumbsCommand" public="true">
<tag name="console.command"/>
<argument type="service" id="sonata.media.manager.media"/>
<argument type="service" id="sonata.media.pool"/>
</service>
<service id="Sonata\MediaBundle\Command\UpdateCdnStatusCommand" class="Sonata\MediaBundle\Command\UpdateCdnStatusCommand" public="true">
<tag name="console.command"/>
<argument type="service" id="sonata.media.manager.media"/>
<argument type="service" id="sonata.media.pool"/>
</service>
</services>
</container>
Loading

0 comments on commit dadd6ab

Please sign in to comment.