Skip to content

Commit

Permalink
Move templates into own root level. (propelorm#1848)
Browse files Browse the repository at this point in the history
* Move templates into own root level.

* Clean up excludePaths
  • Loading branch information
dereuromark authored Mar 29, 2022
1 parent 767a72d commit d4c4392
Show file tree
Hide file tree
Showing 75 changed files with 114 additions and 31 deletions.
2 changes: 0 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

<file>src/</file>

<exclude-pattern>*/templates/*</exclude-pattern>

<rule ref="Spryker"/>

<rule ref="vendor/spryker/code-sniffer/SprykerStrict/ruleset.xml">
Expand Down
11 changes: 0 additions & 11 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ parameters:
reportUnmatchedIgnoredErrors: false
paths:
- '%rootDir%/../../../src/'
excludePaths:
- '%rootDir%/../../../src/Propel/Generator/Command/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Behavior/AggregateColumn/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Behavior/AggregateMultipleColumns/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Behavior/I18n/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Builder/Om/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Behavior/Validate/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Behavior/Archivable/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Behavior/Delegate/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Behavior/Sortable/templates/*'
- '%rootDir%/../../../src/Propel/Generator/Behavior/NestedSet/templates/*'
ignoreErrors:
- '#Call to an undefined method .+Collection::.+Array\(\)#'
-
Expand Down
3 changes: 0 additions & 3 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
>
<projectFiles>
<directory name="src/Propel/"/>
<ignoreFiles>
<directory name="src/Propel/Generator/Command/templates/" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
Expand Down
67 changes: 67 additions & 0 deletions src/Propel/Common/Util/PathTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* MIT License. This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Propel\Common\Util;

use RuntimeException;

trait PathTrait
{
/**
* Template paths are by convention in
* - templates/
* - besides src/ folder (which is required to detect root path)
*
* Note:
* - Propel/Generator/ prefix is removed from the path
*
* Examples:
* - Behavior/BehaviorName/
* - Builder/Om/
* - Command/
* - Manager/
*
* @param string $path
*
* @throws \RuntimeException
*
* @return string
*/
protected function getTemplatePath(string $path): string
{
$srcPos = strpos($path, DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR);
if ($srcPos === false) {
throw new RuntimeException('Cannot find root of repository. Please manually set a template path to file.');
}

$root = substr($path, 0, $srcPos) . DIRECTORY_SEPARATOR;

$pathElements = explode(DIRECTORY_SEPARATOR, $path);
$pathElements = array_reverse($pathElements);

$elements = [];
foreach ($pathElements as $element) {
if ($element === 'src') {
break;
}

$elements[] = $element;
}

$elements = array_reverse($elements);
// Propel/Generator/ prefixes are just noise and filtered out
if ($elements[0] === 'Propel') {
array_shift($elements);
}
if ($elements[0] === 'Generator') {
array_shift($elements);
}

return $root . 'templates' . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $elements) . DIRECTORY_SEPARATOR;
}
}
15 changes: 11 additions & 4 deletions src/Propel/Generator/Builder/Om/AbstractOMBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Propel\Generator\Builder\Om;

