Class used to hold and resolve template files
and paths in multiple supported ways.
The purpose of this class is to homogenise the
API that is used when working with template
paths coming from TypoScript, as well as serve
as a way to quickly generate default template-,
layout- and partial root paths by package.
The constructor accepts two different types of
input - anything not of those types is silently
ignored:
- a string input is assumed a package name
and will call the fillDefaultsByPackageName
value filling method.
- an array input is assumed a TypoScript-style
array of root paths in one or more of the
supported structures and will call the
fillFromTypoScriptArray method.
Either method can also be called after instance
is created, but both will overwrite any paths
you have previously configured.
/** * @return void */ public function testDetectAndRegisterAllFluidBackendModules() { $form = Form::create(); $instance = $this->getMock('FluidTYPO3\\Fluidbackend\\Service\\ConfigurationService', array('getBackendModuleTemplatePaths', 'getFormFromTemplateFile', 'registerModuleBasedOnFluxForm')); $paths = new TemplatePaths('FluidTYPO3.Fluidbackend'); $instance->expects($this->once())->method('getBackendModuleTemplatePaths')->willReturn(array('FluidTYPO3.Fluidbackend' => $paths->toArray())); $instance->expects($this->atLeastOnce())->method('registerModuleBasedOnFluxForm'); $instance->expects($this->atLeastOnce())->method('getFormFromTemplateFile')->willReturn($form); $instance->detectAndRegisterAllFluidBackendModules(); }
/** * @dataProvider getInitializeVariantsTestValues * @param array $variants * @param array $expectedVariants * @param array $expectedVersions */ public function testInitializeVariants(array $variants, array $expectedVariants, array $expectedVersions) { $templatePaths = new TemplatePaths('moox_core'); $paths = $templatePaths->toArray(); $instance = new AccessibleConfigurationService(); $instance->setRegisteredVariants($variants); $instance->setViewConfiguration($paths); $instance->initializeVariants(); $this->assertEquals($expectedVariants, $instance->getVariants()); $this->assertEquals($expectedVersions, $instance->getVersions()); }
/** * @dataProvider getInitializeVariantsTestValues * @param array $variants * @param array $expectedVariants * @param array $expectedVersions */ public function testInitializeVariants(array $variants, array $expectedVariants, array $expectedVersions) { $templatePaths = new TemplatePaths('fluidcontent_core'); $templatePaths->setTemplateRootPaths(array(ExtensionManagementUtility::extPath('fluidcontent_core', 'Tests/Fixtures/Templates/'))); $paths = $templatePaths->toArray(); $instance = new AccessibleConfigurationService(); $instance->setRegisteredVariants($variants); $instance->setViewConfiguration($paths); $instance->initializeVariants(); $this->assertEquals($expectedVariants, $instance->getVariants()); $this->assertEquals($expectedVersions, $instance->getVersions()); }
/** * Renders a Fluid Template Layout select field * * @param array $parameters * @param mixed $pObj * @return string */ public function addLayoutOptions(&$parameters, &$pObj) { $referringField = $parameters['config']['arguments']['referring_field']; $currentValue = $parameters['row'][$referringField]; $configuration = $this->configurationService->getViewConfigurationByFileReference($currentValue); $templatePaths = new TemplatePaths($configuration); $files = $templatePaths->resolveAvailableLayoutFiles(); $files = array_map('basename', $files); foreach ($files as $file) { if (0 !== strpos($file, '.')) { $file = pathinfo($file, PATHINFO_FILENAME); array_push($parameters['items'], [$file, $file]); } } }
/** * @return void */ public function testToArray() { $instance = new TemplatePaths(); $instance->setTemplateRootPaths(array('1')); $instance->setLayoutRootPaths(array('2')); $instance->setPartialRootPaths(array('3')); $result = $instance->toArray(); $expected = array(TemplatePaths::CONFIG_TEMPLATEROOTPATHS => array(1), TemplatePaths::CONFIG_LAYOUTROOTPATHS => array(2), TemplatePaths::CONFIG_PARTIALROOTPATHS => array(3)); $this->assertEquals($expected, $result); }
/** * @param array $row * @return string */ public function getTemplatePathAndFilename(array $row) { if (FALSE === empty($this->templatePathAndFilename)) { $templateReference = $this->templatePathAndFilename; if ('/' !== $templateReference[0]) { $templateReference = GeneralUtility::getFileAbsFileName($templateReference); } if (TRUE === file_exists($templateReference)) { return $templateReference; } return NULL; } $templateReference = $this->getControllerActionReferenceFromRecord($row); list(, $filename) = explode(':', $templateReference); list($controllerAction, $format) = explode('.', $filename); $controllerAction = $this->getControllerActionFromRecord($row); $paths = $this->getTemplatePaths($row); $templatePaths = new TemplatePaths($paths); return $templatePaths->resolveTemplateFileForControllerAndActionAndFormat('Content', $controllerAction, $format); }
/** * @param TemplatePaths $templatePaths * @return void */ public function setTemplatePaths(TemplatePaths $templatePaths) { $this->setTemplateRootPaths($templatePaths->getTemplateRootPaths()); $this->setLayoutRootPaths($templatePaths->getLayoutRootPaths()); $this->setPartialRootPaths($templatePaths->getPartialRootPaths()); }
/** * @param string $extension * @param string $template * @param array $parameters * @return string */ protected function renderOption($extension, $template, array $parameters) { $name = $parameters['itemFormElName']; $value = $parameters['itemFormElValue']; $onChange = 'onclick="if (confirm(TBE_EDITOR.labels.onChangeAlert) && TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm() };"'; $selector = ''; try { $extensionName = ExtensionNamingUtility::getExtensionKey($extension); $paths = $this->configurationService->getPageConfiguration($extensionName); $templatePaths = new TemplatePaths($paths); $templatePathAndFilename = $templatePaths->resolveTemplateFileForControllerAndActionAndFormat('Page', $template); if (FALSE === file_exists($templatePathAndFilename)) { $this->configurationService->message('Missing template file: ' . $templatePathAndFilename, GeneralUtility::SYSLOG_SEVERITY_WARNING); return ''; } $viewContext = new ViewContext($templatePathAndFilename, $extensionName); $viewContext->setTemplatePaths($templatePaths); $viewContext->setSectionName('Configuration'); $form = $this->configurationService->getFormFromTemplateFile($viewContext); if (FALSE === $form instanceof Form) { $this->configurationService->message('Template file ' . $templatePathAndFilename . ' contains an unparsable Form definition', GeneralUtility::SYSLOG_SEVERITY_FATAL); return ''; } if (FALSE === $form->getEnabled()) { $this->configurationService->message('Template file ' . $templatePathAndFilename . ' is disabled by configuration', GeneralUtility::SYSLOG_SEVERITY_NOTICE); return ''; } $thumbnail = MiscellaneousUtility::getIconForTemplate($form); $label = $form->getLabel(); $optionValue = $extension . '->' . $template; $selected = $optionValue == $value ? ' checked="checked"' : ''; $option = '<label style="padding: 0.5em; border: 1px solid #CCC; display: inline-block; vertical-align: bottom; margin: 0 1em 1em 0; cursor: pointer; ' . ($selected ? 'background-color: #DDD;' : '') . '">'; $option .= '<img src="' . $thumbnail . '" alt="' . $label . '" style="margin: 0.5em 0 0.5em 0; max-width: 196px; max-height: 128px;"/><br />'; $option .= '<input type="radio" value="' . $optionValue . '"' . $selected . ' name="' . $name . '" ' . $onChange . ' /> ' . $label; $option .= '</label>'; $selector .= $option . LF; } catch (\RuntimeException $error) { $this->configurationService->debug($error); } return $selector; }
/** * @param array $row * @return NULL|string */ public function getTemplatePathAndFilename(array $row) { $action = $this->getControllerActionFromRecord($row); $paths = $this->getTemplatePaths($row); $templatePaths = new TemplatePaths($paths); return $templatePaths->resolveTemplateFileForControllerAndActionAndFormat('Backend', ucfirst($action)); }
/** * Gets a list of usable Page Templates from defined page template TypoScript * * @param string $format * @return array * @api */ public function getAvailablePageTemplateFiles($format = 'html') { $typoScript = $this->configurationService->getPageConfiguration(); $output = array(); if (FALSE === is_array($typoScript)) { return $output; } foreach ($typoScript as $extensionName => $group) { if (TRUE === isset($group['enable']) && 1 > $group['enable']) { continue; } $output[$extensionName] = array(); $templatePaths = new TemplatePaths($group); $templateRootPaths = $templatePaths->getTemplateRootPaths(); foreach ($templateRootPaths as $templateRootPath) { $configuredPath = $templateRootPath . 'Page/'; if (FALSE === is_dir($configuredPath)) { $this->configurationService->message('The template group "' . $extensionName . '" has been configured to use the templateRootPath "' . $configuredPath . '" but this directory does not exist.', GeneralUtility::SYSLOG_SEVERITY_FATAL); continue; } $files = scandir($configuredPath); foreach ($files as $key => $file) { $pathinfo = pathinfo($file); $extension = $pathinfo['extension']; $filename = $pathinfo['filename']; if ('.' === substr($file, 0, 1)) { unset($files[$key]); } else { if (strtolower($extension) !== strtolower($format)) { unset($files[$key]); } else { $output[$extensionName][$filename] = $filename; } } } } } return $output; }
/** * @param array $row * @return string */ public function getTemplatePathAndFilename(array $row) { $templatePathAndFilename = $this->templatePathAndFilename; $action = $this->getControllerActionReferenceFromRecord($row); if (false === empty($action)) { $paths = $this->getTemplatePaths($row); $templatePaths = new TemplatePaths($paths); list(, $action) = explode('->', $action); $action = ucfirst($action); $templatePathAndFilename = $templatePaths->resolveTemplateFileForControllerAndActionAndFormat('Page', $action); } return $templatePathAndFilename; }
/** * @param string $extensionKey * @param string $contentType * @param string $variant * @param string $version * @return string */ public function resolveTemplateFileForVariant($extensionKey, $contentType, $variant = NULL, $version = NULL) { $paths = $this->getViewConfigurationForExtensionName(FALSE === empty($variant) ? $variant : $extensionKey); $templatePaths = new TemplatePaths($paths); $controllerName = 'CoreContent'; $controllerAction = FALSE === empty($version) ? $contentType . '/' . $version : $contentType; return $templatePaths->resolveTemplateFileForControllerAndActionAndFormat($controllerName, $controllerAction); }
/** * Gets a list of usable Page Templates from defined page template TypoScript. * Returns a list of Form instances indexed by the path ot the template file. * * @param string $format * @return Form[] * @api */ public function getAvailablePageTemplateFiles($format = 'html') { $typoScript = $this->configurationService->getPageConfiguration(); $output = []; foreach ((array) $typoScript as $extensionName => $group) { if (true === isset($group['enable']) && 1 > $group['enable']) { continue; } $output[$extensionName] = []; $templatePaths = new TemplatePaths($group); $templateRootPaths = $templatePaths->getTemplateRootPaths(); foreach ($templateRootPaths as $templateRootPath) { $configuredPath = $templateRootPath . 'Page/'; if (false === is_dir($configuredPath)) { $this->configurationService->message(sprintf('The template group "%s" has been configured to use the templateRootPath "' . '%s" but this directory does not exist.', $extensionName, $configuredPath), GeneralUtility::SYSLOG_SEVERITY_FATAL); continue; } $files = scandir($configuredPath); foreach ($files as $key => $file) { $pathinfo = pathinfo($file); $extension = $pathinfo['extension']; if ('.' === substr($file, 0, 1)) { continue; } elseif (strtolower($extension) !== strtolower($format)) { continue; } $filename = $pathinfo['filename']; if (isset($output[$extensionName][$filename])) { continue; } $viewContext = new ViewContext($configuredPath . $file, $extensionName, 'Page'); $viewContext->setSectionName('Configuration'); $viewContext->setTemplatePaths($templatePaths); $form = $this->configurationService->getFormFromTemplateFile($viewContext); $templatePathAndFilename = $form->getOption(Form::OPTION_TEMPLATEFILE); if (false === $form instanceof Form) { $this->configurationService->message('Template file ' . $viewContext . ' contains an unparsable Form definition', GeneralUtility::SYSLOG_SEVERITY_FATAL); continue; } if (false === $form->getEnabled()) { $this->configurationService->message('Template file ' . $templatePathAndFilename . ' is disabled by configuration', GeneralUtility::SYSLOG_SEVERITY_NOTICE); continue; } $form->setOption(Form::OPTION_TEMPLATEFILE, $configuredPath . $file); $output[$extensionName][$filename] = $form; } } } return $output; }