function sqlToYml($sql, $model_name, $database_name) { $output = ""; $model_name_camel = Dinkly::convertToCamelCase($model_name, 1); // Output the table name $output .= "table_name: " . $model_name . "\nregistry:\n"; // Loop over the result set foreach ($sql as $row) { if (in_array($row["Field"], array("id", "created_at", "updated_at"))) { $output .= " - " . $row["Field"] . "\n"; } else { //Separate the type and length $type = explode("(", $row["Type"]); /* Output the row/collection indicator */ //add the name $output .= " - " . $row["Field"] . ":"; //add the type $output .= " { type: " . $type[0]; //if the length contains a comma (for decimals) quote the length if ($type[1] and strpos($type[1], ",") != false) { $output .= ", length: " . str_replace(")", "", $type[1]) . "'"; } elseif ($type[1]) { $output .= ", length: " . str_replace(")", "", $type[1]); } //set whether null is allowable $output .= ", allow_null: "; if ($row["Null"] == "Yes") { $output .= "true"; } else { $output .= "false"; } //add a line break $output .= " }\n"; } } //Set directory path to keep things cleaner $dir = dirname(__FILE__) . "/../config/schemas/" . $database_name; $yml_file = $model_name_camel . ".yml"; //Create directory if it doesn"t exist if (!file_exists($dir)) { shell_exec("mkdir {$dir}"); } //If file exists move it to a backup if (file_exists("{$dir}/{$yml_file}")) { shell_exec("mv {$dir}/{$yml_file} {$dir}/{$yml_file}~"); } //Write to the yml file $handle = fopen("{$dir}/{$yml_file}", "w") or die("Cannot open file: " . $yml_file); fwrite($handle, $output); echo "\nYML file created...\n"; }
<?php //rename_app require_once 'config/bootstrap.php'; $options = getopt("a:d:"); if (!isset($options['a']) || !isset($options['d'])) { echo "\nPlease use the -a flag and -d flag to indicate the source application name and the destination application name to use.\nExample: php tools/copy_app.php -a=admin -d=my_admin\n"; } else { echo "\nCopying " . $options['a'] . " app to " . $options['d'] . " app \n"; $source = $_SERVER['APPLICATION_ROOT'] . 'apps/' . $options['a']; $dest = $_SERVER['APPLICATION_ROOT'] . 'apps/' . $options['d']; $source_prefix = Dinkly::convertToCamelCase($options['a'], true); $destination_prefix = Dinkly::convertToCamelCase($options['d'], true); if (mkdir($dest)) { echo "\nCreated directory for " . $options['d'] . " app \n"; } echo "\nCopying files for " . $options['d'] . " app:\n"; foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) { if ($item->isDir()) { mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); } else { $dest_file = $dest . DIRECTORY_SEPARATOR . str_replace($source_prefix, $destination_prefix, $iterator->getSubPathName()); copy($item, $dest_file); $contents = file_get_contents($dest_file); $contents = str_replace($options['a'], $options['d'], $contents); $contents = str_replace($source_prefix, $destination_prefix, $contents); file_put_contents($dest_file, $contents); echo $dest_file . "\n"; } } echo "\n" . $options['d'] . " app succesfully create!\n\n";
/** * Update an existing database object * * @return bool true on success of query or no changes to be made or false on failed DB execution * @throws Exception if unable to perform update of object */ protected function update() { if (!$this->db) { throw new Exception("Unable to perform update without a database object"); } $reg = $this->getRegistry(); $is_valid = false; $query = "update " . $this->getDBTable() . " set "; $i = 1; foreach ($this->getColumns() as $col) { if (array_key_exists($col, $this->regDirty)) { if ($i < sizeof($this->regDirty)) { $query .= $col . "=" . $this->db->quote($this->{$reg}[$col]) . ", "; $is_valid = true; } else { if (sizeof($this->regDirty) == 1 || $i == sizeof($this->regDirty)) { $query .= $col . "=" . $this->db->quote($this->{$reg}[$col]); $is_valid = true; } } $i++; } } $primaryKey = $this->getPrimaryKey(); $query .= " where " . $primaryKey . "='" . $this->{Dinkly::convertToCamelCase($primaryKey, true)} . "'"; if ($is_valid) { return $this->db->exec($query); } else { return false; } }
<?php /** * Used for upgrade from v2.04 or lower to prefix the app name to the class name * and file name for controllers */ require_once 'config/bootstrap.php'; $apps = array_filter(glob($_SERVER['APPLICATION_ROOT'] . 'apps/*'), 'is_dir'); foreach ($apps as $app) { $app_path_parts = explode('/', $app); $app_name = end($app_path_parts); $source_prefix = Dinkly::convertToCamelCase($app_name, true); echo "\nUpdateing " . $app_name . " app\n"; foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($app, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) { if (!$item->isDir()) { $name_parts = explode('/', $iterator->getSubPathName()); $file_name = end($name_parts); if (strstr($file_name, 'Controller') && !strstr($file_name, $source_prefix . "Controller") && !strstr($file_name, $source_prefix)) { $controller_name = str_replace('.php', '', $file_name); $new_file_name = $source_prefix . $file_name; $new_controller_name = $source_prefix . $controller_name; $source = $app . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); $dest = $app . DIRECTORY_SEPARATOR . str_replace($file_name, $new_file_name, $iterator->getSubPathName()); echo $file_name . " changes to " . $new_file_name . "\n"; echo $controller_name . " changes to " . $new_controller_name . "\n"; rename($source, $dest); $contents = file_get_contents($dest); $contents = str_replace($controller_name, $new_controller_name, $contents); file_put_contents($dest, $contents); } }
/** * Load a specific fixture to populate DB table * * @param string $set: folder name of fixtures you would like to load * @param string $model_name: name model fixture to be parsed * @param bool $truncate (optional): truncate the table if set to true, or append records if false * @param bool $verbose_output (optional): how chatty would you like the build to be? * @param string $override_database_name (optional): if passed, this will override the name of the database as it appears in config/db.yml * * @return bool true if loaded successfully, false if load fails */ public static function loadFixture($set, $model_name, $plugin_name = null, $truncate = true, $verbose_output = true, $override_database_name = null) { //Use the proper DB credentials, or apply a passed-in override $creds = DinklyDataConfig::getDBCreds(); if ($override_database_name) { $creds['name'] = $override_database_name; DinklyDataConfig::setActiveConnection($creds); } //Create database if it doesn't exist $db = self::fetchDB($creds); $file_path = null; if ($plugin_name) { $file_path = $_SERVER['APPLICATION_ROOT'] . "plugins/" . $plugin_name . "/config/fixtures/" . $set . "/" . $model_name . ".yml"; } else { $file_path = $_SERVER['APPLICATION_ROOT'] . "config/fixtures/" . $set . "/" . $model_name . ".yml"; } if ($verbose_output) { echo "Attempting to parse '" . $model_name . "' fixture yaml..."; } $fixture = Yaml::parse($file_path); $model_name = $collection_name = null; if (isset($fixture['table_name'])) { $model_name = Dinkly::convertToCamelCase($fixture['table_name'], true); if ($verbose_output) { echo "success!\n"; } } else { return false; } if (isset($fixture['records'])) { if ($truncate) { if ($verbose_output) { echo "Truncating '" . $fixture['table_name'] . "'..."; } $db->exec("truncate table " . $fixture['table_name']); if ($verbose_output) { echo "success!\n"; } } $count = sizeof($fixture['records']); if ($verbose_output) { echo "Inserting " . $count . " record(s) into table '" . $fixture['table_name'] . "'"; } foreach ($fixture['records'] as $pos => $record) { if ($verbose_output) { echo "..."; } $model = new $model_name(); foreach ($record as $col_name => $value) { //Automatically set created date if none was passed if ($col_name == 'created_at' && $value == "") { $value = date('Y-m-d G:i:s'); } $set_field = 'set' . Dinkly::convertToCamelCase($col_name, true); $model->{$set_field}($value); } $model->save(); } if ($verbose_output) { echo "success!\n"; } return true; } }