/** * Generates a model file. * * @param string $bundle_name Name of the bundle * @param string $model_name Name of the model * @param array $model_data Model data * * @return bool true if file was created, false if not. */ public function createModelFile($bundle_name, $model_name, $model_data) { $model_id = $this->getIdentifier($model_data); $this->file_uses = []; $bundle_directory = 'bundle/' . $bundle_name; if (is_dir($bundle_directory)) { $model_directory = $bundle_directory . '/model'; if (!is_dir($model_directory)) { mkdir($model_directory); } else { // Model directory cleanup if ($model_directory != $this->last_model_directory) { $model_directory_files = glob($model_directory . '/*.*'); if (!empty($model_directory_files)) { foreach ($model_directory_files as $file) { unlink($file); } } } } $this->last_model_directory = $model_directory; $model_file = $model_directory . '/' . App::toCamelCase($model_name) . '.php'; // Custom methods backup. if (file_exists($model_file)) { $file_content = file_get_contents($model_file); $regex = '/(?:(?:final )?)class (?:[A-Za-z_]+) extends Model\\n\\{(?:.*)\\/\\*(?:\\n)(?:[ ]+)\\* Custom methods.(?:[ ]*)(?:\\n)(?:[ ]+)\\*\\/(?:[ ]*)(?:\\n)(.*)\\}/s'; if (preg_match_all($regex, $file_content, $matches) && isset($matches[1][0]) && !preg_match('/^\\n+$/', $matches[1][0])) { $custom_methods = $matches[1][0]; } } $output_file = "<?php\n"; $output_file .= "namespace " . $this->getNamespace($bundle_name) . ";\n\n"; $output_file .= "/* use_emplacement */\n"; // Use lines can be injected at any moment with addUse() method. if (!empty($model_data)) { $output_file .= "final class " . App::toCamelCase($model_name) . " extends \\Sybil\\ORM\\DBMS\\Model\n"; $output_file .= "{\n"; if (isset($model_data['_params']['alias'])) { $output_file .= " protected " . '$entity_name' . " = '" . $model_data['_params']['alias'] . "';\n"; } else { $output_file .= " protected " . '$entity_name' . " = '" . App::unCamelCase($model_name) . "';\n"; } $output_file .= " protected " . '$identifier' . " = "; if (count($model_id) == 1) { $output_file .= "'" . $model_id[0] . "';\n"; } else { $model_id_array = "["; foreach ($model_id as $id) { $model_id_array .= "'" . $id . "',"; } $model_id_array = trim($model_id_array, ','); $output_file .= $model_id_array . "];\n"; } $output_file .= "\n"; foreach ($model_data as $property_name => $property_data) { if ($property_name != '_params' && $property_name != '_objects') { $output_file .= " protected \$" . $property_name . ";\n"; } } $output_file .= "\n /*\n * Getters & setters.\n */\n\n"; foreach ($model_data as $property_name => $property_data) { if ($property_name != '_params' && $property_name != '_objects') { $output_file .= " public function get" . App::toCamelCase($property_name) . "()\n {\n"; $output_file .= " return " . '$this->' . $property_name . ";\n"; $output_file .= " }\n\n"; if (!isset($property_data['auto']) || $property_data['auto'] == false) { $namespace = isset($property_data['indexOf']) ? '\\' . $this->getNamespace(isset($property_data['indexOf']['bundle']) ? $property_data['indexOf']['bundle'] : $bundle_name, $property_data['indexOf']['model']) . ' ' : ''; $output_file .= " public function set" . App::toCamelCase($property_name) . '(' . $namespace . '$' . $property_name . ')' . "\n {\n"; $output_file .= " " . '$this->' . $property_name . " = " . $this->applyFunctions($property_name, $property_data) . ";\n"; $output_file .= " }\n\n"; } } } if (isset($this->app_schema[$bundle_name][$model_name]['_objects'])) { $output_file .= " /*\n * Objects methods.\n */\n\n"; foreach ($this->app_schema[$bundle_name][$model_name]['_objects'] as $obj_bundle_name => $obj_models) { foreach ($obj_models as $obj_model_name => $obj_properties) { foreach ($obj_properties as $obj_property_name => $obj_property_data) { $namespace = $this->getNamespace($obj_bundle_name, $obj_model_name); $target_field = $model_id[0]; $this->addUse($namespace); $output_file .= " public function get" . App::pluralize(App::toCamelCase($obj_property_name)) . "(" . '$params=null' . ")\n {\n"; $output_file .= " \$" . $obj_model_name . " = new " . App::toCamelCase($obj_model_name) . "();\n"; $output_file .= " return \$" . $obj_model_name . "->match(['" . $obj_property_data['link'] . "' => " . '$this->' . $target_field . "])->params(" . '$params' . ")->execute();\n"; $output_file .= " }\n\n"; } } } } $output_file .= " /*\n * Custom methods.\n */\n"; $output_file .= isset($custom_methods) ? $custom_methods : ''; $output_file .= "}\n"; } $output_file = $this->applyUses($output_file); if (file_put_contents($model_file, $output_file)) { chmod($model_file, 0777); return true; } else { return false; } } else { return false; } }