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\Core;
  4 
  5 use Illuminate\Support\Facades\File;
  6 use Scaffolder\Compilers\AbstractCoreCompiler;
  7 use Scaffolder\Support\FileToCompile;
  8 use Scaffolder\Support\PathParser;
  9 use stdClass;
 10 
 11 class ControllerCompiler extends AbstractCoreCompiler
 12 {
 13     /**
 14      * Compiles a controller.
 15      *
 16      * @param $stub
 17      * @param $modelName
 18      * @param $modelData
 19      * @param \stdClass $scaffolderConfig
 20      * @param $hash
 21      * @param \Scaffolder\Support\Contracts\ScaffolderExtensionInterface[] $extensions
 22      * @param null $extra
 23      *
 24      * @return string
 25      */
 26     public function compile($stub, $modelName, $modelData, stdClass $scaffolderConfig, $hash, array $extensions, $extra = null)
 27     {
 28         if (File::exists(base_path('scaffolder-config/cache/controller_' . $hash . self::CACHE_EXT)))
 29         {
 30             return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
 31         }
 32         else
 33         {
 34             $this->stub = $stub;
 35 
 36             $this->replaceClassName($modelName)
 37                 ->setValidations($modelData)
 38                 ->replacePrimaryKey($modelData)
 39                 ->replaceRoutePrefix($scaffolderConfig->routing->prefix);
 40 
 41             foreach ($extensions as $extension)
 42             {
 43                 $this->stub = $extension->runAfterControllerIsCompiled($this->stub, $modelData, $scaffolderConfig);
 44             }
 45 
 46             return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
 47         }
 48     }
 49 
 50     /**
 51      * Store the compiled stub.
 52      *
 53      * @param $modelName
 54      * @param \stdClass $scaffolderConfig
 55      * @param $compiled
 56      * @param \Scaffolder\Support\FileToCompile $fileToCompile
 57      *
 58      * @return string
 59      */
 60     protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
 61     {
 62         $path = PathParser::parse($scaffolderConfig->paths->controllers) . $modelName . 'Controller.php';
 63 
 64         // Store in cache
 65         if ($fileToCompile->cached)
 66         {
 67             File::copy(base_path('scaffolder-config/cache/controller_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 68         }
 69         else
 70         {
 71             File::put(base_path('scaffolder-config/cache/controller_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
 72             File::copy(base_path('scaffolder-config/cache/controller_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 73         }
 74 
 75         return $path;
 76     }
 77 
 78     /**
 79      * Set validations.
 80      *
 81      * @param $modelData
 82      *
 83      * @return $this
 84      */
 85     protected function setValidations($modelData)
 86     {
 87         $fields = '';
 88         $firstIteration = true;
 89 
 90         foreach ($modelData->fields as $field)
 91         {
 92             if ($firstIteration)
 93             {
 94                 $fields .= sprintf("'%s' => '%s'," . PHP_EOL, $field->name, $field->validations);
 95                 $firstIteration = false;
 96             }
 97             else
 98             {
 99                 $fields .= sprintf($this->tab(3) . "'%s' => '%s'," . PHP_EOL, $field->name, $field->validations);
100             }
101         }
102 
103         $this->stub = str_replace('{{validations}}', $fields, $this->stub);
104 
105         return $this;
106     }
107 
108     /**
109      * Replace the route prefix.
110      *
111      * @param $prefix
112      *
113      * @return $this
114      */
115     private function replaceRoutePrefix($prefix)
116     {
117         $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
118 
119         return $this;
120     }
121 
122     /**
123      * Replace the primary key.
124      *
125      * @param $modelData
126      *
127      * @return $this
128      */
129     private function replacePrimaryKey($modelData)
130     {
131         $primaryKey = 'id';
132 
133         foreach ($modelData->fields as $field)
134         {
135             if ($field->index == 'primary')
136             {
137                 $primaryKey = $field->name;
138                 break;
139             }
140         }
141 
142         $this->stub = str_replace('{{primaryKey}}', $primaryKey, $this->stub);
143 
144         return $this;
145     }
146 }
147 
Scaffolder v2.0.0 API documentation generated by ApiGen