use Propel\Common\Util\PathTrait;
use Propel\Generator\Builder\DataModelBuilder;
use Propel\Generator\Builder\Util\PropelTemplate;
use Propel\Generator\Exception\InvalidArgumentException;
Expand All @@ -29,6 +30,8 @@
*/
abstract class AbstractOMBuilder extends DataModelBuilder
{
use PathTrait;

/**
* Declared fully qualified classnames, to build the 'namespace' statements
* according to this table's namespace.
Expand Down Expand Up @@ -1054,20 +1057,24 @@ public function getBehaviorContentBase(string $contentName, string $modifier): ?
*
* @param string $filename
* @param array $vars
* @param string $templateDir
* @param string|null $templatePath
*
* @throws \Propel\Generator\Exception\InvalidArgumentException
*
* @return string
*/
public function renderTemplate(string $filename, array $vars = [], string $templateDir = '/templates/'): string
public function renderTemplate(string $filename, array $vars = [], ?string $templatePath = null): string
{
$filePath = __DIR__ . $templateDir . $filename;
if ($templatePath === null) {
$templatePath = $this->getTemplatePath(__DIR__);
}

$filePath = $templatePath . $filename;
if (!file_exists($filePath)) {
// try with '.php' at the end
$filePath = $filePath . '.php';
if (!file_exists($filePath)) {
throw new InvalidArgumentException(sprintf('Template "%s" not found in "%s" directory', $filename, __DIR__ . $templateDir));
throw new InvalidArgumentException(sprintf('Template `%s` not found in `%s` directory', $filename, $templatePath));
}
}
$template = new PropelTemplate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Propel\Generator\Builder\Om;

use Propel\Common\Util\PathTrait;
use Propel\Generator\Builder\Util\PropelTemplate;
use Propel\Generator\Config\GeneratorConfigInterface;
use Propel\Generator\Model\Table;
Expand All @@ -18,6 +19,8 @@
*/
class TableMapLoaderScriptBuilder
{
use PathTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -95,7 +98,9 @@ protected function getFullyQualifiedTableMapClassName(Table $table): string
*/
protected function renderTemplate(array $vars): string
{
$filePath = implode(DIRECTORY_SEPARATOR, [__DIR__, 'templates', 'tableMapLoaderScript.php']);
$templatePath = $this->getTemplatePath(__DIR__);

$filePath = $templatePath . 'tableMapLoaderScript.php';
$template = new PropelTemplate();
$template->setTemplateFile($filePath);

Expand Down
14 changes: 11 additions & 3 deletions src/Propel/Generator/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Propel\Runtime\Adapter\AdapterFactory;
use Propel\Runtime\Connection\ConnectionFactory;
use Propel\Runtime\Connection\Exception\ConnectionException;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -269,18 +270,25 @@ private function initDsn(ConsoleHelper $consoleHelper, string $rdbms)
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param array<string, mixed> $options
*
* @throws \RuntimeException
*
* @return void
*/
private function generateProject(OutputInterface $output, array $options): void
{
$templatesPath = dirname(__FILE__, 5) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
if (!is_dir($templatesPath)) {
throw new RuntimeException(sprintf('Cannot find templates path `%s`', $templatesPath));
}

$schema = new PropelTemplate();
$schema->setTemplateFile(__DIR__ . '/templates/schema.xml.php');
$schema->setTemplateFile($templatesPath . 'Command/schema.xml.php');

$config = new PropelTemplate();
$config->setTemplateFile(__DIR__ . '/templates/propel.' . $options['format'] . '.php');
$config->setTemplateFile($templatesPath . 'Command/propel.' . $options['format'] . '.php');

$distConfig = new PropelTemplate();
$distConfig->setTemplateFile(__DIR__ . '/templates/propel.' . $options['format'] . '.dist.php');
$distConfig->setTemplateFile($templatesPath . 'Command/propel.' . $options['format'] . '.dist.php');

if (!isset($options['schema'])) {
$options['schema'] = $schema->render($options);
Expand Down
9 changes: 7 additions & 2 deletions src/Propel/Generator/Manager/MigrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Exception;
use PDO;
use PDOException;
use Propel\Common\Util\PathTrait;
use Propel\Generator\Builder\Util\PropelTemplate;
use Propel\Generator\Exception\InvalidArgumentException;
use Propel\Generator\Model\Column;
Expand All @@ -29,6 +30,8 @@
*/
class MigrationManager extends AbstractManager
{
use PathTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -422,8 +425,10 @@ public function getMigrationClassBody(array $migrationsUp, array $migrationsDown
'connectionToVariableName' => $connectionToVariableName,
];

$templatePath = $this->getTemplatePath(__DIR__);

$template = new PropelTemplate();
$filePath = implode(DIRECTORY_SEPARATOR, [__DIR__, 'templates', 'migration_template.php']);
$filePath = $templatePath . 'migration_template.php';
$template->setTemplateFile($filePath);

return $template->render($vars);
Expand All @@ -435,7 +440,7 @@ public function getMigrationClassBody(array $migrationsUp, array $migrationsDown
* @param array $migrationsUp
* @param array $migrationsDown
*
* @return array
* @return array<string, string>
*/
protected static function buildConnectionToVariableNameMap(array $migrationsUp, array $migrationsDown): array
{
Expand Down
17 changes: 12 additions & 5 deletions src/Propel/Generator/Model/Behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Propel\Generator\Model;

use InvalidArgumentException;
use Propel\Common\Util\PathTrait;
use Propel\Generator\Builder\Util\PropelTemplate;
use Propel\Generator\Exception\LogicException;
use ReflectionObject;
Expand All @@ -21,6 +22,8 @@
*/
class Behavior extends MappingModel
{
use PathTrait;

/**
* The table object on which the behavior is applied.
*
Expand Down Expand Up @@ -345,23 +348,27 @@ public function isTableModified(): bool
*
* @param string $filename
* @param array $vars
* @param string $templateDir
* @param string|null $templatePath
*
* @throws \InvalidArgumentException
*
* @return string
*/
public function renderTemplate(string $filename, array $vars = [], string $templateDir = '/templates/'): string
public function renderTemplate(string $filename, array $vars = [], ?string $templatePath = null): string
{
$filePath = $this->getDirname() . $templateDir . $filename;
if ($templatePath === null) {
$templatePath = $this->getTemplatePath($this->getDirname());
}

$filePath = $templatePath . $filename;
if (!file_exists($filePath)) {
// try with '.php' at the end
$filePath = $filePath . '.php';
if (!file_exists($filePath)) {
throw new InvalidArgumentException(sprintf(
'Template "%s" not found in "%s" directory',
'Template `%s` not found in `%s` directory',
$filename,
$this->getDirname() . $templateDir,
$templatePath,
));
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d4c4392

Please sign in to comment.