/
ElementController.php
101 lines (91 loc) · 2.94 KB
/
ElementController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Concrete\Core\Controller;
use Concrete\Core\Filesystem\FileLocator;
use Concrete\Core\Support\Facade\Facade;
use Concrete\Core\View\BasicFileView;
use Illuminate\Filesystem\Filesystem;
abstract class ElementController extends AbstractController
{
/**
* The handle of the package defining this element.
*
* @var string|null
*/
protected $pkgHandle;
/**
* The view instance to be used when rendering the view.
*
* @var \Concrete\Core\View\BasicFileView|null
*/
protected $view;
/**
* Get the element name.
*
* @return string
*/
abstract public function getElement();
/**
* Get the view instance to be used when rendering the view.
*
* @return \Concrete\Core\View\BasicFileView
*
* @deprecated Consider using the Element class instead (see the description of the render() method for examples)
* @see \Concrete\Core\Controller\ElementController::render()
*/
public function getViewObject()
{
if ($this->view === null) {
$locator = new FileLocator(new Filesystem(), Facade::getFacadeApplication());
if ($this->pkgHandle) {
$locator->addPackageLocation($this->pkgHandle);
}
$r = $locator->getRecord(DIRNAME_ELEMENTS . '/' . $this->getElement() . '.php');
$this->view = new BasicFileView($r->getFile());
$this->view->setController($this);
}
return $this->view;
}
/**
* Get the handle of the package defining this element.
*
* @return string|null
*/
public function getPackageHandle()
{
return $this->pkgHandle;
}
/**
* Set the handle of the package defining this element.
*
* @param string|null $pkgHandle
*/
public function setPackageHandle($pkgHandle)
{
$this->pkgHandle = $pkgHandle;
}
/**
* Render the element.
*
* @deprecated Consider using the Element class instead
*
* @example <code><pre>
* // Initialize the Element (usually done in controllers or service classes)
* $currentPage = $app->make(\Concrete\Core\Http\Request::class)->getCurrentPage();
* $argumentsForElementControllerConstructor = [];
*
* /// ... with the element manager (extensible, better performance when the same element many times)
* $elementManager = $app->make(\Concrete\Core\Filesystem\ElementManager::class);
* $element = $elementManager->get('name', 'pkgHandle', $currentPage, $argumentsForElementControllerConstructor);
*
* /// ... without the element manager
* $element = new \Concrete\Core\Filesystem\Element('name', 'pkgHandle', $currentPage, $argumentsForElementControllerConstructor);
*
* // Render the Element (usually done in views)
* $element->render();
* </pre></code>
*/
public function render()
{
return $this->getViewObject()->render();
}
}