mirror of
https://github.com/brmlab/brmsklad.git
synced 2025-10-30 15:53:59 +01:00
Upgrade CakePHP from 2.2.5 to 2.9.5
This commit is contained in:
parent
5a580df460
commit
235a541597
793 changed files with 60746 additions and 23753 deletions
|
|
@ -2,19 +2,18 @@
|
|||
/**
|
||||
* Schema database management for CakePHP.
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Model
|
||||
* @since CakePHP(tm) v 1.2.0.5550
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('Model', 'Model');
|
||||
|
|
@ -23,49 +22,49 @@ App::uses('ConnectionManager', 'Model');
|
|||
App::uses('File', 'Utility');
|
||||
|
||||
/**
|
||||
* Base Class for Schema management
|
||||
* Base Class for Schema management.
|
||||
*
|
||||
* @package Cake.Model
|
||||
*/
|
||||
class CakeSchema extends Object {
|
||||
class CakeSchema extends CakeObject {
|
||||
|
||||
/**
|
||||
* Name of the schema
|
||||
* Name of the schema.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = null;
|
||||
|
||||
/**
|
||||
* Path to write location
|
||||
* Path to write location.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $path = null;
|
||||
|
||||
/**
|
||||
* File to write
|
||||
* File to write.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $file = 'schema.php';
|
||||
|
||||
/**
|
||||
* Connection used for read
|
||||
* Connection used for read.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $connection = 'default';
|
||||
|
||||
/**
|
||||
* plugin name.
|
||||
* Plugin name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $plugin = null;
|
||||
|
||||
/**
|
||||
* Set of tables
|
||||
* Set of tables.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
|
|
@ -74,7 +73,7 @@ class CakeSchema extends Object {
|
|||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options optional load object properties
|
||||
* @param array $options Optional load object properties.
|
||||
*/
|
||||
public function __construct($options = array()) {
|
||||
parent::__construct();
|
||||
|
|
@ -87,7 +86,7 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
if (strtolower($this->name) === 'cake') {
|
||||
$this->name = Inflector::camelize(Inflector::slug(Configure::read('App.dir')));
|
||||
$this->name = 'App';
|
||||
}
|
||||
|
||||
if (empty($options['path'])) {
|
||||
|
|
@ -99,9 +98,9 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Builds schema object properties
|
||||
* Builds schema object properties.
|
||||
*
|
||||
* @param array $data loaded object properties
|
||||
* @param array $data Loaded object properties.
|
||||
* @return void
|
||||
*/
|
||||
public function build($data) {
|
||||
|
|
@ -130,29 +129,29 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Before callback to be implemented in subclasses
|
||||
* Before callback to be implemented in subclasses.
|
||||
*
|
||||
* @param array $event schema object properties
|
||||
* @return boolean Should process continue
|
||||
* @param array $event Schema object properties.
|
||||
* @return bool Should process continue.
|
||||
*/
|
||||
public function before($event = array()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* After callback to be implemented in subclasses
|
||||
* After callback to be implemented in subclasses.
|
||||
*
|
||||
* @param array $event schema object properties
|
||||
* @param array $event Schema object properties.
|
||||
* @return void
|
||||
*/
|
||||
public function after($event = array()) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads database and creates schema tables
|
||||
* Reads database and creates schema tables.
|
||||
*
|
||||
* @param array $options schema object properties
|
||||
* @return array Set of name and tables
|
||||
* @param array $options Schema object properties.
|
||||
* @return array Set of name and tables.
|
||||
*/
|
||||
public function load($options = array()) {
|
||||
if (is_string($options)) {
|
||||
|
|
@ -164,11 +163,10 @@ class CakeSchema extends Object {
|
|||
|
||||
$class = $name . 'Schema';
|
||||
|
||||
if (!class_exists($class)) {
|
||||
if (file_exists($path . DS . $file) && is_file($path . DS . $file)) {
|
||||
require_once $path . DS . $file;
|
||||
} elseif (file_exists($path . DS . 'schema.php') && is_file($path . DS . 'schema.php')) {
|
||||
require_once $path . DS . 'schema.php';
|
||||
if (!class_exists($class) && !$this->_requireFile($path, $file)) {
|
||||
$class = Inflector::camelize(Inflector::slug(Configure::read('App.dir'))) . 'Schema';
|
||||
if (!class_exists($class)) {
|
||||
$this->_requireFile($path, $file);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -180,7 +178,7 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Reads database and creates schema tables
|
||||
* Reads database and creates schema tables.
|
||||
*
|
||||
* Options
|
||||
*
|
||||
|
|
@ -188,8 +186,8 @@ class CakeSchema extends Object {
|
|||
* - 'name' - name of the schema
|
||||
* - 'models' - a list of models to use, or false to ignore models
|
||||
*
|
||||
* @param array $options schema object properties
|
||||
* @return array Array indexed by name and tables
|
||||
* @param array $options Schema object properties.
|
||||
* @return array Array indexed by name and tables.
|
||||
*/
|
||||
public function read($options = array()) {
|
||||
extract(array_merge(
|
||||
|
|
@ -226,12 +224,12 @@ class CakeSchema extends Object {
|
|||
foreach ($models as $model) {
|
||||
$importModel = $model;
|
||||
$plugin = null;
|
||||
if ($model == 'AppModel') {
|
||||
if ($model === 'AppModel') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($this->plugin)) {
|
||||
if ($model == $this->plugin . 'AppModel') {
|
||||
if ($model === $this->plugin . 'AppModel') {
|
||||
continue;
|
||||
}
|
||||
$importModel = $model;
|
||||
|
|
@ -254,44 +252,50 @@ class CakeSchema extends Object {
|
|||
continue;
|
||||
}
|
||||
|
||||
if (!is_object($Object) || $Object->useTable === false) {
|
||||
continue;
|
||||
}
|
||||
$db = $Object->getDataSource();
|
||||
if (is_object($Object) && $Object->useTable !== false) {
|
||||
$fulltable = $table = $db->fullTableName($Object, false, false);
|
||||
if ($prefix && strpos($table, $prefix) !== 0) {
|
||||
|
||||
$fulltable = $table = $db->fullTableName($Object, false, false);
|
||||
if ($prefix && strpos($table, $prefix) !== 0) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($fulltable, $currentTables)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$table = $this->_noPrefixTable($prefix, $table);
|
||||
|
||||
$key = array_search($fulltable, $currentTables);
|
||||
if (empty($tables[$table])) {
|
||||
$tables[$table] = $this->_columns($Object);
|
||||
$tables[$table]['indexes'] = $db->index($Object);
|
||||
$tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
|
||||
unset($currentTables[$key]);
|
||||
}
|
||||
if (empty($Object->hasAndBelongsToMany)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($Object->hasAndBelongsToMany as $assocData) {
|
||||
if (isset($assocData['with'])) {
|
||||
$class = $assocData['with'];
|
||||
}
|
||||
if (!is_object($Object->$class)) {
|
||||
continue;
|
||||
}
|
||||
$table = $this->_noPrefixTable($prefix, $table);
|
||||
$withTable = $db->fullTableName($Object->$class, false, false);
|
||||
if ($prefix && strpos($withTable, $prefix) !== 0) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($withTable, $currentTables)) {
|
||||
$key = array_search($withTable, $currentTables);
|
||||
$noPrefixWith = $this->_noPrefixTable($prefix, $withTable);
|
||||
|
||||
if (in_array($fulltable, $currentTables)) {
|
||||
$key = array_search($fulltable, $currentTables);
|
||||
if (empty($tables[$table])) {
|
||||
$tables[$table] = $this->_columns($Object);
|
||||
$tables[$table]['indexes'] = $db->index($Object);
|
||||
$tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
|
||||
unset($currentTables[$key]);
|
||||
}
|
||||
if (!empty($Object->hasAndBelongsToMany)) {
|
||||
foreach ($Object->hasAndBelongsToMany as $assocData) {
|
||||
if (isset($assocData['with'])) {
|
||||
$class = $assocData['with'];
|
||||
}
|
||||
if (is_object($Object->$class)) {
|
||||
$withTable = $db->fullTableName($Object->$class, false, false);
|
||||
if ($prefix && strpos($withTable, $prefix) !== 0) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($withTable, $currentTables)) {
|
||||
$key = array_search($withTable, $currentTables);
|
||||
$noPrefixWith = $this->_noPrefixTable($prefix, $withTable);
|
||||
|
||||
$tables[$noPrefixWith] = $this->_columns($Object->$class);
|
||||
$tables[$noPrefixWith]['indexes'] = $db->index($Object->$class);
|
||||
$tables[$noPrefixWith]['tableParameters'] = $db->readTableParameters($withTable);
|
||||
unset($currentTables[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$tables[$noPrefixWith] = $this->_columns($Object->$class);
|
||||
$tables[$noPrefixWith]['indexes'] = $db->index($Object->$class);
|
||||
$tables[$noPrefixWith]['tableParameters'] = $db->readTableParameters($withTable);
|
||||
unset($currentTables[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -336,11 +340,11 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Writes schema file from object or options
|
||||
* Writes schema file from object or options.
|
||||
*
|
||||
* @param array|object $object schema object or options array
|
||||
* @param array $options schema object properties to override object
|
||||
* @return mixed false or string written to file
|
||||
* @param array|object $object Schema object or options array.
|
||||
* @param array $options Schema object properties to override object.
|
||||
* @return mixed False or string written to file.
|
||||
*/
|
||||
public function write($object, $options = array()) {
|
||||
if (is_object($object)) {
|
||||
|
|
@ -393,56 +397,61 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Generate the code for a table. Takes a table name and $fields array
|
||||
* Returns a completed variable declaration to be used in schema classes
|
||||
* Generate the schema code for a table.
|
||||
*
|
||||
* Takes a table name and $fields array and returns a completed,
|
||||
* escaped variable declaration to be used in schema classes.
|
||||
*
|
||||
* @param string $table Table name you want returned.
|
||||
* @param array $fields Array of field information to generate the table with.
|
||||
* @return string Variable declaration for a schema class
|
||||
* @return string Variable declaration for a schema class.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function generateTable($table, $fields) {
|
||||
// Valid var name regex (http://www.php.net/manual/en/language.variables.basics.php)
|
||||
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $table)) {
|
||||
throw new Exception("Invalid table name '{$table}'");
|
||||
}
|
||||
|
||||
$out = "\tpublic \${$table} = array(\n";
|
||||
if (is_array($fields)) {
|
||||
$cols = array();
|
||||
foreach ($fields as $field => $value) {
|
||||
if ($field != 'indexes' && $field != 'tableParameters') {
|
||||
if ($field !== 'indexes' && $field !== 'tableParameters') {
|
||||
if (is_string($value)) {
|
||||
$type = $value;
|
||||
$value = array('type' => $type);
|
||||
}
|
||||
$col = "\t\t'{$field}' => array('type' => '" . $value['type'] . "', ";
|
||||
unset($value['type']);
|
||||
$col .= join(', ', $this->_values($value));
|
||||
} elseif ($field == 'indexes') {
|
||||
$col .= implode(', ', $this->_values($value));
|
||||
} elseif ($field === 'indexes') {
|
||||
$col = "\t\t'indexes' => array(\n\t\t\t";
|
||||
$props = array();
|
||||
foreach ((array)$value as $key => $index) {
|
||||
$props[] = "'{$key}' => array(" . join(', ', $this->_values($index)) . ")";
|
||||
$props[] = "'{$key}' => array(" . implode(', ', $this->_values($index)) . ")";
|
||||
}
|
||||
$col .= join(",\n\t\t\t", $props) . "\n\t\t";
|
||||
} elseif ($field == 'tableParameters') {
|
||||
$col .= implode(",\n\t\t\t", $props) . "\n\t\t";
|
||||
} elseif ($field === 'tableParameters') {
|
||||
$col = "\t\t'tableParameters' => array(";
|
||||
$props = array();
|
||||
foreach ((array)$value as $key => $param) {
|
||||
$props[] = "'{$key}' => '$param'";
|
||||
}
|
||||
$col .= join(', ', $props);
|
||||
$props = $this->_values($value);
|
||||
$col .= implode(', ', $props);
|
||||
}
|
||||
$col .= ")";
|
||||
$cols[] = $col;
|
||||
}
|
||||
$out .= join(",\n", $cols);
|
||||
$out .= implode(",\n", $cols);
|
||||
}
|
||||
$out .= "\n\t);\n";
|
||||
$out .= "\n\t);\n\n";
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two sets of schemas
|
||||
* Compares two sets of schemas.
|
||||
*
|
||||
* @param array|object $old Schema object or array
|
||||
* @param array|object $new Schema object or array
|
||||
* @return array Tables (that are added, dropped, or changed)
|
||||
* @param array|object $old Schema object or array.
|
||||
* @param array|object $new Schema object or array.
|
||||
* @return array Tables (that are added, dropped, or changed.)
|
||||
*/
|
||||
public function compare($old, $new = null) {
|
||||
if (empty($new)) {
|
||||
|
|
@ -465,11 +474,11 @@ class CakeSchema extends Object {
|
|||
}
|
||||
$tables = array();
|
||||
foreach ($new as $table => $fields) {
|
||||
if ($table == 'missing') {
|
||||
if ($table === 'missing') {
|
||||
continue;
|
||||
}
|
||||
if (!array_key_exists($table, $old)) {
|
||||
$tables[$table]['add'] = $fields;
|
||||
$tables[$table]['create'] = $fields;
|
||||
} else {
|
||||
$diff = $this->_arrayDiffAssoc($fields, $old[$table]);
|
||||
if (!empty($diff)) {
|
||||
|
|
@ -484,6 +493,9 @@ class CakeSchema extends Object {
|
|||
foreach ($fields as $field => $value) {
|
||||
if (!empty($old[$table][$field])) {
|
||||
$diff = $this->_arrayDiffAssoc($value, $old[$table][$field]);
|
||||
if (empty($diff)) {
|
||||
$diff = $this->_arrayDiffAssoc($old[$table][$field], $value);
|
||||
}
|
||||
if (!empty($diff) && $field !== 'indexes' && $field !== 'tableParameters') {
|
||||
$tables[$table]['change'][$field] = $value;
|
||||
}
|
||||
|
|
@ -524,15 +536,15 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Extended array_diff_assoc noticing change from/to NULL values
|
||||
* Extended array_diff_assoc noticing change from/to NULL values.
|
||||
*
|
||||
* It behaves almost the same way as array_diff_assoc except for NULL values: if
|
||||
* one of the values is not NULL - change is detected. It is useful in situation
|
||||
* where one value is strval('') ant other is strval(null) - in string comparing
|
||||
* methods this results as EQUAL, while it is not.
|
||||
*
|
||||
* @param array $array1 Base array
|
||||
* @param array $array2 Corresponding array checked for equality
|
||||
* @param array $array1 Base array.
|
||||
* @param array $array2 Corresponding array checked for equality.
|
||||
* @return array Difference as array with array(keys => values) from input array
|
||||
* where match was not found.
|
||||
*/
|
||||
|
|
@ -544,7 +556,7 @@ class CakeSchema extends Object {
|
|||
continue;
|
||||
}
|
||||
$correspondingValue = $array2[$key];
|
||||
if (is_null($value) !== is_null($correspondingValue)) {
|
||||
if (($value === null) !== ($correspondingValue === null)) {
|
||||
$difference[$key] = $value;
|
||||
continue;
|
||||
}
|
||||
|
|
@ -564,17 +576,17 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Formats Schema columns from Model Object
|
||||
* Formats Schema columns from Model Object.
|
||||
*
|
||||
* @param array $values options keys(type, null, default, key, length, extra)
|
||||
* @return array Formatted values
|
||||
* @param array $values Options keys(type, null, default, key, length, extra).
|
||||
* @return array Formatted values.
|
||||
*/
|
||||
protected function _values($values) {
|
||||
$vals = array();
|
||||
if (is_array($values)) {
|
||||
foreach ($values as $key => $val) {
|
||||
if (is_array($val)) {
|
||||
$vals[] = "'{$key}' => array(" . implode(", ", $this->_values($val)) . ")";
|
||||
$vals[] = "'{$key}' => array(" . implode(", ", $this->_values($val)) . ")";
|
||||
} else {
|
||||
$val = var_export($val, true);
|
||||
if ($val === 'NULL') {
|
||||
|
|
@ -592,10 +604,10 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Formats Schema columns from Model Object
|
||||
* Formats Schema columns from Model Object.
|
||||
*
|
||||
* @param array $Obj model object
|
||||
* @return array Formatted columns
|
||||
* @param array &$Obj model object.
|
||||
* @return array Formatted columns.
|
||||
*/
|
||||
protected function _columns(&$Obj) {
|
||||
$db = $Obj->getDataSource();
|
||||
|
|
@ -603,7 +615,7 @@ class CakeSchema extends Object {
|
|||
|
||||
$columns = array();
|
||||
foreach ($fields as $name => $value) {
|
||||
if ($Obj->primaryKey == $name) {
|
||||
if ($Obj->primaryKey === $name) {
|
||||
$value['key'] = 'primary';
|
||||
}
|
||||
if (!isset($db->columns[$value['type']])) {
|
||||
|
|
@ -619,7 +631,7 @@ class CakeSchema extends Object {
|
|||
unset($value['limit']);
|
||||
}
|
||||
|
||||
if (isset($value['default']) && ($value['default'] === '' || $value['default'] === false)) {
|
||||
if (isset($value['default']) && ($value['default'] === '' || ($value['default'] === false && $value['type'] !== 'boolean'))) {
|
||||
unset($value['default']);
|
||||
}
|
||||
if (empty($value['length'])) {
|
||||
|
|
@ -635,10 +647,10 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Compare two schema files table Parameters
|
||||
* Compare two schema files table Parameters.
|
||||
*
|
||||
* @param array $new New indexes
|
||||
* @param array $old Old indexes
|
||||
* @param array $new New indexes.
|
||||
* @param array $old Old indexes.
|
||||
* @return mixed False on failure, or an array of parameters to add & drop.
|
||||
*/
|
||||
protected function _compareTableParameters($new, $old) {
|
||||
|
|
@ -650,11 +662,11 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Compare two schema indexes
|
||||
* Compare two schema indexes.
|
||||
*
|
||||
* @param array $new New indexes
|
||||
* @param array $old Old indexes
|
||||
* @return mixed false on failure or array of indexes to add and drop
|
||||
* @param array $new New indexes.
|
||||
* @param array $old Old indexes.
|
||||
* @return mixed False on failure or array of indexes to add and drop.
|
||||
*/
|
||||
protected function _compareIndexes($new, $old) {
|
||||
if (!is_array($new) || !is_array($old)) {
|
||||
|
|
@ -701,14 +713,33 @@ class CakeSchema extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Trim the table prefix from the full table name, and return the prefix-less table
|
||||
* Trim the table prefix from the full table name, and return the prefix-less
|
||||
* table.
|
||||
*
|
||||
* @param string $prefix Table prefix
|
||||
* @param string $table Full table name
|
||||
* @return string Prefix-less table name
|
||||
* @param string $prefix Table prefix.
|
||||
* @param string $table Full table name.
|
||||
* @return string Prefix-less table name.
|
||||
*/
|
||||
protected function _noPrefixTable($prefix, $table) {
|
||||
return preg_replace('/^' . preg_quote($prefix) . '/', '', $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to require the schema file specified.
|
||||
*
|
||||
* @param string $path Filesystem path to the file.
|
||||
* @param string $file Filesystem basename of the file.
|
||||
* @return bool True when a file was successfully included, false on failure.
|
||||
*/
|
||||
protected function _requireFile($path, $file) {
|
||||
if (file_exists($path . DS . $file) && is_file($path . DS . $file)) {
|
||||
require_once $path . DS . $file;
|
||||
return true;
|
||||
} elseif (file_exists($path . DS . 'schema.php') && is_file($path . DS . 'schema.php')) {
|
||||
require_once $path . DS . 'schema.php';
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue