mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-07 03:52:41 +00:00
More work towards model loading
This commit is contained in:
parent
fa83ecda5b
commit
4bb6d583b7
2 changed files with 0 additions and 0 deletions
279
lib/Cake/Model/BehaviorCollection.php
Normal file
279
lib/Cake/Model/BehaviorCollection.php
Normal file
|
@ -0,0 +1,279 @@
|
|||
<?php
|
||||
/**
|
||||
* BehaviorCollection
|
||||
*
|
||||
* Provides managment and interface for interacting with collections of behaviors.
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.model
|
||||
* @since CakePHP(tm) v 1.2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::uses('ObjectCollection', 'Core');
|
||||
|
||||
/**
|
||||
* Model behavior collection class.
|
||||
*
|
||||
* Defines the Behavior interface, and contains common model interaction functionality.
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.model
|
||||
*/
|
||||
class BehaviorCollection extends ObjectCollection {
|
||||
|
||||
/**
|
||||
* Stores a reference to the attached name
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
public $modelName = null;
|
||||
|
||||
/**
|
||||
* Keeps a list of all methods of attached behaviors
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $__methods = array();
|
||||
|
||||
/**
|
||||
* Keeps a list of all methods which have been mapped with regular expressions
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $__mappedMethods = array();
|
||||
|
||||
/**
|
||||
* Attaches a model object and loads a list of behaviors
|
||||
*
|
||||
* @todo Make this method a constructor instead..
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function init($modelName, $behaviors = array()) {
|
||||
$this->modelName = $modelName;
|
||||
|
||||
if (!empty($behaviors)) {
|
||||
foreach (BehaviorCollection::normalizeObjectArray($behaviors) as $behavior => $config) {
|
||||
$this->load($config['class'], $config['settings']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Backwards compatible alias for load()
|
||||
*
|
||||
* @return void
|
||||
* @deprecated Replaced with load()
|
||||
*/
|
||||
public function attach($behavior, $config = array()) {
|
||||
return $this->load($behavior, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a behavior into the collection. You can use use `$config['enabled'] = false`
|
||||
* to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors
|
||||
* can still be used as normal.
|
||||
*
|
||||
* @param string $behavior CamelCased name of the behavior to load
|
||||
* @param array $config Behavior configuration parameters
|
||||
* @return boolean True on success, false on failure
|
||||
* @throws MissingBehaviorFileException or MissingBehaviorClassException when a behavior could not be found.
|
||||
*/
|
||||
public function load($behavior, $config = array()) {
|
||||
list($plugin, $name) = pluginSplit($behavior);
|
||||
$class = $name . 'Behavior';
|
||||
|
||||
if (!App::import('Behavior', $behavior)) {
|
||||
throw new MissingBehaviorFileException(array(
|
||||
'file' => Inflector::underscore($behavior) . '.php',
|
||||
'class' => $class
|
||||
));
|
||||
}
|
||||
if (!class_exists($class)) {
|
||||
throw new MissingBehaviorClassException(array(
|
||||
'file' => Inflector::underscore($behavior) . '.php',
|
||||
'class' => $class
|
||||
));
|
||||
}
|
||||
|
||||
if (!isset($this->{$name})) {
|
||||
if (ClassRegistry::isKeySet($class)) {
|
||||
$this->_loaded[$name] = ClassRegistry::getObject($class);
|
||||
} else {
|
||||
$this->_loaded[$name] = new $class();
|
||||
ClassRegistry::addObject($class, $this->_loaded[$name]);
|
||||
if (!empty($plugin)) {
|
||||
ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$name]);
|
||||
}
|
||||
}
|
||||
} elseif (isset($this->_loaded[$name]->settings) && isset($this->_loaded[$name]->settings[$this->modelName])) {
|
||||
if ($config !== null && $config !== false) {
|
||||
$config = array_merge($this->_loaded[$name]->settings[$this->modelName], $config);
|
||||
} else {
|
||||
$config = array();
|
||||
}
|
||||
}
|
||||
if (empty($config)) {
|
||||
$config = array();
|
||||
}
|
||||
$this->_loaded[$name]->setup(ClassRegistry::getObject($this->modelName), $config);
|
||||
|
||||
foreach ($this->_loaded[$name]->mapMethods as $method => $alias) {
|
||||
$this->__mappedMethods[$method] = array($alias, $name);
|
||||
}
|
||||
$methods = get_class_methods($this->_loaded[$name]);
|
||||
$parentMethods = array_flip(get_class_methods('ModelBehavior'));
|
||||
$callbacks = array(
|
||||
'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
|
||||
'beforeDelete', 'afterDelete', 'afterError'
|
||||
);
|
||||
|
||||
foreach ($methods as $m) {
|
||||
if (!isset($parentMethods[$m])) {
|
||||
$methodAllowed = (
|
||||
$m[0] != '_' && !array_key_exists($m, $this->__methods) &&
|
||||
!in_array($m, $callbacks)
|
||||
);
|
||||
if ($methodAllowed) {
|
||||
$this->__methods[$m] = array($m, $name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$configDisabled = isset($config['enabled']) && $config['enabled'] === false;
|
||||
if (!in_array($name, $this->_enabled) && !$configDisabled) {
|
||||
$this->enable($name);
|
||||
} elseif ($configDisabled) {
|
||||
$this->disable($name);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detaches a behavior from a model
|
||||
*
|
||||
* @param string $name CamelCased name of the behavior to unload
|
||||
* @return void
|
||||
*/
|
||||
public function unload($name) {
|
||||
list($plugin, $name) = pluginSplit($name);
|
||||
if (isset($this->_loaded[$name])) {
|
||||
$this->_loaded[$name]->cleanup(ClassRegistry::getObject($this->modelName));
|
||||
unset($this->_loaded[$name]);
|
||||
}
|
||||
foreach ($this->__methods as $m => $callback) {
|
||||
if (is_array($callback) && $callback[1] == $name) {
|
||||
unset($this->__methods[$m]);
|
||||
}
|
||||
}
|
||||
$this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Backwards compatible alias for unload()
|
||||
*
|
||||
* @param string $name Name of behavior
|
||||
* @return void
|
||||
* @deprecated Use unload instead.
|
||||
*/
|
||||
public function detach($name) {
|
||||
return $this->unload($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a behavior method
|
||||
*
|
||||
* @return array All methods for all behaviors attached to this object
|
||||
*/
|
||||
public function dispatchMethod(&$model, $method, $params = array(), $strict = false) {
|
||||
$methods = array_keys($this->__methods);
|
||||
$check = array_flip($methods);
|
||||
$found = isset($check[$method]);
|
||||
$call = null;
|
||||
|
||||
if ($strict && !$found) {
|
||||
trigger_error(sprintf(__("BehaviorCollection::dispatchMethod() - Method %s not found in any attached behavior"), $method), E_USER_WARNING);
|
||||
return null;
|
||||
} elseif ($found) {
|
||||
$methods = array_combine($methods, array_values($this->__methods));
|
||||
$call = $methods[$method];
|
||||
} else {
|
||||
$count = count($this->__mappedMethods);
|
||||
$mapped = array_keys($this->__mappedMethods);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if (preg_match($mapped[$i] . 'i', $method)) {
|
||||
$call = $this->__mappedMethods[$mapped[$i]];
|
||||
array_unshift($params, $method);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($call)) {
|
||||
return call_user_func_array(
|
||||
array(&$this->_loaded[$call[1]], $call[0]),
|
||||
array_merge(array(&$model), $params)
|
||||
);
|
||||
}
|
||||
return array('unhandled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a behavior callback on all attached behavior objects
|
||||
*
|
||||
* @param model $model
|
||||
* @param string $callback
|
||||
* @param array $params
|
||||
* @param array $options
|
||||
* @return mixed
|
||||
*/
|
||||
public function trigger(&$model, $callback, $params = array(), $options = array()) {
|
||||
if (empty($this->_enabled)) {
|
||||
return true;
|
||||
}
|
||||
$options = array_merge(
|
||||
array('break' => false, 'breakOn' => array(null, false), 'modParams' => false),
|
||||
$options
|
||||
);
|
||||
foreach ($this->_enabled as $name) {
|
||||
$result = call_user_func_array(
|
||||
array(&$this->_loaded[$name], $callback),
|
||||
array_merge(array(&$model), $params)
|
||||
);
|
||||
if (
|
||||
$options['break'] && ($result === $options['breakOn'] ||
|
||||
(is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
|
||||
) {
|
||||
return $result;
|
||||
} elseif ($options['modParams'] && is_array($result)) {
|
||||
$params[0] = $result;
|
||||
}
|
||||
}
|
||||
if ($options['modParams'] && isset($params[0])) {
|
||||
return $params[0];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the method list for attached behaviors, i.e. all public, non-callback methods
|
||||
*
|
||||
* @return array All public methods for all behaviors attached to this collection
|
||||
*/
|
||||
public function methods() {
|
||||
return $this->__methods;
|
||||
}
|
||||
|
||||
}
|
276
lib/Cake/Model/ConnectionManager.php
Normal file
276
lib/Cake/Model/ConnectionManager.php
Normal file
|
@ -0,0 +1,276 @@
|
|||
<?php
|
||||
/**
|
||||
* Datasource connection manager
|
||||
*
|
||||
* Provides an interface for loading and enumerating connections defined in app/config/database.php
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.model
|
||||
* @since CakePHP(tm) v 0.10.x.1402
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Manages loaded instances of DataSource objects
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.model
|
||||
*/
|
||||
class ConnectionManager {
|
||||
|
||||
/**
|
||||
* Holds a loaded instance of the Connections object
|
||||
*
|
||||
* @var DATABASE_CONFIG
|
||||
* @access public
|
||||
*/
|
||||
public $config = null;
|
||||
|
||||
/**
|
||||
* Holds instances DataSource objects
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
protected $_dataSources = array();
|
||||
|
||||
/**
|
||||
* Contains a list of all file and class names used in Connection settings
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
protected $_connectionsEnum = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
*/
|
||||
function __construct() {
|
||||
include_once CONFIGS . 'database.php';
|
||||
if (class_exists('DATABASE_CONFIG')) {
|
||||
$this->config = new DATABASE_CONFIG();
|
||||
$this->_getConnectionObjects();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a reference to the ConnectionManger object instance
|
||||
*
|
||||
* @return object Instance
|
||||
*/
|
||||
public static function &getInstance() {
|
||||
static $instance = array();
|
||||
|
||||
if (!$instance) {
|
||||
$instance[0] = new ConnectionManager();
|
||||
}
|
||||
|
||||
return $instance[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a reference to a DataSource object
|
||||
*
|
||||
* @param string $name The name of the DataSource, as defined in app/config/database.php
|
||||
* @return object Instance
|
||||
*/
|
||||
public static function &getDataSource($name) {
|
||||
$_this = ConnectionManager::getInstance();
|
||||
|
||||
if (!empty($_this->_dataSources[$name])) {
|
||||
$return = $_this->_dataSources[$name];
|
||||
return $return;
|
||||
}
|
||||
|
||||
if (empty($_this->_connectionsEnum[$name])) {
|
||||
trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s"), $name), E_USER_ERROR);
|
||||
$null = null;
|
||||
return $null;
|
||||
}
|
||||
$conn = $_this->_connectionsEnum[$name];
|
||||
$class = $conn['classname'];
|
||||
|
||||
require LIBS . 'model' . DS . 'datasources' . DS . 'datasource.php';
|
||||
if ($_this->loadDataSource($name) === null) {
|
||||
trigger_error(sprintf(__("ConnectionManager::getDataSource - Could not load class %s"), $class), E_USER_ERROR);
|
||||
$null = null;
|
||||
return $null;
|
||||
}
|
||||
$_this->_dataSources[$name] = new $class($_this->config->{$name});
|
||||
$_this->_dataSources[$name]->configKeyName = $name;
|
||||
|
||||
$return = $_this->_dataSources[$name];
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of available DataSource connections
|
||||
*
|
||||
* @return array List of available connections
|
||||
*/
|
||||
public static function sourceList() {
|
||||
$_this = ConnectionManager::getInstance();
|
||||
return array_keys($_this->_dataSources);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a DataSource name from an object reference.
|
||||
*
|
||||
* **Warning** this method may cause fatal errors in PHP4.
|
||||
*
|
||||
* @param object $source DataSource object
|
||||
* @return string Datasource name, or null if source is not present
|
||||
* in the ConnectionManager.
|
||||
*/
|
||||
public static function getSourceName(&$source) {
|
||||
$_this = ConnectionManager::getInstance();
|
||||
foreach ($_this->_dataSources as $name => $ds) {
|
||||
if ($ds == $source) {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the DataSource class for the given connection name
|
||||
*
|
||||
* @param mixed $connName A string name of the connection, as defined in app/config/database.php,
|
||||
* or an array containing the filename (without extension) and class name of the object,
|
||||
* to be found in app/models/datasources/ or cake/libs/model/datasources/.
|
||||
* @return boolean True on success, null on failure or false if the class is already loaded
|
||||
*/
|
||||
public static function loadDataSource($connName) {
|
||||
$_this = ConnectionManager::getInstance();
|
||||
|
||||
if (is_array($connName)) {
|
||||
$conn = $connName;
|
||||
} else {
|
||||
$conn = $_this->_connectionsEnum[$connName];
|
||||
}
|
||||
|
||||
if (class_exists($conn['classname'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!empty($conn['parent'])) {
|
||||
$_this->loadDataSource($conn['parent']);
|
||||
}
|
||||
|
||||
$conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
|
||||
$class = "{$conn['plugin']}.{$conn['classname']}";
|
||||
|
||||
if (!App::import('Datasource', $class, !is_null($conn['plugin']))) {
|
||||
trigger_error(sprintf(__('ConnectionManager::loadDataSource - Unable to import DataSource class %s'), $class), E_USER_ERROR);
|
||||
return null;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of connections
|
||||
*
|
||||
* @return array An associative array of elements where the key is the connection name
|
||||
* (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
|
||||
*/
|
||||
public static function enumConnectionObjects() {
|
||||
$_this = ConnectionManager::getInstance();
|
||||
return $_this->_connectionsEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically creates a DataSource object at runtime, with the given name and settings
|
||||
*
|
||||
* @param string $name The DataSource name
|
||||
* @param array $config The DataSource configuration settings
|
||||
* @return object A reference to the DataSource object, or null if creation failed
|
||||
*/
|
||||
public static function &create($name = '', $config = array()) {
|
||||
$_this = ConnectionManager::getInstance();
|
||||
|
||||
if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
|
||||
$null = null;
|
||||
return $null;
|
||||
}
|
||||
$_this->config->{$name} = $config;
|
||||
$_this->_connectionsEnum[$name] = $_this->__connectionData($config);
|
||||
$return = $_this->getDataSource($name);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of class and file names associated with the user-defined DataSource connections
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _getConnectionObjects() {
|
||||
$connections = get_object_vars($this->config);
|
||||
|
||||
if ($connections != null) {
|
||||
foreach ($connections as $name => $config) {
|
||||
$this->_connectionsEnum[$name] = $this->__connectionData($config);
|
||||
}
|
||||
} else {
|
||||
throw new MissingConnectionException(array('class' => 'ConnectionManager'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file, class name, and parent for the given driver.
|
||||
*
|
||||
* @return array An indexed array with: filename, classname, plugin and parent
|
||||
*/
|
||||
private function __connectionData($config) {
|
||||
if (!isset($config['datasource'])) {
|
||||
$config['datasource'] = 'dbo';
|
||||
}
|
||||
$filename = $classname = $parent = $plugin = null;
|
||||
|
||||
if (!empty($config['driver'])) {
|
||||
$parent = $this->__connectionData(array('datasource' => $config['datasource']));
|
||||
$parentSource = preg_replace('/_source$/', '', $parent['filename']);
|
||||
|
||||
list($plugin, $classname) = pluginSplit($config['driver']);
|
||||
if ($plugin) {
|
||||
$source = Inflector::underscore($classname);
|
||||
} else {
|
||||
$source = $parentSource . '_' . $config['driver'];
|
||||
$classname = Inflector::camelize(strtolower($source));
|
||||
}
|
||||
$filename = $parentSource . DS . $source;
|
||||
} else {
|
||||
list($plugin, $classname) = pluginSplit($config['datasource']);
|
||||
if ($plugin) {
|
||||
$filename = Inflector::underscore($classname);
|
||||
} else {
|
||||
$filename = Inflector::underscore($config['datasource']);
|
||||
}
|
||||
if (substr($filename, -7) != '_source') {
|
||||
$filename .= '_source';
|
||||
}
|
||||
$classname = Inflector::camelize(strtolower($filename));
|
||||
}
|
||||
return compact('filename', 'classname', 'parent', 'plugin');
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*
|
||||
*/
|
||||
function __destruct() {
|
||||
if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) {
|
||||
session_write_close();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue