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 ModelCompiler extends AbstractCoreCompiler
 12 {
 13     /**
 14      * Compiles a model.
 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/model_' . $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->replaceNamespace($scaffolderConfig)
 37                 ->replaceNamespaceModelExtend($scaffolderConfig)
 38                 ->replaceClassName($modelName)
 39                 ->setPrimaryKey($modelData)
 40                 ->addFillable($modelData)
 41                 ->replaceTableName($scaffolderConfig, $modelName);
 42 
 43             foreach ($extensions as $extension)
 44             {
 45                 $this->stub = $extension->runAfterModelIsCompiled($this->stub, $modelData, $scaffolderConfig);
 46             }
 47 
 48             return $this->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
 49         }
 50     }
 51 
 52     /**
 53      * Store the compiled stub.
 54      *
 55      * @param $modelName
 56      * @param \stdClass $scaffolderConfig
 57      * @param $compiled
 58      * @param \Scaffolder\Support\FileToCompile $fileToCompile
 59      *
 60      * @return string
 61      */
 62     protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
 63     {
 64         $path = PathParser::parse($scaffolderConfig->paths->models) . $modelName . '.php';
 65 
 66         // Store in cache
 67         if ($fileToCompile->cached)
 68         {
 69             File::copy(base_path('scaffolder-config/cache/model_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 70         }
 71         else
 72         {
 73             File::put(base_path('scaffolder-config/cache/model_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
 74             File::copy(base_path('scaffolder-config/cache/model_' . $fileToCompile->hash . self::CACHE_EXT), $path);
 75         }
 76 
 77         return $path;
 78     }
 79 
 80     /**
 81      * Replace the namespace which the model extends
 82      *
 83      * @param \stdClass $scaffolderConfig
 84      *
 85      * @return $this
 86      */
 87     private function replaceNamespaceModelExtend(stdClass $scaffolderConfig)
 88     {
 89         $this->stub = str_replace('{{namespace_model_extend}}', $scaffolderConfig->inheritance->model, $this->stub);
 90 
 91         return $this;
 92     }
 93 
 94     /**
 95      * Add fillable.
 96      *
 97      * @param $modelData
 98      *
 99      * @return $this
100      */
101     protected function addFillable($modelData)
102     {
103         $fields = '';
104         $firstIteration = true;
105 
106         foreach ($modelData->fields as $field)
107         {
108             if ($firstIteration)
109             {
110                 $fields .= sprintf("'%s'," . PHP_EOL, $field->name);
111                 $firstIteration = false;
112             }
113             else
114             {
115                 $fields .= sprintf($this->tab(2) . "'%s'," . PHP_EOL, $field->name);
116             }
117         }
118 
119         $this->stub = str_replace('{{fillable}}', $fields, $this->stub);
120 
121         return $this;
122     }
123 
124     /**
125      * Replace the table name.
126      *
127      * @param \stdClass $scaffolderConfig
128      * @param $modelName
129      *
130      * @return $this
131      */
132     protected function replaceTableName(stdClass $scaffolderConfig, $modelName)
133     {
134         $tableName = isset($scaffolderConfig->tableName) && !empty($scaffolderConfig->tableName) ? $scaffolderConfig->tableName : $modelName . 's';
135 
136         $this->stub = str_replace('{{table_name}}', strtolower($tableName), $this->stub);
137 
138         return $this;
139     }
140 
141     /**
142      * Set the primary key.
143      *
144      * @param $modelData
145      *
146      * @return $this
147      */
148     protected function setPrimaryKey($modelData)
149     {
150         $primaryKey = '// Using default primary key' . PHP_EOL;
151 
152         foreach ($modelData->fields as $field)
153         {
154             if ($field->index == 'primary')
155             {
156                 $primaryKey = 'protected $primaryKey = \'' . $field->name . '\';' . PHP_EOL;
157                 break;
158             }
159         }
160 
161         $this->stub = str_replace('{{primaryKey}}', $primaryKey, $this->stub);
162 
163         return $this;
164     }
165 }
166 
Scaffolder v2.0.0 API documentation generated by ApiGen