diff --git a/Generator/Generator.php b/Generator/Generator.php index 9e8e5d1..3939425 100644 --- a/Generator/Generator.php +++ b/Generator/Generator.php @@ -2,9 +2,25 @@ namespace Webbamboo\MaterialDashboard\Generator; use Symfony\Bundle\MakerBundle\Generator as OriginalGenerator; +use Symfony\Bundle\MakerBundle\FileManager; +use Symfony\Bundle\MakerBundle\GeneratorTwigHelper; +use Symfony\Bundle\MakerBundle\Str; class Generator extends OriginalGenerator { + private $fileManager; + private $twigHelper; + private $pendingOperations = []; + private $namespacePrefix; + + public function __construct(FileManager $fileManager, string $namespacePrefix){ + parent::__construct($fileManager, $namespacePrefix); + + $this->fileManager = $fileManager; + $this->twigHelper = new GeneratorTwigHelper($fileManager); + $this->namespacePrefix = trim($namespacePrefix, '\\'); + } + private function addOperation(string $targetPath, string $templateName, array $variables) { if ($this->fileManager->fileExists($targetPath)) { @@ -19,7 +35,6 @@ private function addOperation(string $targetPath, string $templateName, array $v $templatePath = $templateName; if (!file_exists($templatePath)) { $templatePath = __DIR__.'/../Resources/skeleton/'.$templateName; - if (!file_exists($templatePath)) { throw new \Exception(sprintf('Cannot find template "%s"', $templateName)); } @@ -30,4 +45,84 @@ private function addOperation(string $targetPath, string $templateName, array $v 'variables' => $variables, ]; } + + /** + * Generate a normal file from a template. + * + * @param string $targetPath + * @param string $templateName + * @param array $variables + */ + public function generateFile(string $targetPath, string $templateName, array $variables) + { + $variables = array_merge($variables, [ + 'helper' => $this->twigHelper, + ]); + + $this->addOperation($targetPath, $templateName, $variables); + } + + /** + * Actually writes and file changes that are pending. + */ + public function writeChanges() + { + foreach ($this->pendingOperations as $targetPath => $templateData) { + if (isset($templateData['contents'])) { + $this->fileManager->dumpFile($targetPath, $templateData['contents']); + + continue; + } + + $this->fileManager->dumpFile( + $targetPath, + $this->getFileContentsForPendingOperation($targetPath, $templateData) + ); + } + } + + public function getFileContentsForPendingOperation(string $targetPath): string + { + if (!isset($this->pendingOperations[$targetPath])) { + throw new RuntimeCommandException(sprintf('File "%s" is not in the Generator\'s pending operations', $targetPath)); + } + + $templatePath = $this->pendingOperations[$targetPath]['template']; + $parameters = $this->pendingOperations[$targetPath]['variables']; + + $templateParameters = array_merge($parameters, [ + 'relative_path' => $this->fileManager->relativizePath($targetPath), + ]); + + return $this->fileManager->parseTemplate($templatePath, $templateParameters); + } + + /** + * Generate a new file for a class from a template. + * + * @param string $className The fully-qualified class name + * @param string $templateName Template name in Resources/skeleton to use + * @param array $variables Array of variables to pass to the template + * + * @return string The path where the file will be created + * + * @throws \Exception + */ + public function generateClass(string $className, string $templateName, array $variables = []): string + { + $targetPath = $this->fileManager->getRelativePathForFutureClass($className); + + if (null === $targetPath) { + throw new \LogicException(sprintf('Could not determine where to locate the new class "%s", maybe try with a full namespace like "\\My\\Full\\Namespace\\%s"', $className, Str::getShortClassName($className))); + } + + $variables = array_merge($variables, [ + 'class_name' => Str::getShortClassName($className), + 'namespace' => Str::getNamespace($className), + ]); + + $this->addOperation($targetPath, $templateName, $variables); + + return $targetPath; + } } \ No newline at end of file diff --git a/Resources/skeleton/crud/templates/_delete_form.tpl.php b/Resources/skeleton/crud/templates/_delete_form.tpl.php index e322ec7..1016884 100644 --- a/Resources/skeleton/crud/templates/_delete_form.tpl.php +++ b/Resources/skeleton/crud/templates/_delete_form.tpl.php @@ -1,5 +1,4 @@ -
+ - -
+ \ No newline at end of file diff --git a/Resources/skeleton/crud/templates/_form.tpl.php b/Resources/skeleton/crud/templates/_form.tpl.php index bf20b98..cacd3de 100644 --- a/Resources/skeleton/crud/templates/_form.tpl.php +++ b/Resources/skeleton/crud/templates/_form.tpl.php @@ -1,4 +1,4 @@ {{ form_start(form) }} - {{ form_widget(form) }} + {{ form_widget(form, { 'attr': {'class': 'form-horizontal'} }) }} {{ form_end(form) }} diff --git a/Resources/skeleton/crud/templates/edit.tpl.php b/Resources/skeleton/crud/templates/edit.tpl.php index f0d42f5..f1f7d59 100644 --- a/Resources/skeleton/crud/templates/edit.tpl.php +++ b/Resources/skeleton/crud/templates/edit.tpl.php @@ -1,11 +1,52 @@ -getHeadPrintCode('Edit '.$entity_class_name) ?> +{% extends '@MaterialDashboard/base.html.twig' %} -{% block body %} -

Edit

- - {{ include('/_form.html.twig', {'button_label': 'Update'}) }} - - back to list +{% block title %}Edit {% endblock %} +{% block body %} +
+
+
+
+
+ +
+ {{ include('/_form.html.twig', {'button_label': 'Update'}) }} +
+
+
+
+
+
{{ include('/_delete_form.html.twig') }} {% endblock %} +{% block javascripts %} + {{ parent() }} + +{% endblock %} \ No newline at end of file diff --git a/Resources/skeleton/crud/templates/index.tpl.php b/Resources/skeleton/crud/templates/index.tpl.php index d7c1d47..68863a7 100644 --- a/Resources/skeleton/crud/templates/index.tpl.php +++ b/Resources/skeleton/crud/templates/index.tpl.php @@ -1,35 +1,47 @@ -getHeadPrintCode($entity_class_name.' index'); ?> +{% extends "@MaterialDashboard/base.html.twig" %} -{% block body %} -

index

+{% block meta_title %} index{% endblock %} - - - +{% block body %} +
+
+
+
+
+
+

index Create new

+
+
+
+ + - + - - - - - {% for in %} - + + + + + {% for in %} + - + - - - {% else %} - - - - {% endfor %} - -
actions
Actions
{{ getEntityFieldPrintCode($entity_twig_var_singular, $field) ?> }}{{ getEntityFieldPrintCode($entity_twig_var_singular, $field) ?> }} - show - edit -
no records found
- - Create new + + Show + Edit + + + {% else %} + + no records found + + {% endfor %} + + + + + + + {% endblock %} diff --git a/Resources/skeleton/crud/templates/new.tpl.php b/Resources/skeleton/crud/templates/new.tpl.php index e2818d2..8d31be4 100644 --- a/Resources/skeleton/crud/templates/new.tpl.php +++ b/Resources/skeleton/crud/templates/new.tpl.php @@ -1,9 +1,35 @@ -getHeadPrintCode('New '.$entity_class_name) ?> +{% extends '@MaterialDashboard/base.html.twig' %} -{% block body %} -

Create new

- - {{ include('/_form.html.twig') }} +{% block title %}New {% endblock %} - back to list -{% endblock %} +{% block body %} +
+
+
+
+
+
+ +
+
+ {{ include('/_form.html.twig') }} +
+
+
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/Resources/skeleton/crud/templates/show.tpl.php b/Resources/skeleton/crud/templates/show.tpl.php index d016c60..51c639b 100644 --- a/Resources/skeleton/crud/templates/show.tpl.php +++ b/Resources/skeleton/crud/templates/show.tpl.php @@ -1,22 +1,66 @@ -getHeadPrintCode($entity_class_name) ?> +{% extends "@MaterialDashboard/base.html.twig" %} -{% block body %} -

+{% block meta_title %}Notification{% endblock %} - - +{% block body %} +
+
+
+ - - - - + + + + - -
{{ getEntityFieldPrintCode($entity_twig_var_singular, $field) ?> }}
{{ getEntityFieldPrintCode($entity_twig_var_singular, $field) ?> }}
- - back to list - - edit - + + + + + + + {{ include('/_delete_form.html.twig') }} {% endblock %} +{% block javascripts %} + {{ parent() }} + +{% endblock %} \ No newline at end of file