Skip to content

Commit

Permalink
Enable configuring template root paths
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodh committed Aug 13, 2017
1 parent 1e22f97 commit 62b6c59
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,9 @@ public function getSettings($typoscript = null)
$typoscript = $this->getConfiguration(parent::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
}
$settings = $typoscript['module.']['extension_builder.']['settings.'];
if (empty($settings['codeTemplateRootPath'])) {
$settings['codeTemplateRootPath'] = self::DEFAULT_TEMPLATE_ROOTPATH;
}
$settings['codeTemplateRootPath'] = self::substituteExtensionPath($settings['codeTemplateRootPath']);
$settings['extConf'] = $this->getExtensionBuilderSettings();
if (empty($settings['publicResourcesPath'])) {
$settings['publicResourcesPath'] = ExtensionManagementUtility::extRelPath('extension_builder') . 'Resources/Public/';
$settings['publicResourcesPath'] = ExtensionManagementUtility::siteRelPath('extension_builder') . 'Resources/Public/';
}
return $settings;
}
Expand Down
51 changes: 37 additions & 14 deletions Classes/Service/FileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ class FileGenerator
*/
protected $roundTripService = null;
/**
* @var string
* @var array
*/
protected $codeTemplateRootPath = '';
protected $codeTemplateRootPaths = [];
/**
* @var array
*/
protected $codeTemplatePartialPaths = [];
/**
* @var \EBT\ExtensionBuilder\Domain\Model\Extension
*/
Expand Down Expand Up @@ -137,11 +141,16 @@ public function build(\EBT\ExtensionBuilder\Domain\Model\Extension $extension)
$this->roundTripEnabled = true;
$this->roundTripService->initialize($extension);
}
if (isset($this->settings['codeTemplateRootPath'])) {
$this->codeTemplateRootPath = $this->settings['codeTemplateRootPath'];
if (isset($this->settings['codeTemplateRootPaths.'])) {
$this->codeTemplateRootPaths = $this->settings['codeTemplateRootPaths.'];
} else {
throw new \Exception('No codeTemplateRootPath configured');
}
if (isset($this->settings['codeTemplatePartialPaths.'])) {
$this->codeTemplatePartialPaths = $this->settings['codeTemplatePartialPaths.'];
} else {
throw new \Exception('No codeTemplatePartialPaths configured');
}
// Base directory already exists at this point
$this->extensionDirectory = $this->extension->getExtensionDir();
if (!is_dir($this->extensionDirectory)) {
Expand Down Expand Up @@ -366,7 +375,7 @@ protected function generateTemplateFiles($templateSubFolder = '')
* @var \EBT\ExtensionBuilder\Domain\Model\DomainObject\Action $action
*/
if ($action->getNeedsTemplate()
&& file_exists($this->codeTemplateRootPath . $templateRootFolder . 'Templates/' . $action->getName() . '.htmlt')
&& file_exists($this->getTemplatePath($templateRootFolder . 'Templates/' . $action->getName() . '.htmlt'))

) {
$hasTemplates = true;
Expand Down Expand Up @@ -420,6 +429,20 @@ protected function generateTemplateFiles($templateSubFolder = '')
}
}

/**
* get template file according to configured template root paths
* @param $fileName
* @return string
*/
protected function getTemplatePath($fileName) {
foreach(array_reverse($this->codeTemplateRootPaths) as $rootPath) {
$path = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($rootPath);
if (file_exists($path . $fileName)) {
return $path . $fileName;
}
}
}

protected function generateTyposcriptFiles()
{
if ($this->extension->hasPlugins() || $this->extension->hasBackendModules()) {
Expand Down Expand Up @@ -631,15 +654,15 @@ protected function copyStaticFiles()
if ($this->extension->hasBackendModules()) {
foreach ($this->extension->getBackendModules() as $backendModule) {
$this->upload_copy_move(
$this->codeTemplateRootPath . 'Resources/Public/Icons/user_extension.svg',
$this->getTemplatePath('Resources/Public/Icons/user_extension.svg'),
$this->iconsDirectory . 'user_mod_' . $backendModule->getKey() . '.svg'
);
}
}
if ($this->extension->hasPlugins()) {
foreach ($this->extension->getPlugins() as $plugin) {
$this->upload_copy_move(
$this->codeTemplateRootPath . 'Resources/Public/Icons/user_extension.svg',
$this->getTemplatePath('Resources/Public/Icons/user_extension.svg'),
$this->iconsDirectory . 'user_plugin_' . $plugin->getKey() . '.svg'
);
}
Expand Down Expand Up @@ -668,7 +691,7 @@ protected function generateDocumentationFiles()
if (is_dir($docFile)) {
$this->mkdir_deep(
$this->extensionDirectory,
'Documentation.tmpl/' . str_replace($this->codeTemplateRootPath . 'Documentation.tmpl/', '', $docFile)
'Documentation.tmpl/' . str_replace($this->getTemplatePath('Documentation.tmpl/'), '', $docFile)
);
} elseif (strpos($docFile, '.rstt') === false && strpos($docFile, '.ymlt') === false) {
$this->upload_copy_move(
Expand Down Expand Up @@ -698,10 +721,10 @@ public function renderTemplate($filePath, $variables)
$variables['settings'] = $this->settings;
/* @var \TYPO3\CMS\Fluid\View\StandaloneView $standAloneView */
$standAloneView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
$standAloneView->setLayoutRootPaths(array($this->codeTemplateRootPath));
$standAloneView->setPartialRootPaths(array($this->codeTemplateRootPath . 'Partials'));
$standAloneView->setLayoutRootPaths($this->codeTemplateRootPaths);
$standAloneView->setPartialRootPaths($this->codeTemplatePartialPaths);
$standAloneView->setFormat('txt');
$templatePathAndFilename = $this->codeTemplateRootPath . $filePath;
$templatePathAndFilename = $this->getTemplatePath($filePath);
$standAloneView->setTemplatePathAndFilename($templatePathAndFilename);
$standAloneView->assignMultiple($variables);
$renderedContent = $standAloneView->render();
Expand All @@ -719,7 +742,7 @@ public function renderTemplate($filePath, $variables)
public function generateActionControllerCode(
\EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject)
{
$controllerTemplateFilePath = $this->codeTemplateRootPath . 'Classes/Controller/Controller.phpt';
$controllerTemplateFilePath = $this->getTemplatePath('Classes/Controller/Controller.phpt');
$existingClassFileObject = null;
if ($this->roundTripEnabled) {
$existingClassFileObject = $this->roundTripService->getControllerClassFile($domainObject);
Expand Down Expand Up @@ -747,7 +770,7 @@ public function generateActionControllerCode(
*/
public function generateDomainObjectCode(\EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject)
{
$modelTemplateClassPath = $this->codeTemplateRootPath . 'Classes/Domain/Model/Model.phpt';
$modelTemplateClassPath = $this->getTemplatePath('Classes/Domain/Model/Model.phpt');
$existingClassFileObject = null;
if ($this->roundTripEnabled) {
$existingClassFileObject = $this->roundTripService->getDomainModelClassFile($domainObject);
Expand Down Expand Up @@ -787,7 +810,7 @@ protected function renderClassFile($classObject)
*/
public function generateDomainRepositoryCode(\EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject)
{
$repositoryTemplateClassPath = $this->codeTemplateRootPath . 'Classes/Domain/Repository/Repository.phpt';
$repositoryTemplateClassPath = $this->getTemplatePath('Classes/Domain/Repository/Repository.phpt');
$existingClassFileObject = null;
if ($this->roundTripEnabled) {
$existingClassFileObject = $this->roundTripService->getRepositoryClassFile($domainObject);
Expand Down
1 change: 0 additions & 1 deletion Resources/Public/jsDomainModeling/extensionProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ extbaseModeling_wiringEditorLanguage.propertiesFields =
typeInvite: TYPO3.settings.extensionBuilder._LOCAL_LANG.extensionKey,
forceLowerCase: true,
forceAlphaNumericUnderscore: true,
regexp: /^[a-z]/,
minLength: 3,
maxLength: 30,
cols: 30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
result = result && !forceAlphaNumericRegex.test(val);
}
if (this.options.forceAlphaNumericUnderscore) {
var forceAlphaNumericUnderscoreRegex = new RegExp(/[a-zA-Z0-9_]/g);
var forceAlphaNumericUnderscoreRegex = new RegExp(/[^a-zA-Z0-9_]/g);
result = result && !forceAlphaNumericUnderscoreRegex.test(val);
}
if (this.options.firstCharNonNumeric) {
Expand Down
6 changes: 4 additions & 2 deletions Tests/BaseFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,15 @@ protected function setUp()

$this->fileGenerator->setSettings(
array(
'codeTemplateRootPath' => $this->codeTemplateRootPath,
'codeTemplateRootPaths' => [$this->codeTemplateRootPath],
'codeTemplatePartialPaths' => [$this->codeTemplateRootPath . 'Partials'],
'extConf' => array(
'enableRoundtrip' => '1'
)
)
);
$this->fileGenerator->_set('codeTemplateRootPath', __DIR__ . '/../Resources/Private/CodeTemplates/Extbase/');
$this->fileGenerator->_set('codeTemplateRootPaths', [__DIR__ . '/../Resources/Private/CodeTemplates/Extbase/']);
$this->fileGenerator->_set('codeTemplatePartialPaths', [__DIR__ . '/../Resources/Private/CodeTemplates/Extbase/']);
$this->fileGenerator->_set('enableRoundtrip', true);
$this->fileGenerator->_set('extension', $this->extension);
}
Expand Down
5 changes: 3 additions & 2 deletions ext_typoscript_setup.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# global configuration
module.extension_builder {
settings{
codeTemplateRootPath = EXT:extension_builder/Resources/Private/CodeTemplates/Extbase/
codeTemplateRootPaths.0 = EXT:extension_builder/Resources/Private/CodeTemplates/Extbase/
codeTemplatePartialPaths.0 = EXT:extension_builder/Resources/Private/CodeTemplates/Extbase/Partials
}
}
}

0 comments on commit 62b6c59

Please sign in to comment.