patches/PersistenceCreator.php
changeset 30 5751ca5a55c0
child 33 5cb77e9211d8
equal deleted inserted replaced
29:42432a207bd0 30:5751ca5a55c0
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * PersistenceCreator for the BFW Framework
       
     5  *
       
     6  * @copyright Copyright(C) 2016 4customers
       
     7  * @author    Markus Bröker<mbroeker@4customers.de>
       
     8  */
       
     9 define("VERSION", "0.1");
       
    10 require_once '../config/config.php';
       
    11 
       
    12 $header = '<?php
       
    13 
       
    14 /**
       
    15  * Entities for The BFW Framework
       
    16  *
       
    17  * @copyright Copyright(C) 2016 4customers
       
    18  * @author    Markus Bröker<mbroeker@4customers.de>
       
    19  */
       
    20 
       
    21 namespace bfw\entities\core;
       
    22 
       
    23 use bfw\core\Entity;
       
    24 
       
    25 /**
       
    26 ';
       
    27 
       
    28 $sql = "SHOW TABLES";
       
    29 $db = bfw\database\Database::getInstance();
       
    30 
       
    31 $rows = $db->query($sql);
       
    32 
       
    33 foreach ($rows as $row) {
       
    34     foreach ($row as $name) {
       
    35         $fields = $db->query(sprintf("DESCRIBE %s", $name));
       
    36         $className = $name;
       
    37         $className = preg_replace('/^t_([a-z])/e', 'strtoupper("T$1")', $className);
       
    38         $className = preg_replace('/^r_([a-z])/e', 'strtoupper("R$1")', $className);
       
    39         $className = preg_replace('/_([a-z])/e', 'strtoupper("$1")', $className);
       
    40         $filename = sprintf("%s/classes/bfw/entities/core/%s.php", BFW_PATH, $className);
       
    41 
       
    42         $handle = fopen($filename, "w+");
       
    43         $content = $header;
       
    44         foreach ($fields as $field) {
       
    45             if ($field['Field'] != 'id') {
       
    46                 $methodName = ucfirst($field['Field']);
       
    47                 $methodName = preg_replace('/_([a-z])/e', 'strtoupper("$1")', $methodName);
       
    48                 $type = substr($field['Type'], 0, strpos($field['Type'], '('));
       
    49 
       
    50                 switch ($type) {
       
    51                     case 'int':
       
    52                     case 'bigint':
       
    53                     case 'smallint':
       
    54                         $type = "int";
       
    55                         break;
       
    56                     case 'tinyint':
       
    57                         $type = "bool";
       
    58                         break;
       
    59                     case 'real':
       
    60                     case 'float':
       
    61                         $type = "float";
       
    62                         break;
       
    63                     case 'varchar':
       
    64                     case 'text':
       
    65                     case 'char':
       
    66                         $type = "string";
       
    67                         break;
       
    68                 }
       
    69 
       
    70                 $content .= sprintf(" * @method %s get%s()\n", $type, $methodName);
       
    71                 $content .= sprintf(" * @method set%s(\$%s)\n", $methodName, $type);
       
    72             }
       
    73         }
       
    74         $content .= "*/\n";
       
    75 
       
    76         $content .= sprintf("class %s extends Entity {\n", $className);
       
    77         $content .= sprintf("    public function __construct() {\n");
       
    78         $content .= sprintf("        parent::__construct('%s');\n", $name);
       
    79         $content .= sprintf("    }\n");
       
    80         $content .= sprintf("}\n");
       
    81 
       
    82         fwrite($handle, $content);
       
    83         fclose($handle);
       
    84     }
       
    85 }