Scaffolder v2.0.0
  • Namespace
  • Class

Namespaces

  • Scaffolder
    • Commands
    • Compilers
      • Core
        • Api
      • Layout
      • View

Classes

  • Scaffolder\Commands\BaseCommand
  • Scaffolder\Commands\ClearCacheCommand
  • Scaffolder\Commands\GeneratorCommand
  • Scaffolder\Commands\InitializeApiCommand
  • Scaffolder\Compilers\AbstractCompiler
  • Scaffolder\Compilers\AbstractCoreCompiler
  • Scaffolder\Compilers\AbstractViewCompiler
  • Scaffolder\Compilers\Core\Api\ApiControllerCompiler
  • Scaffolder\Compilers\Core\Api\ApiModelCompiler
  • Scaffolder\Compilers\Core\Api\ApiRouteCompiler
  • Scaffolder\Compilers\Core\ControllerCompiler
  • Scaffolder\Compilers\Core\MigrationCompiler
  • Scaffolder\Compilers\Core\ModelCompiler
  • Scaffolder\Compilers\Core\RouteCompiler
  • Scaffolder\Compilers\Layout\CreateLayoutCompiler
  • Scaffolder\Compilers\Layout\EditLayoutCompiler
  • Scaffolder\Compilers\Layout\PageLayoutCompiler
  • Scaffolder\Compilers\View\CreateViewCompiler
  • Scaffolder\Compilers\View\DashboardViewCompiler
  • Scaffolder\Compilers\View\EditViewCompiler
  • Scaffolder\Compilers\View\IndexViewCompiler
  • Scaffolder\Compilers\View\LoginViewCompiler
  • Scaffolder\Compilers\View\WelcomeViewCompiler
  • Scaffolder\ScaffolderServiceProvider
  1 <?php
  2 
  3 namespace Scaffolder\Compilers\View;
  4 
  5 use Illuminate\Support\Facades\File;
  6 use Scaffolder\Compilers\AbstractViewCompiler;
  7 use Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface;
  8 use Scaffolder\Support\FileToCompile;
  9 use Scaffolder\Support\InputTypeResolverTrait;
 10 use Scaffolder\Support\PathParser;
 11 use stdClass;
 12 
 13 class CreateViewCompiler extends AbstractViewCompiler
 14 {
 15     use InputTypeResolverTrait;
 16 
 17     /**
 18      * Compiles the create view.
 19      *
 20      * @param $stub
 21      * @param $modelName
 22      * @param $modelData
 23      * @param \stdClass $scaffolderConfig
 24      * @param $hash
 25      * @param \Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface $themeExtension
 26      * @param \Scaffolder\Support\Contracts\ScaffolderExtensionInterface[] $extensions
 27      * @param null $extra
 28      *
 29      * @return string
 30      */
 31     public function compile($stub, $modelName, $modelData, stdClass $scaffolderConfig, $hash, ScaffolderThemeExtensionInterface $themeExtension, array $extensions, $extra = null)
 32     {
 33         if (File::exists(base_path('scaffolder-config/cache/view_create_' . $hash . self::CACHE_EXT)))
 34         {
 35             return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
 36         }
 37         else
 38         {
 39             $this->stub = $stub;
 40 
 41             $this->replaceClassName($modelName)
 42                 ->replaceBreadcrumb($modelName, $modelData->modelLabel)
 43                 ->addFields($modelData)
 44                 ->replaceRoutePrefix($scaffolderConfig->routing->prefix);
 45 
 46             $this->stub = $themeExtension->runAfterCreateViewIsCompiled($this->stub, $modelData, $scaffolderConfig);
 47 
 48             foreach ($extensions as $extension)
 49             {
 50                 $this->stub = $extension->runAfterCreateViewIsCompiled($this->stub, $modelData, $scaffolderConfig);
 51             }
 52 
 53             return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
 54         }
 55     }
 56 
 57     /**
 58      * Store the compiled stub.
 59      *
 60      * @param $modelName
 61      * @param \stdClass $scaffolderConfig
 62      * @param $compiled
 63      * @param \Scaffolder\Support\FileToCompile $fileToCompile
 64      *
 65      * @return string
 66      */
 67     protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
 68     {
 69         $path = PathParser::parse($scaffolderConfig->paths->views) . strtolower($modelName) . '/create.blade.php';
 70 
 71         // Store in cache
 72         if ($fileToCompile->cached)
 73         {
 74             File::copy(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 75         }
 76         else
 77         {
 78             File::put(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
 79             File::copy(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 80         }
 81 
 82         return $path;
 83     }
 84 
 85     /**
 86      * Add fields to the create view.
 87      *
 88      * @param $modelData
 89      *
 90      * @return $this
 91      */
 92     private function addFields($modelData)
 93     {
 94         $fields = '';
 95         $firstIteration = true;
 96 
 97         foreach ($modelData->fields as $field)
 98         {
 99             if ($firstIteration)
100             {
101                 $fields .= self::getInputFor($field) . PHP_EOL;
102                 $firstIteration = false;
103             }
104             else
105             {
106                 $fields .= $this->tab(1) . self::getInputFor($field) . PHP_EOL;
107             }
108         }
109 
110         $this->stub = str_replace('{{fields}}', $fields, $this->stub);
111 
112         return $this;
113     }
114 
115     /**
116      * Replace the prefix.
117      *
118      * @param string $prefix
119      *
120      * @return $this
121      */
122     private function replaceRoutePrefix($prefix)
123     {
124         $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
125 
126         return $this;
127     }
128 }
129 
Scaffolder v2.0.0 API documentation generated by ApiGen