Skip to content

Commit

Permalink
Replace $GLOBALS['objPage'] in the model argument resolver (see #7000)
Browse files Browse the repository at this point in the history
Description
-----------

Follow-up on #6997

Commits
-------

4b8d029 Replace `$GLOBALS['objPage']` in the model argument resolver
188c42a Only check the pageModel if is_a($type, PageModel::class)
571bd9c Remove the special handling
9747b4c Remove a left-over comment
5c986dc Revert the changes in the ModelArgumentResolver
  • Loading branch information
leofeyer committed Mar 12, 2024
1 parent 0ffdad8 commit d23230e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 42 deletions.
11 changes: 0 additions & 11 deletions core-bundle/src/HttpKernel/ModelArgumentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\Model;
use Contao\PageModel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
Expand Down Expand Up @@ -71,16 +70,6 @@ private function fetchModel(Request $request, ArgumentMetadata $argument): Model
return $value;
}

// Special handling for pageModel that could be globally registered
if (
isset($GLOBALS['objPage'])
&& $GLOBALS['objPage'] instanceof PageModel
&& $GLOBALS['objPage']->id === (int) $value
&& is_a($type, PageModel::class, true)
) {
return $GLOBALS['objPage'];
}

/** @var Adapter<Model> $model */
$model = $this->framework->getAdapter($type);

Expand Down
31 changes: 0 additions & 31 deletions core-bundle/tests/HttpKernel/ModelArgumentResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace Contao\CoreBundle\Tests\HttpKernel;

use Contao\CoreBundle\ContaoCoreBundle;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\HttpKernel\ModelArgumentResolver;
use Contao\CoreBundle\Tests\TestCase;
use Contao\PageModel;
Expand All @@ -37,8 +36,6 @@ protected function tearDown(): void
*/
public function testResolvesTheModel(string $name, string $class): void
{
unset($GLOBALS['objPage']);

System::setContainer($this->getContainerWithContaoConfiguration());

$pageModel = $this->createMock(PageModel::class);
Expand Down Expand Up @@ -167,32 +164,4 @@ public function testChecksIfTheModelExistsIfTheArgumentIsNotNullable(): void

$this->assertSame([], $resolver->resolve($request, $metadata));
}

public function testReturnsTheGlobalPageModel(): void
{
$framework = $this->createMock(ContaoFramework::class);
$framework
->expects($this->once())
->method('initialize')
;

$framework
->expects($this->never())
->method('getAdapter')
;

$pageModel = $this->mockClassWithProperties(PageModel::class, ['id' => 42]);
$GLOBALS['objPage'] = $pageModel;

$request = Request::create('/foobar');
$request->attributes->set('pageModel', 42);
$request->attributes->set('_scope', ContaoCoreBundle::SCOPE_FRONTEND);

$metadata = new ArgumentMetadata('pageModel', PageModel::class, false, false, '', true);
$resolver = new ModelArgumentResolver($framework, $this->mockScopeMatcher());

foreach ($resolver->resolve($request, $metadata) as $model) {
$this->assertSame($pageModel, $model);
}
}
}

0 comments on commit d23230e

Please sign in to comment.