Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
638 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Routing; | ||
|
||
use Contao\CoreBundle\Framework\ContaoFramework; | ||
use Contao\Model\Collection; | ||
use Contao\PageModel; | ||
use Doctrine\DBAL\Connection; | ||
use Symfony\Cmf\Component\Routing\Candidates\CandidatesInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
trait CandidatePagesTrait | ||
{ | ||
/** | ||
* @var ContaoFramework | ||
*/ | ||
private $framework; | ||
|
||
/** | ||
* @var Connection | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var CandidatesInterface | ||
*/ | ||
private $candidates; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
m-vo
Member
|
||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
private function findCandidatePages(Request $request): array | ||
{ | ||
$candidates = $this->candidates->getCandidates($request); | ||
|
||
if (empty($candidates)) { | ||
return []; | ||
} | ||
|
||
$ids = []; | ||
$aliases = []; | ||
|
||
foreach ($candidates as $candidate) { | ||
if (is_numeric($candidate)) { | ||
$ids[] = (int) $candidate; | ||
} else { | ||
$aliases[] = $this->connection->quote($candidate); | ||
} | ||
} | ||
|
||
$conditions = []; | ||
|
||
if (!empty($ids)) { | ||
$conditions[] = 'tl_page.id IN ('.implode(',', $ids).')'; | ||
} | ||
|
||
if (!empty($aliases)) { | ||
$conditions[] = 'tl_page.alias IN ('.implode(',', $aliases).')'; | ||
} | ||
|
||
/** @var PageModel $pageModel */ | ||
$pageModel = $this->framework->getAdapter(PageModel::class); | ||
$pages = $pageModel->findBy([implode(' OR ', $conditions)], []); | ||
|
||
if (!$pages instanceof Collection) { | ||
return []; | ||
} | ||
|
||
return $pages->getModels(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Routing\Candidates; | ||
|
||
use Contao\CoreBundle\Routing\Page\PageRegistry; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class LocaleCandidates extends AbstractCandidates | ||
{ | ||
/** | ||
* @var PageRegistry | ||
*/ | ||
private $pageRegistry; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $initialized = false; | ||
|
||
public function __construct(PageRegistry $pageRegistry) | ||
{ | ||
parent::__construct([''], []); | ||
|
||
$this->pageRegistry = $pageRegistry; | ||
} | ||
|
||
public function getCandidates(Request $request): array | ||
{ | ||
$this->initialize(); | ||
|
||
return parent::getCandidates($request); | ||
} | ||
|
||
/** | ||
* Lazy-initialize because we don't want to query the database when creating the service. | ||
*/ | ||
private function initialize(): void | ||
{ | ||
if ($this->initialized) { | ||
return; | ||
} | ||
|
||
$this->initialized = true; | ||
|
||
$this->setUrlSuffixes($this->pageRegistry->getUrlSuffixes()); | ||
} | ||
} |
Oops, something went wrong.
I'm not sure about best practice regarding services in traits. I find it a bit strange that I have to 'share' my private variables holding service references with the trait (and I have to initialize it for it). But maybe that's how it works.
I've seen some examples in the Symfony codebase where they added a
init()
method that gets called from the constructor of the class that is using the trait (but never with a service).(If this isn't intended to be used outside the core, we should probably add
@internal
.)