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

Support shared site.contentType.php controller #6950

Merged
Merged
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
25 changes: 11 additions & 14 deletions src/Cms/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,29 +464,26 @@ public function controller(
$data = [];

// always use the site controller as defaults, if available
if ($controller = $this->controllerLookup('site')) {
$data = (array)$controller->call($this, $arguments);
$site = $this->controllerLookup('site', $contentType);
$site ??= $this->controllerLookup('site');
distantnative marked this conversation as resolved.
Show resolved Hide resolved

if ($site !== null) {
$data = (array)$site->call($this, $arguments);
}

// try to find a specific representation controller
if ($controller = $this->controllerLookup($name, $contentType)) {
$controller = $this->controllerLookup($name, $contentType);
// no luck for a specific representation controller?
// let's try the html controller instead
$controller ??= $this->controllerLookup($name);

if ($controller !== null) {
return [
...$data,
...(array)$controller->call($this, $arguments)
];
}

if ($contentType !== 'html') {
// no luck for a specific representation controller?
// let's try the html controller instead
if ($controller = $this->controllerLookup($name)) {
return [
...$data,
...(array)$controller->call($this, $arguments)
];
}
}

return $data;
}

Expand Down
63 changes: 46 additions & 17 deletions tests/Cms/App/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1326,8 +1326,8 @@ public function testController()
]);

$this->assertSame([
'title' => 'Site',
'foo' => 'bar'
'site' => 'html',
'test' => 'html'
], $app->controller('test'));
}

Expand All @@ -1342,17 +1342,20 @@ public function testControllerCallback()
'index' => '/dev/null'
],
'controllers' => [
'test' => fn () => ['foo' => 'bar']
'test' => fn () => ['homer' => 'simpson']

]
]);

Page::factory([
'slug' => 'test',
'slug' => 'test',
'template' => 'test'
]);

$this->assertSame(['foo' => 'bar'], $app->controller('test'));
$this->assertSame(
['homer' => 'simpson'],
$app->controller('test')
);
}

/**
Expand All @@ -1369,22 +1372,21 @@ public function testControllerRepresentation()
]);

Page::factory([
'slug' => 'test',
'slug' => 'test',
'template' => 'another'
]);

ob_start();
$app->controller('another.json', [], 'json');
distantnative marked this conversation as resolved.
Show resolved Hide resolved
$response = ob_get_clean();

$this->assertSame('{"foo":"bar"}', $response);
$this->assertSame([
'site' => 'json',
'another' => 'json'
], $app->controller('another', contentType: 'json'));
}

/**
* @covers ::controller
* @covers ::controllerLookup
*/
public function testControllerHtmlRepresentation()
public function testControllerHtmlForMissingRepresentation()
{
$app = new App([
'roots' => [
Expand All @@ -1399,16 +1401,40 @@ public function testControllerHtmlRepresentation()
]);

$this->assertSame([
'title' => 'Site',
'foo' => 'bar'
], $app->controller('test', [], 'json'));
'site' => 'json',
'test' => 'html'
], $app->controller('test', contentType: 'json'));
}

/**
* @covers ::controller
* @covers ::controllerLookup
*/
public function testControllerMissing()
{
$app = new App([
'roots' => [
'index' => '/dev/null',
'controllers' => static::FIXTURES . '/controllers'
]
]);

Page::factory([
'slug' => 'test',
'template' => 'none'
]);

$this->assertSame(
['site' => 'html'],
$app->controller('none')
);
}

/**
* @covers ::controller
* @covers ::controllerLookup
*/
public function testControllerFallbackRepresentation()
public function testControllerMissingRepresentation()
{
$app = new App([
'roots' => [
Expand All @@ -1422,7 +1448,10 @@ public function testControllerFallbackRepresentation()
'template' => 'none'
]);

$this->assertSame(['title' => 'Site'], $app->controller('none', [], 'json'));
$this->assertSame(
['site' => 'json'],
$app->controller('none', contentType: 'json')
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/Cms/App/fixtures/controllers/another.json.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<?php

echo json_encode(['foo' => 'bar']);
distantnative marked this conversation as resolved.
Show resolved Hide resolved
return function () {
return ['another' => 'json'];
};
5 changes: 5 additions & 0 deletions tests/Cms/App/fixtures/controllers/site.json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return function () {
return ['site' => 'json'];
};
2 changes: 1 addition & 1 deletion tests/Cms/App/fixtures/controllers/site.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return function () {
return ['title' => 'Site'];
return ['site' => 'html'];
};
2 changes: 1 addition & 1 deletion tests/Cms/App/fixtures/controllers/test.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return function () {
return ['foo' => 'bar'];
return ['test' => 'html'];
};
Loading