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 EditViewCompiler extends AbstractViewCompiler
 14 {
 15     use InputTypeResolverTrait;
 16 
 17     /**
 18      * Compiles the edit 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_edit_' . $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                 ->replacePrimaryKey($modelData)
 45                 ->replaceRoutePrefix($scaffolderConfig->routing->prefix);
 46 
 47             $this->stub = $themeExtension->runAfterEditViewIsCompiler($this->stub, $modelData, $scaffolderConfig);
 48 
 49             foreach ($extensions as $extension)
 50             {
 51                 $this->stub = $extension->runAfterEditViewIsCompiler($this->stub, $modelData, $scaffolderConfig);
 52             }
 53 
 54             return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
 55         }
 56     }
 57 
 58     /**
 59      * Store the compiled stub.
 60      *
 61      * @param $modelName
 62      * @param \stdClass $scaffolderConfig
 63      * @param $compiled
 64      * @param \Scaffolder\Support\FileToCompile $fileToCompile
 65      *
 66      * @return string
 67      */
 68     protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
 69     {
 70         $path = PathParser::parse($scaffolderConfig->paths->views) . strtolower($modelName) . '/edit.blade.php';
 71 
 72         // Store in cache
 73         if ($fileToCompile->cached)
 74         {
 75             File::copy(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 76         }
 77         else
 78         {
 79             File::put(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
 80             File::copy(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 81         }
 82 
 83         return $path;
 84     }
 85 
 86     /**
 87      * Add fields to the edit view.
 88      *
 89      * @param $modelData
 90      *
 91      * @return $this
 92      */
 93     private function addFields($modelData)
 94     {
 95         $fields = '';
 96         $firstIteration = true;
 97 
 98         foreach ($modelData->fields as $field)
 99         {
100             if ($firstIteration)
101             {
102                 $fields .= self::getInputFor($field) . PHP_EOL;
103                 $firstIteration = false;
104             }
105             else
106             {
107                 $fields .= $this->tab(2) . self::getInputFor($field) . PHP_EOL;
108             }
109         }
110 
111         $this->stub = str_replace('{{fields}}', $fields, $this->stub);
112 
113         return $this;
114     }
115 
116     /**
117      * Replace the prefix.
118      *
119      * @param $prefix
120      *
121      * @return $this
122      */
123     private function replaceRoutePrefix($prefix)
124     {
125         $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
126 
127         return $this;
128     }
129 
130     /**
131      * Replace the primary key.
132      *
133      * @param $modelData
134      *
135      * @return $this
136      */
137     private function replacePrimaryKey($modelData)
138     {
139         $primaryKey = 'id';
140 
141         foreach ($modelData->fields as $field)
142         {
143             if ($field->index == 'primary')
144             {
145                 $primaryKey = $field->name;
146                 break;
147             }
148         }
149 
150         $this->stub = str_replace('{{primaryKey}}', $primaryKey, $this->stub);
151 
152         return $this;
153     }
154 }
155 
Scaffolder v2.0.0 API documentation generated by ApiGen