From dd23d386eb49e9b83966223ddbfa2ef362dc82c4 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Wed, 18 Oct 2017 15:30:24 +0200 Subject: [PATCH] Use latest Repository feature to replace static model calls. --- composer.json | 2 +- .../Label/Layer/FileLabelRenderer.php | 21 ++++++++++++++++- .../Label/Layer/MarkersLabelRenderer.php | 23 +++++++++++++++++-- .../Label/Layer/OverpassLabelRenderer.php | 2 +- .../Label/Layer/ReferenceLabelRenderer.php | 23 +++++++++++++++++-- .../Label/Layer/VectorsLabelRenderer.php | 23 +++++++++++++++++-- src/Bundle/Resources/config/layers.yml | 8 +++++++ 7 files changed, 93 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index c1962f9..093bc87 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "netzmacht/contao-leaflet-libraries": "^1.0", "netzmacht/php-javascript-builder": "^1.0", "netzmacht/php-leaflet": "^1.0.2", - "netzmacht/contao-toolkit": "^3.0@dev", + "netzmacht/contao-toolkit": "dev-hotfix/3.0.0-beta2", "contao-community-alliance/meta-palettes": "^1.5", "menatwork/contao-multicolumnwizard": "^3.2", "doctrine/cache": "^1.0" diff --git a/src/Backend/Renderer/Label/Layer/FileLabelRenderer.php b/src/Backend/Renderer/Label/Layer/FileLabelRenderer.php index 6c2256a..3c3a354 100644 --- a/src/Backend/Renderer/Label/Layer/FileLabelRenderer.php +++ b/src/Backend/Renderer/Label/Layer/FileLabelRenderer.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; use Contao\FilesModel; +use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager; use Symfony\Component\Translation\TranslatorInterface as Translator; /** @@ -24,6 +25,23 @@ use Symfony\Component\Translation\TranslatorInterface as Translator; */ class FileLabelRenderer extends AbstractLabelRenderer { + /** + * Repository manager. + * + * @var RepositoryManager + */ + private $repositoryManager; + + /** + * FileLabelRenderer constructor. + * + * @param RepositoryManager $repositoryManager Repository manager. + */ + public function __construct(RepositoryManager $repositoryManager) + { + $this->repositoryManager = $repositoryManager; + } + /** * {@inheritDoc} */ @@ -37,7 +55,8 @@ class FileLabelRenderer extends AbstractLabelRenderer */ public function render(array $row, string $label, Translator $translator): string { - $file = FilesModel::findByPk($row['file']); + $repository = $this->repositoryManager->getRepository(FilesModel::class); + $file = $repository->findByPk($row['file']); if ($file) { $label .= ' (' . $file->path . ')'; diff --git a/src/Backend/Renderer/Label/Layer/MarkersLabelRenderer.php b/src/Backend/Renderer/Label/Layer/MarkersLabelRenderer.php index 442d9f0..319fb5d 100644 --- a/src/Backend/Renderer/Label/Layer/MarkersLabelRenderer.php +++ b/src/Backend/Renderer/Label/Layer/MarkersLabelRenderer.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; use Netzmacht\Contao\Leaflet\Model\MarkerModel; +use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager; use Symfony\Component\Translation\TranslatorInterface as Translator; /** @@ -24,6 +25,23 @@ use Symfony\Component\Translation\TranslatorInterface as Translator; */ final class MarkersLabelRenderer extends AbstractLabelRenderer { + /** + * Repository manager. + * + * @var RepositoryManager + */ + private $repositoryManager; + + /** + * FileLabelRenderer constructor. + * + * @param RepositoryManager $repositoryManager Repository manager. + */ + public function __construct(RepositoryManager $repositoryManager) + { + $this->repositoryManager = $repositoryManager; + } + /** * {@inheritdoc} */ @@ -37,8 +55,9 @@ final class MarkersLabelRenderer extends AbstractLabelRenderer */ public function render(array $row, string $label, Translator $translator): string { - $count = MarkerModel::countBy('pid', $row['id']); - $label .= sprintf( + $repository = $this->repositoryManager->getRepository(MarkerModel::class); + $count = $repository->countBy(['pid'], [$row['pid']]); + $label .= sprintf( ' (%s %s)', $count, $translator->trans('countEntries', [], 'contao_tl_leaflet_layer') diff --git a/src/Backend/Renderer/Label/Layer/OverpassLabelRenderer.php b/src/Backend/Renderer/Label/Layer/OverpassLabelRenderer.php index 4c291ad..f32f19a 100644 --- a/src/Backend/Renderer/Label/Layer/OverpassLabelRenderer.php +++ b/src/Backend/Renderer/Label/Layer/OverpassLabelRenderer.php @@ -29,7 +29,7 @@ final class OverpassLabelRenderer extends AbstractLabelRenderer */ protected function getLayerType(): string { - return 'vectors'; + return 'overpass'; } /** diff --git a/src/Backend/Renderer/Label/Layer/ReferenceLabelRenderer.php b/src/Backend/Renderer/Label/Layer/ReferenceLabelRenderer.php index f7fbb6f..afb5497 100644 --- a/src/Backend/Renderer/Label/Layer/ReferenceLabelRenderer.php +++ b/src/Backend/Renderer/Label/Layer/ReferenceLabelRenderer.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; use Netzmacht\Contao\Leaflet\Model\LayerModel; +use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager; use Symfony\Component\Translation\TranslatorInterface as Translator; /** @@ -24,12 +25,29 @@ use Symfony\Component\Translation\TranslatorInterface as Translator; */ final class ReferenceLabelRenderer extends AbstractLabelRenderer { + /** + * Repository manager. + * + * @var RepositoryManager + */ + private $repositoryManager; + + /** + * FileLabelRenderer constructor. + * + * @param RepositoryManager $repositoryManager Repository manager. + */ + public function __construct(RepositoryManager $repositoryManager) + { + $this->repositoryManager = $repositoryManager; + } + /** * {@inheritdoc} */ protected function getLayerType(): string { - return 'vectors'; + return 'reference'; } /** @@ -37,7 +55,8 @@ final class ReferenceLabelRenderer extends AbstractLabelRenderer */ public function render(array $row, string $label, Translator $translator): string { - $reference = LayerModel::findByPk($row['reference']); + $repository = $this->repositoryManager->getRepository(LayerModel::class); + $reference = $repository->find((int) $row['reference']); if ($reference) { $label .= ' (' . $reference->title . ')'; diff --git a/src/Backend/Renderer/Label/Layer/VectorsLabelRenderer.php b/src/Backend/Renderer/Label/Layer/VectorsLabelRenderer.php index 01f96d1..57d6b48 100644 --- a/src/Backend/Renderer/Label/Layer/VectorsLabelRenderer.php +++ b/src/Backend/Renderer/Label/Layer/VectorsLabelRenderer.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer; use Netzmacht\Contao\Leaflet\Model\VectorModel; +use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager; use Symfony\Component\Translation\TranslatorInterface as Translator; /** @@ -24,6 +25,23 @@ use Symfony\Component\Translation\TranslatorInterface as Translator; */ final class VectorsLabelRenderer extends AbstractLabelRenderer { + /** + * Repository manager. + * + * @var RepositoryManager + */ + private $repositoryManager; + + /** + * FileLabelRenderer constructor. + * + * @param RepositoryManager $repositoryManager Repository manager. + */ + public function __construct(RepositoryManager $repositoryManager) + { + $this->repositoryManager = $repositoryManager; + } + /** * {@inheritdoc} */ @@ -37,8 +55,9 @@ final class VectorsLabelRenderer extends AbstractLabelRenderer */ public function render(array $row, string $label, Translator $translator): string { - $count = VectorModel::countBy('pid', $row['id']); - $label .= sprintf( + $repository = $this->repositoryManager->getRepository(VectorModel::class); + $count = $repository->countBy(['pid'], [$row['id']]); + $label .= sprintf( ' (%s %s)', $count, $translator->trans('countEntries', [], 'contao_tl_leaflet_layer') diff --git a/src/Bundle/Resources/config/layers.yml b/src/Bundle/Resources/config/layers.yml index ce7f93f..c6dcc77 100644 --- a/src/Bundle/Resources/config/layers.yml +++ b/src/Bundle/Resources/config/layers.yml @@ -62,6 +62,8 @@ services: netzmacht.contao_leaflet.layer_label_renderer.markers: class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\MarkersLabelRenderer + arguments: + - '@netzmacht.contao_toolkit.repository_manager' tags: - { name: 'netzmacht.contao_leaflet.layer_label_renderer' } @@ -77,15 +79,21 @@ services: netzmacht.contao_leaflet.layer_label_renderer.reference: class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\ReferenceLabelRenderer + arguments: + - '@netzmacht.contao_toolkit.repository_manager' tags: - { name: 'netzmacht.contao_leaflet.layer_label_renderer' } netzmacht.contao_leaflet.layer_label_renderer.vectors: class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\VectorsLabelRenderer + arguments: + - '@netzmacht.contao_toolkit.repository_manager' tags: - { name: 'netzmacht.contao_leaflet.layer_label_renderer' } netzmacht.contao_leaflet.layer_label_renderer.file: class: Netzmacht\Contao\Leaflet\Backend\Renderer\Label\Layer\FileLabelRenderer + arguments: + - '@netzmacht.contao_toolkit.repository_manager' tags: - { name: 'netzmacht.contao_leaflet.layer_label_renderer' }