1 頁 (共 1 頁)

Administrator config generator

發表於 : 2016-04-11 10:57:43
yehlu
config/administrator/551gen.php

代碼: 選擇全部

<?php
//print_r($argv);

//Get .env
$handle = @fopen("../../.env", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        $tmp = explode("=", $buffer);
        if (count($tmp) > 1) {
            $env[$tmp[0]] = str_replace("\n", '', $tmp[1]);
        }
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

//print_r($env);
if (isset($argv['1'])) {

//Open MySQL
    $dsn = $env['DB_CONNECTION'] . ":host=" . $env['DB_HOST'] . ";dbname=" . $env['DB_DATABASE'];
//echo $dsn;
    try {
        $db = new PDO($dsn, $env['DB_USERNAME'], $env['DB_PASSWORD']);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
        $db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES UTF8');
    } catch (PDOException $e) {
        throw new PDOException("Error  : " . $e->getMessage());
    }

    try {
        $q = $db->prepare("DESCRIBE " . $argv['1']);
        $q->execute();
        $table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
        print_r($table_fields);
    } catch (PDOException $ex) {
        echo "An Error occured!"; //user friendly message
        some_logging_function($ex->getMessage());
    }

    $db = null;

    $phpstr = "<?php

/**
 *
 */
return array(
   'title' => '" . $argv['1'] . "',
   'single' => '" . $argv['1'] . "',
   'model' => 'App\Models\\" . $argv['1'] . "',
   /**
    * The display columns
    */
   'columns' => array(";
    foreach ($table_fields as $key => $value) {
        $phpstr .= "'$value',\n";
    }
    $phpstr .= "
   ),
   /**
    * The filter set
    */

   'filters' => array(";
    foreach ($table_fields as $key => $value) {
        $phpstr .= "'$value',\n";
    }
    $phpstr .= "
   ),

   /**
    * The editable fields
    */

   'edit_fields' => array(
";
    foreach ($table_fields as $key => $value) {
        $phpstr .= "'$value',\n";
    }
    $phpstr .= "
   ),

   'action_permissions' => array(
      'update' => function (" . '$model' . ") {
         //return false;
         return true;
      },
      'delete' => function (" . '$model' . ") {
         return false;
      },
   ),

);
";

    echo $phpstr;
    $fp = fopen($argv['1'] . '.php', 'w');
    fwrite($fp, $phpstr);
    fclose($fp);

}


Use Way

代碼: 選擇全部

php 551gen.php tablename