示例#1
0
 public function generateEntity($data)
 {
     Makiavelo::debug("Generating entity '" . $data['name'] . "'...", Makiavelo::DEBUG_LEVEL_INFO);
     $en_name = $this->en_name;
     $this->en_attributes = array_merge($this->default_entity_attributes, $data['fields']);
     //Grab the template for the class and replace the fields
     $entity_template = ROOT_PATH . Makiavelo::TEMPLATES_FOLDER . "EntityTemplateClass.php";
     Makiavelo::debug("Opening entity template: {$entity_template}", Makiavelo::DEBUG_LEVEL_INFO);
     $fp = fopen($entity_template, "r");
     $entity_code = "";
     if ($fp) {
         $entity_code = fread($fp, filesize($entity_template));
         $entity_code = str_replace("[NAME]", $en_name, $entity_code);
         $attr_code = "";
         foreach ($this->en_attributes as $att_name => $att_type) {
             Makiavelo::debug("Adding attribute: {$att_name} => {$att_type}", Makiavelo::DEBUG_LEVEL_INFO);
             $attr_code .= "private \$" . $att_name . "; //type: " . $att_type . "\n";
         }
         $entity_code = str_replace("[ATTRIBUTES]", $attr_code, $entity_code);
         fclose($fp);
         $entity_code = str_replace("[VALIDATIONS]", $this->validations_str, $entity_code);
     }
     //Save the template into the new entity
     $entity_destination_file = ROOT_PATH . Makiavelo::ENTITITES_FOLDER . $en_name . "Class.php";
     Makiavelo::debug("Saving entity in: {$entity_destination_file}", Makiavelo::DEBUG_LEVEL_INFO);
     if (file_exists($entity_destination_file)) {
         Makiavelo::info("Entity class already exists, not saving...");
         return;
     } else {
         $fp = fopen($entity_destination_file, "w");
         if ($fp) {
             fwrite($fp, $entity_code);
             fclose($fp);
         }
     }
 }