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\PathParser;
 10 use stdClass;
 11 
 12 class IndexViewCompiler extends AbstractViewCompiler
 13 {
 14     /**
 15      * Compiles the index view.
 16      *
 17      * @param $stub
 18      * @param $modelName
 19      * @param $modelData
 20      * @param \stdClass $scaffolderConfig
 21      * @param $hash
 22      * @param \Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface $themeExtension
 23      * @param \Scaffolder\Support\Contracts\ScaffolderExtensionInterface[] $extensions
 24      * @param null $extra
 25      *
 26      * @return string
 27      */
 28     public function compile($stub, $modelName, $modelData, stdClass $scaffolderConfig, $hash, ScaffolderThemeExtensionInterface $themeExtension, array $extensions, $extra = null)
 29     {
 30         if (File::exists(base_path('scaffolder-config/cache/view_index_' . $hash . self::CACHE_EXT)))
 31         {
 32             return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
 33         }
 34         else
 35         {
 36             $this->stub = $stub;
 37 
 38             $this->replaceClassName($modelName)
 39                 ->replaceBreadcrumb($modelName, $modelData->modelLabel)
 40                 ->addDatatableFields($modelName, $modelData)
 41                 ->setTableHeaders($modelData)
 42                 ->replaceRoutePrefix($scaffolderConfig->routing->prefix);
 43 
 44             $this->stub = $themeExtension->runAfterIndexViewIsCompiled($this->stub, $modelData, $scaffolderConfig);
 45 
 46             foreach ($extensions as $extension)
 47             {
 48                 $this->stub = $extension->runAfterIndexViewIsCompiled($this->stub, $modelData, $scaffolderConfig);
 49             }
 50 
 51             return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
 52         }
 53     }
 54 
 55     /**
 56      * Store the compiled stub.
 57      *
 58      * @param $modelName
 59      * @param \stdClass $scaffolderConfig
 60      * @param $compiled
 61      * @param \Scaffolder\Support\FileToCompile $fileToCompile
 62      *
 63      * @return string
 64      */
 65     protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
 66     {
 67         $path = PathParser::parse($scaffolderConfig->paths->views) . strtolower($modelName) . '/index.blade.php';
 68 
 69         // Store in cache
 70         if ($fileToCompile->cached)
 71         {
 72             File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 73         }
 74         else
 75         {
 76             File::put(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
 77             File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 78         }
 79 
 80         return $path;
 81     }
 82 
 83     /**
 84      * Datatable fields.
 85      *
 86      * @param $modelName
 87      * @param $modelData
 88      *
 89      * @return $this
 90      */
 91     private function addDatatableFields($modelName, $modelData)
 92     {
 93         $fields = '';
 94         $firstIteration = true;
 95 
 96         foreach ($modelData->fields as $field)
 97         {
 98             if ($field->hideInListings === false)
 99             {
100                 if ($firstIteration)
101                 {
102                     $fields .= sprintf("{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
103                     $firstIteration = false;
104                 }
105                 else
106                 {
107                     $fields .= sprintf($this->tab(4) . "{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
108                 }
109             }
110         }
111 
112         $this->stub = str_replace('{{datatable_fields}}', $fields, $this->stub);
113         $this->stub = str_replace('{{datatable_url}}', ucfirst($modelName), $this->stub);
114 
115         return $this;
116     }
117 
118     /**
119      * Set index table headers.
120      *
121      * @param $modelData
122      *
123      * @return $this
124      */
125     private function setTableHeaders($modelData)
126     {
127         $fields = '';
128         $firstIteration = true;
129 
130         foreach ($modelData->fields as $field)
131         {
132             if ($field->hideInListings === false)
133             {
134                 if ($firstIteration)
135                 {
136                     $fields .= sprintf("<th>%s</th>" . PHP_EOL, ucfirst($field->name));
137                     $firstIteration = false;
138 
139                 }
140                 else
141                 {
142                     $fields .= sprintf($this->tab(3) . "<th>%s</th>" . PHP_EOL, ucfirst($field->name));
143                 }
144             }
145         }
146 
147         $this->stub = str_replace('{{table_headers}}', $fields, $this->stub);
148 
149         return $this;
150     }
151 
152     /**
153      * Replace the prefix.
154      *
155      * @param $prefix
156      *
157      * @return $this
158      */
159     private function replaceRoutePrefix($prefix)
160     {
161         $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
162 
163         return $this;
164     }
165 }
166 
Scaffolder v2.0.0 API documentation generated by ApiGen