Skip to content
Browse files

Using tabs instead of spaces

  • Loading branch information...
1 parent 519283d commit a6bd4c4dc4eb467ccc93f10f6a637454e64f4df8 @mariano committed
View
166 extensions/adapter/security/auth/Form.php
@@ -2,8 +2,8 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_doctrine2\extensions\adapter\security\auth;
@@ -26,97 +26,97 @@
* {{{
* use lithium\security\Auth;
* Auth::config(array(
- * 'default' => array(
- * 'adapter' => 'li3_doctrine2\extensions\adapter\security\auth\Form',
- * 'model' => 'app\models\User',
- * 'fields' => array('email', 'password')
- * )
+ * 'default' => array(
+ * 'adapter' => 'li3_doctrine2\extensions\adapter\security\auth\Form',
+ * 'model' => 'app\models\User',
+ * 'fields' => array('email', 'password')
+ * )
* ));
* }}}
*/
class Form extends \lithium\security\auth\adapter\Form {
- /**
- * Repository for records
- *
- * @var object
- */
- protected $repository;
+ /**
+ * Repository for records
+ *
+ * @var object
+ */
+ protected $repository;
- /**
- * Sets the initial configuration for the `Form` adapter.
- *
- * @see lithium\security\auth\adapter\Form::$__construct
- * @param array $config Sets the configuration for the adapter, which has the following options:
- * - `'model'` _string_: The name of the model class to use. See the `$_model`
- * property for details.
- * - `'fields'` _array_: The model fields to query against when taking input from
- * the request data. See the `$_fields` property for details.
- * - `'scope'` _array_: Any additional conditions used to constrain the
- * authentication query. For example, if active accounts in an application have
- * an `active` field which must be set to `true`, you can specify
- * `'scope' => array('active' => true)`. See the `$_scope` property for more
- * details.
- * - `'filters'` _array_: Named callbacks to apply to request data before the user
- * lookup query is generated. See the `$_filters` property for more details.
- * - `'validators'` _array_: Named callbacks to apply to fields in request data and
- * corresponding fields in database data in order to do programmatic
- * authentication checks after the query has occurred. See the `$_validators`
- * property for more details.
- * - `'query'` _string_: Determines the model method to invoke for authentication
- * checks. See the `$_query` property for more details.
- */
- public function __construct(array $config = array()) {
- $config += array(
- 'model' => null,
- 'entityManager' => null,
- 'repositoryMethod' => 'findOneBy'
- );
- if (empty($config['model']) || !class_exists($config['model'])) {
- throw new ConfigException("No valid model \"{$config['model']}\" available to use for Form auth adapter");
- } elseif (empty($config['entityManager']) && (
- !method_exists($config['model'], 'getEntityManager') ||
- !is_callable($config['model'] . '::getEntityManager')
- )) {
- throw new ConfigException("The model {$config['model']} must define a getEntityManager() static method, or you must set the entityManager auth config variable");
- }
+ /**
+ * Sets the initial configuration for the `Form` adapter.
+ *
+ * @see lithium\security\auth\adapter\Form::$__construct
+ * @param array $config Sets the configuration for the adapter, which has the following options:
+ * - `'model'` _string_: The name of the model class to use. See the `$_model`
+ * property for details.
+ * - `'fields'` _array_: The model fields to query against when taking input from
+ * the request data. See the `$_fields` property for details.
+ * - `'scope'` _array_: Any additional conditions used to constrain the
+ * authentication query. For example, if active accounts in an application have
+ * an `active` field which must be set to `true`, you can specify
+ * `'scope' => array('active' => true)`. See the `$_scope` property for more
+ * details.
+ * - `'filters'` _array_: Named callbacks to apply to request data before the user
+ * lookup query is generated. See the `$_filters` property for more details.
+ * - `'validators'` _array_: Named callbacks to apply to fields in request data and
+ * corresponding fields in database data in order to do programmatic
+ * authentication checks after the query has occurred. See the `$_validators`
+ * property for more details.
+ * - `'query'` _string_: Determines the model method to invoke for authentication
+ * checks. See the `$_query` property for more details.
+ */
+ public function __construct(array $config = array()) {
+ $config += array(
+ 'model' => null,
+ 'entityManager' => null,
+ 'repositoryMethod' => 'findOneBy'
+ );
+ if (empty($config['model']) || !class_exists($config['model'])) {
+ throw new ConfigException("No valid model \"{$config['model']}\" available to use for Form auth adapter");
+ } elseif (empty($config['entityManager']) && (
+ !method_exists($config['model'], 'getEntityManager') ||
+ !is_callable($config['model'] . '::getEntityManager')
+ )) {
+ throw new ConfigException("The model {$config['model']} must define a getEntityManager() static method, or you must set the entityManager auth config variable");
+ }
- $reflection = new \ReflectionClass($config['model']);
- if (!$reflection->implementsInterface('li3_doctrine2\models\IUser')) {
- throw new ConfigException("The model {$config['model']} must implement IUser");
- }
+ $reflection = new \ReflectionClass($config['model']);
+ if (!$reflection->implementsInterface('li3_doctrine2\models\IUser')) {
+ throw new ConfigException("The model {$config['model']} must implement IUser");
+ }
- $entityManager = $config['entityManager'] ?:
- call_user_func($config['model'] . '::getEntityManager');
- if (!isset($entityManager) || !($entityManager instanceof EntityManager)) {
- throw new ConfigException('Not a valid entity manager');
- }
+ $entityManager = $config['entityManager'] ?:
+ call_user_func($config['model'] . '::getEntityManager');
+ if (!isset($entityManager) || !($entityManager instanceof EntityManager)) {
+ throw new ConfigException('Not a valid entity manager');
+ }
- $this->repository = $entityManager->getRepository($config['model']);
- parent::__construct($config);
- }
+ $this->repository = $entityManager->getRepository($config['model']);
+ parent::__construct($config);
+ }
- /**
- * Called by the `Auth` class to run an authentication check against a model class using the
- * credientials in a data container (a `Request` object), and returns an array of user
- * information on success, or `false` on failure.
- *
- * @param object $credentials A data container which wraps the authentication credentials used
- * to query the model (usually a `Request` object). See the documentation for this
- * class for further details.
- * @param array $options Additional configuration options. Not currently implemented in this
- * adapter.
- * @return array Returns an array containing user information on success, or `false` on failure.
- */
- public function check($credentials, array $options = array()) {
- $data = $this->_filters($credentials->data);
- if (count(array_filter($data)) === 0) {
- return false;
- }
- $conditions = $this->_scope + array_diff_key($data, $this->_validators);
+ /**
+ * Called by the `Auth` class to run an authentication check against a model class using the
+ * credientials in a data container (a `Request` object), and returns an array of user
+ * information on success, or `false` on failure.
+ *
+ * @param object $credentials A data container which wraps the authentication credentials used
+ * to query the model (usually a `Request` object). See the documentation for this
+ * class for further details.
+ * @param array $options Additional configuration options. Not currently implemented in this
+ * adapter.
+ * @return array Returns an array containing user information on success, or `false` on failure.
+ */
+ public function check($credentials, array $options = array()) {
+ $data = $this->_filters($credentials->data);
+ if (count(array_filter($data)) === 0) {
+ return false;
+ }
+ $conditions = $this->_scope + array_diff_key($data, $this->_validators);
- $user = call_user_func(array($this->repository, $this->_config['repositoryMethod']), $conditions);
- return $user ? $this->_validate($user, $data) : false;
- }
+ $user = call_user_func(array($this->repository, $this->_config['repositoryMethod']), $conditions);
+ return $user ? $this->_validate($user, $data) : false;
+ }
}
?>
View
688 extensions/adapter/session/Entity.php
@@ -2,8 +2,8 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_doctrine2\extensions\adapter\session;
@@ -25,9 +25,9 @@
*
* {{{
* /**
- * * @Entity
- * * @Table(name="sessions")
- * *\/
+ * * @Entity
+ * * @Table(name="sessions")
+ * *\/
* class Session extends \li3_doctrine2\models\BaseSession {
* }
* }}}
@@ -38,348 +38,348 @@
* {{{
* use lithium\storage\Session;
* Session::config(array(
- * 'default' => array(
- * 'adapter' => 'li3_doctrine2\extensions\adapter\session\Entity',
- * 'model' => 'app\models\Session',
- * )
+ * 'default' => array(
+ * 'adapter' => 'li3_doctrine2\extensions\adapter\session\Entity',
+ * 'model' => 'app\models\Session',
+ * )
* ));
* }}}
*/
class Entity {
- /**
- * Settings for this session adapter
- *
- * @var array
- */
- protected $config = array(
- 'start' => true,
- 'model' => null,
- 'entityManager' => null,
- 'expiration' => '+3 days',
- 'ini' => array(
- 'cookie_lifetime' => '0',
- 'cookie_httponly' => false,
- 'gc_divisor' => 100
- )
- );
-
- /**
- * Entity manager
- *
- * @var object
- */
- protected $entityManager;
-
- /**
- * Entity (record) instance
- *
- * @var object
- */
- protected $record;
-
- /**
- * Repository for records
- *
- * @var object
- */
- protected $repository;
-
- /**
- * Sets up the adapter with the configuration assigned by the `Session` class.
- *
- * @param array $config Available configuration options for this adapter:
- * - `'config'` _string_: The name of the model that this adapter should use.
- */
- public function __construct(array $config = array()) {
- $this->config = Set::merge($this->config, $config);
-
- if (empty($this->config['model']) || !class_exists($this->config['model'])) {
- throw new ConfigException("No valid model \"{$this->config['model']}\" available to use for Session interaction");
- } elseif (empty($this->config['entityManager']) && (
- !method_exists($this->config['model'], 'getEntityManager') ||
- !is_callable($this->config['model'] . '::getEntityManager')
- )) {
- throw new ConfigException("The session model {$this->config['model']} must define a getEntityManager() static method, or you must set the entityManager session config variable");
- }
-
- $reflection = new \ReflectionClass($this->config['model']);
- if (!$reflection->implementsInterface('li3_doctrine2\models\ISession')) {
- throw new ConfigException("The model {$this->config['model']} must implement ISession");
- }
-
- $this->entityManager = $this->config['entityManager'] ?:
- call_user_func($this->config['model'] . '::getEntityManager');
- if (!isset($this->entityManager) || !($this->entityManager instanceof EntityManager)) {
- throw new ConfigException('Not a valid entity manager');
- }
-
- $this->repository = $this->entityManager->getRepository($this->config['model']);
-
- foreach ($this->config['ini'] as $key => $config) {
- if (
- isset($this->config['ini'][$key]) &&
- ini_set("session.{$key}", $this->config['ini'][$key]) === false
- ) {
- throw new ConfigException("Could not initialize the session variable {$key}");
- }
- }
-
- session_set_save_handler(
- array(&$this, '_open'),
- array(&$this, '_close'),
- array(&$this, '_read'),
- array(&$this, '_write'),
- array(&$this, '_destroy'),
- array(&$this, '_gc')
- );
- register_shutdown_function('session_write_close');
-
- if ($this->config['start']) {
- $this->_startup();
- }
- }
-
- /**
- * Starts the session.
- *
- * @return boolean True if session successfully started (or has already been started),
- * false otherwise.
- */
- protected function _startup() {
- if (session_id()) {
- return true;
- }
- if (!isset($_SESSION)) {
- session_cache_limiter('nocache');
- }
- return session_start();
- }
-
- /**
- * Sets or obtains the session ID.
- *
- * @param string $key Optional. If specified, sets the session ID to the value of `$key`.
- * @return mixed Session ID, or `null` if the session has not been started.
- */
- public function key($key = null) {
- if ($key) {
- return session_id($key);
- }
- return session_id() ?: null;
- }
-
- /**
- * Obtain the status of the session.
- *
- * @return boolean True if $_SESSION is accessible and if a '_timestamp' key
- * has been set, false otherwise.
- */
- public function isStarted() {
- return (boolean) session_id();
- }
-
- /**
- * Checks if a value has been set in the session.
- *
- * @param string $key Key of the entry to be checked.
- * @param array $options Options array. Not used for this adapter method.
- * @return closure Function returning boolean `true` if the key exists, `false` otherwise.
- */
- public function check($key, array $options = array()) {
- if (!$this->isStarted() && !$this->_startup()) {
- throw new RuntimeException("Could not start session");
- }
- return function($self, $params) {
- return Set::check($_SESSION, $params['key']);
- };
- }
-
- /**
- * Read a value from the session.
- *
- * @param null|string $key Key of the entry to be read. If no key is passed, all
- * current session data is returned.
- * @param array $options Options array. Not used for this adapter method.
- * @return closure Function returning data in the session if successful, `false` otherwise.
- */
- public function read($key = null, array $options = array()) {
- if (!$this->isStarted() && !$this->_startup()) {
- throw new RuntimeException("Could not start session");
- }
-
- return function($self, $params) {
- $key = $params['key'];
- if (!$key) {
- return $_SESSION;
- }
- if (strpos($key, '.') === false) {
- return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
- }
- $filter = function($keys, $data) use (&$filter) {
- $key = array_shift($keys);
- if (isset($data[$key])) {
- return (empty($keys)) ?
- $data[$key] :
- $filter($keys, $data[$key]);
- }
- };
- return $filter(explode('.', $key), $_SESSION);
- };
- }
-
- /**
- * Write a value to the session.
- *
- * @param string $key Key of the item to be stored.
- * @param mixed $value The value to be stored.
- * @param array $options Options array. Not used for this adapter method.
- * @return closure Function returning boolean `true` on successful write, `false` otherwise.
- */
- public function write($key, $value, array $options = array()) {
- if (!$this->isStarted() && !$this->_startup()) {
- throw new RuntimeException("Could not start session");
- }
-
- $class = get_called_class();
- return function($self, $params, $chain) use ($class) {
- $_SESSION[$params['key']] = $params['value'];
- return array_key_exists($params['key'], $_SESSION);
- };
- }
-
- /**
- * Delete value from the session
- *
- * @param string $key The key to be deleted
- * @param array $options Options array. Not used for this adapter method.
- * @return closure Function returning boolean `true` if the key no longer exists
- * in the session, `false` otherwise
- */
- public function delete($key, array $options = array()) {
- if (!$this->isStarted() && !$this->_startup()) {
- throw new RuntimeException("Could not start session");
- }
- $class = get_called_class();
- return function($self, $params) use ($class) {
- if (array_key_exists($params['key'], $_SESSION)) {
- unset($_SESSION[$params['key']]);
- }
- return !array_key_exists($params['key'], $_SESSION);
- };
- }
-
- /**
- * Clears all keys from the session.
- *
- * @param array $options Options array. Not used fro this adapter method.
- * @return closure Function returning boolean `true` on successful clear, `false` otherwise.
- */
- public function clear(array $options = array()) {
- if (!$this->isStarted() && !$this->_startup()) {
- throw new RuntimeException("Could not start session");
- }
- return function($self, $params) {
- return session_destroy();
- };
- }
-
- /**
- * Called when opening the session - the equivalent of a 'session constructor'.
- * Creates & memoizes a Model record/document, on which future session operations will interact
- * to reduce the number of roundtrip operations on the persistent storage engine.
- *
- * @param string $path Not used for this adapter.
- * @param string $name Not used for this adapter.
- * @return void
- */
- public function _open($path, $name) {
- $id = $this->key();
- if (isset($this->record)) {
- $this->record = null;
- }
-
- if ($id) {
- $this->record = $this->repository->findOneById($id);
- }
-
- if (!$this->record) {
- $expires = new \DateTime();
- $expires->add(
- \DateInterval::createFromDateString($this->config['expiration'])
- );
-
- $this->record = new $this->config['model']();
- $this->record->setExpires($expires);
- }
- }
-
- /**
- * Closes the session.
- *
- * @return boolean Always returns true.
- */
- public function _close() {
- unset($this->record);
- return true;
- }
-
- /**
- * Session save handler callback for session destruction - called when session_destroy()
- * is invoked.
- *
- * @param string $id The session ID to be destroyed. This is not used explicitly - rather,
- * the memoized DB record object's delete() method is called.
- * @param return boolean True on successful destruction, false otherwise.
- */
- public function _destroy($id) {
- $this->entityManager->remove($this->record);
- $this->entityManager->flush();
- }
-
- /**
- * Delete all expired entries from the session.
- *
- * @param integer $lifetime Maximum valid session lifetime.
- * @return boolean True on successful garbage collect, false otherwise.
- */
- public function _gc($lifetime) {
- $expires = new \DateTime();
- $expires->sub(new \DateInterval("PT{$lifetime}S"));
-
- $this->entityManager
- ->createQuery("DELETE {$this->config['model']} e WHERE e.expires <= :expires")
- ->setParameter('expires', new \DateTime($lifetime))
- ->getResult();
- }
-
- /**
- * The session save handler callback for reading data from the session.
- *
- * @param string $key The key of the data to be returned. If no key is specified,
- * then all session data is returned in an array of key/value pairs.
- * @return mixed Value corresponding to key if set, null otherwise.
- */
- public function _read($id) {
- return $this->record->getData();
- }
-
- /**
- * The session save handler callback for writing data to the session.
- *
- * @param string $key The key of the data to be returned.
- * @param mixed $value The value to be written to the session.
- * @return boolean True if write was successful, false otherwise.
- */
- public function _write($id, $data) {
- $expires = new \DateTime();
- $expires->add(\DateInterval::createFromDateString($this->config['expiration']));
-
- $this->record->setId($id);
- $this->record->setData($data ?: null);
- $this->record->setExpires($expires);
-
- $this->entityManager->persist($this->record);
- $this->entityManager->flush($this->record);
- }
+ /**
+ * Settings for this session adapter
+ *
+ * @var array
+ */
+ protected $config = array(
+ 'start' => true,
+ 'model' => null,
+ 'entityManager' => null,
+ 'expiration' => '+3 days',
+ 'ini' => array(
+ 'cookie_lifetime' => '0',
+ 'cookie_httponly' => false,
+ 'gc_divisor' => 100
+ )
+ );
+
+ /**
+ * Entity manager
+ *
+ * @var object
+ */
+ protected $entityManager;
+
+ /**
+ * Entity (record) instance
+ *
+ * @var object
+ */
+ protected $record;
+
+ /**
+ * Repository for records
+ *
+ * @var object
+ */
+ protected $repository;
+
+ /**
+ * Sets up the adapter with the configuration assigned by the `Session` class.
+ *
+ * @param array $config Available configuration options for this adapter:
+ * - `'config'` _string_: The name of the model that this adapter should use.
+ */
+ public function __construct(array $config = array()) {
+ $this->config = Set::merge($this->config, $config);
+
+ if (empty($this->config['model']) || !class_exists($this->config['model'])) {
+ throw new ConfigException("No valid model \"{$this->config['model']}\" available to use for Session interaction");
+ } elseif (empty($this->config['entityManager']) && (
+ !method_exists($this->config['model'], 'getEntityManager') ||
+ !is_callable($this->config['model'] . '::getEntityManager')
+ )) {
+ throw new ConfigException("The session model {$this->config['model']} must define a getEntityManager() static method, or you must set the entityManager session config variable");
+ }
+
+ $reflection = new \ReflectionClass($this->config['model']);
+ if (!$reflection->implementsInterface('li3_doctrine2\models\ISession')) {
+ throw new ConfigException("The model {$this->config['model']} must implement ISession");
+ }
+
+ $this->entityManager = $this->config['entityManager'] ?:
+ call_user_func($this->config['model'] . '::getEntityManager');
+ if (!isset($this->entityManager) || !($this->entityManager instanceof EntityManager)) {
+ throw new ConfigException('Not a valid entity manager');
+ }
+
+ $this->repository = $this->entityManager->getRepository($this->config['model']);
+
+ foreach ($this->config['ini'] as $key => $config) {
+ if (
+ isset($this->config['ini'][$key]) &&
+ ini_set("session.{$key}", $this->config['ini'][$key]) === false
+ ) {
+ throw new ConfigException("Could not initialize the session variable {$key}");
+ }
+ }
+
+ session_set_save_handler(
+ array(&$this, '_open'),
+ array(&$this, '_close'),
+ array(&$this, '_read'),
+ array(&$this, '_write'),
+ array(&$this, '_destroy'),
+ array(&$this, '_gc')
+ );
+ register_shutdown_function('session_write_close');
+
+ if ($this->config['start']) {
+ $this->_startup();
+ }
+ }
+
+ /**
+ * Starts the session.
+ *
+ * @return boolean True if session successfully started (or has already been started),
+ * false otherwise.
+ */
+ protected function _startup() {
+ if (session_id()) {
+ return true;
+ }
+ if (!isset($_SESSION)) {
+ session_cache_limiter('nocache');
+ }
+ return session_start();
+ }
+
+ /**
+ * Sets or obtains the session ID.
+ *
+ * @param string $key Optional. If specified, sets the session ID to the value of `$key`.
+ * @return mixed Session ID, or `null` if the session has not been started.
+ */
+ public function key($key = null) {
+ if ($key) {
+ return session_id($key);
+ }
+ return session_id() ?: null;
+ }
+
+ /**
+ * Obtain the status of the session.
+ *
+ * @return boolean True if $_SESSION is accessible and if a '_timestamp' key
+ * has been set, false otherwise.
+ */
+ public function isStarted() {
+ return (boolean) session_id();
+ }
+
+ /**
+ * Checks if a value has been set in the session.
+ *
+ * @param string $key Key of the entry to be checked.
+ * @param array $options Options array. Not used for this adapter method.
+ * @return closure Function returning boolean `true` if the key exists, `false` otherwise.
+ */
+ public function check($key, array $options = array()) {
+ if (!$this->isStarted() && !$this->_startup()) {
+ throw new RuntimeException("Could not start session");
+ }
+ return function($self, $params) {
+ return Set::check($_SESSION, $params['key']);
+ };
+ }
+
+ /**
+ * Read a value from the session.
+ *
+ * @param null|string $key Key of the entry to be read. If no key is passed, all
+ * current session data is returned.
+ * @param array $options Options array. Not used for this adapter method.
+ * @return closure Function returning data in the session if successful, `false` otherwise.
+ */
+ public function read($key = null, array $options = array()) {
+ if (!$this->isStarted() && !$this->_startup()) {
+ throw new RuntimeException("Could not start session");
+ }
+
+ return function($self, $params) {
+ $key = $params['key'];
+ if (!$key) {
+ return $_SESSION;
+ }
+ if (strpos($key, '.') === false) {
+ return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
+ }
+ $filter = function($keys, $data) use (&$filter) {
+ $key = array_shift($keys);
+ if (isset($data[$key])) {
+ return (empty($keys)) ?
+ $data[$key] :
+ $filter($keys, $data[$key]);
+ }
+ };
+ return $filter(explode('.', $key), $_SESSION);
+ };
+ }
+
+ /**
+ * Write a value to the session.
+ *
+ * @param string $key Key of the item to be stored.
+ * @param mixed $value The value to be stored.
+ * @param array $options Options array. Not used for this adapter method.
+ * @return closure Function returning boolean `true` on successful write, `false` otherwise.
+ */
+ public function write($key, $value, array $options = array()) {
+ if (!$this->isStarted() && !$this->_startup()) {
+ throw new RuntimeException("Could not start session");
+ }
+
+ $class = get_called_class();
+ return function($self, $params, $chain) use ($class) {
+ $_SESSION[$params['key']] = $params['value'];
+ return array_key_exists($params['key'], $_SESSION);
+ };
+ }
+
+ /**
+ * Delete value from the session
+ *
+ * @param string $key The key to be deleted
+ * @param array $options Options array. Not used for this adapter method.
+ * @return closure Function returning boolean `true` if the key no longer exists
+ * in the session, `false` otherwise
+ */
+ public function delete($key, array $options = array()) {
+ if (!$this->isStarted() && !$this->_startup()) {
+ throw new RuntimeException("Could not start session");
+ }
+ $class = get_called_class();
+ return function($self, $params) use ($class) {
+ if (array_key_exists($params['key'], $_SESSION)) {
+ unset($_SESSION[$params['key']]);
+ }
+ return !array_key_exists($params['key'], $_SESSION);
+ };
+ }
+
+ /**
+ * Clears all keys from the session.
+ *
+ * @param array $options Options array. Not used fro this adapter method.
+ * @return closure Function returning boolean `true` on successful clear, `false` otherwise.
+ */
+ public function clear(array $options = array()) {
+ if (!$this->isStarted() && !$this->_startup()) {
+ throw new RuntimeException("Could not start session");
+ }
+ return function($self, $params) {
+ return session_destroy();
+ };
+ }
+
+ /**
+ * Called when opening the session - the equivalent of a 'session constructor'.
+ * Creates & memoizes a Model record/document, on which future session operations will interact
+ * to reduce the number of roundtrip operations on the persistent storage engine.
+ *
+ * @param string $path Not used for this adapter.
+ * @param string $name Not used for this adapter.
+ * @return void
+ */
+ public function _open($path, $name) {
+ $id = $this->key();
+ if (isset($this->record)) {
+ $this->record = null;
+ }
+
+ if ($id) {
+ $this->record = $this->repository->findOneById($id);
+ }
+
+ if (!$this->record) {
+ $expires = new \DateTime();
+ $expires->add(
+ \DateInterval::createFromDateString($this->config['expiration'])
+ );
+
+ $this->record = new $this->config['model']();
+ $this->record->setExpires($expires);
+ }
+ }
+
+ /**
+ * Closes the session.
+ *
+ * @return boolean Always returns true.
+ */
+ public function _close() {
+ unset($this->record);
+ return true;
+ }
+
+ /**
+ * Session save handler callback for session destruction - called when session_destroy()
+ * is invoked.
+ *
+ * @param string $id The session ID to be destroyed. This is not used explicitly - rather,
+ * the memoized DB record object's delete() method is called.
+ * @param return boolean True on successful destruction, false otherwise.
+ */
+ public function _destroy($id) {
+ $this->entityManager->remove($this->record);
+ $this->entityManager->flush();
+ }
+
+ /**
+ * Delete all expired entries from the session.
+ *
+ * @param integer $lifetime Maximum valid session lifetime.
+ * @return boolean True on successful garbage collect, false otherwise.
+ */
+ public function _gc($lifetime) {
+ $expires = new \DateTime();
+ $expires->sub(new \DateInterval("PT{$lifetime}S"));
+
+ $this->entityManager
+ ->createQuery("DELETE {$this->config['model']} e WHERE e.expires <= :expires")
+ ->setParameter('expires', new \DateTime($lifetime))
+ ->getResult();
+ }
+
+ /**
+ * The session save handler callback for reading data from the session.
+ *
+ * @param string $key The key of the data to be returned. If no key is specified,
+ * then all session data is returned in an array of key/value pairs.
+ * @return mixed Value corresponding to key if set, null otherwise.
+ */
+ public function _read($id) {
+ return $this->record->getData();
+ }
+
+ /**
+ * The session save handler callback for writing data to the session.
+ *
+ * @param string $key The key of the data to be returned.
+ * @param mixed $value The value to be written to the session.
+ * @return boolean True if write was successful, false otherwise.
+ */
+ public function _write($id, $data) {
+ $expires = new \DateTime();
+ $expires->add(\DateInterval::createFromDateString($this->config['expiration']));
+
+ $this->record->setId($id);
+ $this->record->setData($data ?: null);
+ $this->record->setExpires($expires);
+
+ $this->entityManager->persist($this->record);
+ $this->entityManager->flush($this->record);
+ }
}
?>
View
322 extensions/data/source/Doctrine.php
@@ -2,8 +2,8 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_doctrine2\extensions\data\source;
@@ -16,164 +16,164 @@
* This datasource provides integration of Doctrine2 models
*/
class Doctrine extends \lithium\core\Object {
- /**
- * Connection settings for Doctrine
- *
- * @var array
- */
- protected $connectionSettings;
-
- /**
- * Doctrine's configuration
- *
- * @var object
- */
- protected $configuration;
-
- /**
- * Doctrine's event manager
- *
- * @var object
- */
- protected $eventManager;
-
- /**
- * Doctrine's entity manager
- *
- * @var object
- */
- protected $entityManager;
-
- /**
- * Build data source
- *
- * @param array $config Configuration
- */
- public function __construct(array $config = array()) {
- $defaults = array(
- 'models' => LITHIUM_APP_PATH . '/models',
- 'proxies' => LITHIUM_APP_PATH . '/proxies',
- 'proxyNamespace' => 'proxies'
- );
- $this->connectionSettings = array_diff_key($config, array_merge(
- $defaults, array(
- 'type' => null,
- 'adapter' => null,
- 'login' => null,
- 'filters' => null
- )
- ));
- parent::__construct($config + $defaults);
- }
-
- /**
- * Initialize datasource
- */
- protected function _init() {
- $this->configuration = new \Doctrine\ORM\Configuration();
- $this->configuration->setProxyDir($this->_config['proxies']);
- $this->configuration->setProxyNamespace(
- $this->_config['proxyNamespace']
- );
- $this->configuration->setMetadataCacheImpl(
- new \Doctrine\Common\Cache\ArrayCache()
- );
-
- $annotationDriver = $this->configuration->newDefaultAnnotationDriver(
- (array) $this->_config['models']
- );
- $this->configuration->setMetadataDriverImpl($annotationDriver);
-
- $this->eventManager = new \Doctrine\Common\EventManager();
- $this->eventManager->addEventListener(array(
- Events::postLoad,
- Events::prePersist,
- Events::preUpdate
- ), $this);
- }
-
- /**
- * Create an entity manager
- *
- * @param array $params Parameters
- * @return object Entity manager
- * @filter
- */
- protected function createEntityManager() {
- $connection = $this->connectionSettings;
- $configuration = $this->configuration;
- $eventManager = $this->eventManager;
- $params = compact('connection', 'configuration', 'eventManager');
- return $this->_filter(__METHOD__, $params,
- function($self, $params) {
- return \Doctrine\ORM\EntityManager::create(
- $params['connection'],
- $params['configuration'],
- $params['eventManager']
- );
- }
- );
- }
-
- /**
- * Get the entity manager
- *
- * @return object Entity Manager
- */
- public function getEntityManager() {
- if (!isset($this->entityManager)) {
- $this->entityManager = $this->createEntityManager();
- }
- return $this->entityManager;
- }
-
- /**
- * Event executed after a record was loaded
- *
- * @param object $eventArgs Event arguments
- */
- public function postLoad(LifecycleEventArgs $eventArgs) {
- $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPostLoad',
- array($eventArgs)
- );
- }
-
- /**
- * Event executed before a record is to be created
- *
- * @param object $eventArgs Event arguments
- */
- public function prePersist(LifecycleEventArgs $eventArgs) {
- $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPrePersist',
- array($eventArgs)
- );
- }
-
- /**
- * Event executed before a record is to be updated
- *
- * @param object $eventArgs Event arguments
- */
- public function preUpdate(PreUpdateEventArgs $eventArgs) {
- $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPreUpdate',
- array($eventArgs)
- );
- }
-
- /**
- * Dispatch an entity event
- *
- * @param object $entity Entity on where to dispatch event
- * @param string $event Event name
- * @param array $args Event arguments
- */
- protected function dispatchEntityEvent($entity, $eventName, array $args) {
- if (
- method_exists($entity, $eventName) &&
- is_callable(array($entity, $eventName))
- ) {
- call_user_func_array(array($entity, $eventName), $args);
- }
- }
+ /**
+ * Connection settings for Doctrine
+ *
+ * @var array
+ */
+ protected $connectionSettings;
+
+ /**
+ * Doctrine's configuration
+ *
+ * @var object
+ */
+ protected $configuration;
+
+ /**
+ * Doctrine's event manager
+ *
+ * @var object
+ */
+ protected $eventManager;
+
+ /**
+ * Doctrine's entity manager
+ *
+ * @var object
+ */
+ protected $entityManager;
+
+ /**
+ * Build data source
+ *
+ * @param array $config Configuration
+ */
+ public function __construct(array $config = array()) {
+ $defaults = array(
+ 'models' => LITHIUM_APP_PATH . '/models',
+ 'proxies' => LITHIUM_APP_PATH . '/proxies',
+ 'proxyNamespace' => 'proxies'
+ );
+ $this->connectionSettings = array_diff_key($config, array_merge(
+ $defaults, array(
+ 'type' => null,
+ 'adapter' => null,
+ 'login' => null,
+ 'filters' => null
+ )
+ ));
+ parent::__construct($config + $defaults);
+ }
+
+ /**
+ * Initialize datasource
+ */
+ protected function _init() {
+ $this->configuration = new \Doctrine\ORM\Configuration();
+ $this->configuration->setProxyDir($this->_config['proxies']);
+ $this->configuration->setProxyNamespace(
+ $this->_config['proxyNamespace']
+ );
+ $this->configuration->setMetadataCacheImpl(
+ new \Doctrine\Common\Cache\ArrayCache()
+ );
+
+ $annotationDriver = $this->configuration->newDefaultAnnotationDriver(
+ (array) $this->_config['models']
+ );
+ $this->configuration->setMetadataDriverImpl($annotationDriver);
+
+ $this->eventManager = new \Doctrine\Common\EventManager();
+ $this->eventManager->addEventListener(array(
+ Events::postLoad,
+ Events::prePersist,
+ Events::preUpdate
+ ), $this);
+ }
+
+ /**
+ * Create an entity manager
+ *
+ * @param array $params Parameters
+ * @return object Entity manager
+ * @filter
+ */
+ protected function createEntityManager() {
+ $connection = $this->connectionSettings;
+ $configuration = $this->configuration;
+ $eventManager = $this->eventManager;
+ $params = compact('connection', 'configuration', 'eventManager');
+ return $this->_filter(__METHOD__, $params,
+ function($self, $params) {
+ return \Doctrine\ORM\EntityManager::create(
+ $params['connection'],
+ $params['configuration'],
+ $params['eventManager']
+ );
+ }
+ );
+ }
+
+ /**
+ * Get the entity manager
+ *
+ * @return object Entity Manager
+ */
+ public function getEntityManager() {
+ if (!isset($this->entityManager)) {
+ $this->entityManager = $this->createEntityManager();
+ }
+ return $this->entityManager;
+ }
+
+ /**
+ * Event executed after a record was loaded
+ *
+ * @param object $eventArgs Event arguments
+ */
+ public function postLoad(LifecycleEventArgs $eventArgs) {
+ $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPostLoad',
+ array($eventArgs)
+ );
+ }
+
+ /**
+ * Event executed before a record is to be created
+ *
+ * @param object $eventArgs Event arguments
+ */
+ public function prePersist(LifecycleEventArgs $eventArgs) {
+ $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPrePersist',
+ array($eventArgs)
+ );
+ }
+
+ /**
+ * Event executed before a record is to be updated
+ *
+ * @param object $eventArgs Event arguments
+ */
+ public function preUpdate(PreUpdateEventArgs $eventArgs) {
+ $this->dispatchEntityEvent($eventArgs->getEntity(), 'onPreUpdate',
+ array($eventArgs)
+ );
+ }
+
+ /**
+ * Dispatch an entity event
+ *
+ * @param object $entity Entity on where to dispatch event
+ * @param string $event Event name
+ * @param array $args Event arguments
+ */
+ protected function dispatchEntityEvent($entity, $eventName, array $args) {
+ if (
+ method_exists($entity, $eventName) &&
+ is_callable(array($entity, $eventName))
+ ) {
+ call_user_func_array(array($entity, $eventName), $args);
+ }
+ }
}
?>
View
418 models/BaseEntity.php
@@ -2,8 +2,8 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_doctrine2\models;
@@ -18,214 +18,214 @@
* for lithium validation to work on doctrine models.
*/
abstract class BaseEntity extends \lithium\data\Entity implements IModel, IUser {
- /**
- * Criteria for data validation.
- *
- * Example usage:
- * {{{
- * public $validates = array(
- * 'title' => 'please enter a title',
- * 'email' => array(
- * array('notEmpty', 'message' => 'Email is empty.'),
- * array('email', 'message' => 'Email is not valid.'),
- * )
- * );
- * }}}
- *
- * @var array
- */
- protected $validates = array();
-
- /**
- * Class dependencies.
- *
- * @var array
- */
- protected static $_classes = array(
- 'connections' => 'lithium\data\Connections',
- 'validator' => 'lithium\util\Validator'
- );
-
- /**
- * Connection name used for persisting / loading this record
- *
- * @var string
- */
- protected static $connectionName = 'default';
-
- /**
- * Constructor, instance at which the entity fields are loaded from
- * doctrine's class metadata
- */
- public function __construct() {
- $this->_model = get_called_class();
- }
-
- /**
- * Get the entity manager linked to the connection defined in the property
- * `$connectionName`
- *
- * @see IModel::getEntityManager()
- * @return EntityManager entity manager
- */
- public static function getEntityManager() {
- static $entityManager;
- if (!isset($entityManager)) {
- $connections = static::$_classes['connections'];
- $entityManager = $connections::get(static::$connectionName)->getEntityManager();
- }
- return $entityManager;
- }
-
- /**
- * Get the repository for this model
- *
- * @see IModel::getRepository()
- * @return EntityRepository entity repository
- */
- public static function getRepository() {
- return static::getEntityManager()->getRepository(get_called_class());
- }
-
- /**
- * Doctrine callback executed after a record was loaded
- *
- * @param object $eventArgs Event arguments
- */
- public function onPostLoad(LifecycleEventArgs $eventArgs) {
- $this->_exists = true;
- }
-
- /**
- * Doctrine callback executed before persisting a new record
- *
- * @param object $eventArgs Event arguments
- * @throws ValidateException
- */
- public function onPrePersist(LifecycleEventArgs $eventArgs) {
- $this->_exists = false;
- if (!$this->validates()) {
- throw new ValidateException($this->_errors);
- }
- }
-
- /**
- * Doctrine callback executed before persisting an existing record
- *
- * @param object $eventArgs Event arguments
- * @throws ValidateException
- */
- public function onPreUpdate(PreUpdateEventArgs $eventArgs) {
- $this->_exists = true;
- if (!$this->validates()) {
- throw new ValidateException($this->_errors);
- }
- }
-
- /**
- * Perform validation
- *
- * @see IModel::validates()
- * @param array $options Options
- * @return boolean Success
- */
- public function validates(array $options = array()) {
- $defaults = array(
- 'rules' => $this->validates,
- 'events' => $this->exists() ? 'update' : 'create',
- 'model' => get_called_class()
- );
- $options += $defaults;
- $validator = static::$_classes['validator'];
-
- $rules = $options['rules'];
- unset($options['rules']);
-
- if (!empty($rules) && $this->_errors = $validator::check($this->_getData(true), $rules, $options)) {
- return false;
- }
- return true;
- }
+ /**
+ * Criteria for data validation.
+ *
+ * Example usage:
+ * {{{
+ * public $validates = array(
+ * 'title' => 'please enter a title',
+ * 'email' => array(
+ * array('notEmpty', 'message' => 'Email is empty.'),
+ * array('email', 'message' => 'Email is not valid.'),
+ * )
+ * );
+ * }}}
+ *
+ * @var array
+ */
+ protected $validates = array();
+
+ /**
+ * Class dependencies.
+ *
+ * @var array
+ */
+ protected static $_classes = array(
+ 'connections' => 'lithium\data\Connections',
+ 'validator' => 'lithium\util\Validator'
+ );
+
+ /**
+ * Connection name used for persisting / loading this record
+ *
+ * @var string
+ */
+ protected static $connectionName = 'default';
+
+ /**
+ * Constructor, instance at which the entity fields are loaded from
+ * doctrine's class metadata
+ */
+ public function __construct() {
+ $this->_model = get_called_class();
+ }
+
+ /**
+ * Get the entity manager linked to the connection defined in the property
+ * `$connectionName`
+ *
+ * @see IModel::getEntityManager()
+ * @return EntityManager entity manager
+ */
+ public static function getEntityManager() {
+ static $entityManager;
+ if (!isset($entityManager)) {
+ $connections = static::$_classes['connections'];
+ $entityManager = $connections::get(static::$connectionName)->getEntityManager();
+ }
+ return $entityManager;
+ }
+
+ /**
+ * Get the repository for this model
+ *
+ * @see IModel::getRepository()
+ * @return EntityRepository entity repository
+ */
+ public static function getRepository() {
+ return static::getEntityManager()->getRepository(get_called_class());
+ }
+
+ /**
+ * Doctrine callback executed after a record was loaded
+ *
+ * @param object $eventArgs Event arguments
+ */
+ public function onPostLoad(LifecycleEventArgs $eventArgs) {
+ $this->_exists = true;
+ }
+
+ /**
+ * Doctrine callback executed before persisting a new record
+ *
+ * @param object $eventArgs Event arguments
+ * @throws ValidateException
+ */
+ public function onPrePersist(LifecycleEventArgs $eventArgs) {
+ $this->_exists = false;
+ if (!$this->validates()) {
+ throw new ValidateException($this->_errors);
+ }
+ }
+
+ /**
+ * Doctrine callback executed before persisting an existing record
+ *
+ * @param object $eventArgs Event arguments
+ * @throws ValidateException
+ */
+ public function onPreUpdate(PreUpdateEventArgs $eventArgs) {
+ $this->_exists = true;
+ if (!$this->validates()) {
+ throw new ValidateException($this->_errors);
+ }
+ }
+
+ /**
+ * Perform validation
+ *
+ * @see IModel::validates()
+ * @param array $options Options
+ * @return boolean Success
+ */
+ public function validates(array $options = array()) {
+ $defaults = array(
+ 'rules' => $this->validates,
+ 'events' => $this->exists() ? 'update' : 'create',
+ 'model' => get_called_class()
+ );
+ $options += $defaults;
+ $validator = static::$_classes['validator'];
+
+ $rules = $options['rules'];
+ unset($options['rules']);
+
+ if (!empty($rules) && $this->_errors = $validator::check($this->_getData(true), $rules, $options)) {
+ return false;
+ }
+ return true;
+ }
/**
- * Allows several properties to be assigned at once, i.e.:
- * {{{
- * $record->set(array('title' => 'Lorem Ipsum', 'value' => 42));
- * }}}
- *
- * @see IModel::validates()
- * @param array $data An associative array of fields and values to assign to this instance.
- */
- public function set(array $data) {
- if (empty($data)) {
- return;
- }
-
- foreach($data as $field => $value) {
- if (!is_string($field) || !in_array($field, $this->_getEntityFields())) {
- continue;
- }
- $method = 'set' . Inflector::camelize($field);
- if (method_exists($this, $method) && is_callable(array($this, $method))) {
- $this->{$method}($value);
- } else if (property_exists($this, $field)) {
- $this->{$field} = $value;
- }
- }
- }
-
- /**
- * Access the data fields of the record. Can also access a $named field.
- * Only returns data for fields that have a getter method defined.
- *
- * @see IModel::validates()
- * @param string $name Optionally included field name.
- * @return mixed Entire data array if $name is empty, otherwise the value from the named field.
- */
- public function data($name = null) {
- $data = $this->_getData();
- if (isset($name)) {
- return array_key_exists($name, $data) ? $data[$name] : null;
- }
- return $data;
- }
-
- /**
- * Get the entity fields
- *
- * @return array
- */
- protected function _getEntityFields() {
- static $entityFields;
- if (!isset($entityFields)) {
- $entityFields = array_values(static::getEntityManager()->getClassMetadata(get_called_class())->fieldNames);
- if (empty($entityFields)) {
- throw new \Exception($class . ' does not seem to have fields defined');
- }
- }
- return $entityFields;
- }
-
- /**
- * Get record data as an array
- *
- * @param bool $allProperties If true, get also properties without getter methods
- * @return array Data
- */
- protected function _getData($allProperties = false) {
- $data = array();
- foreach($this->_getEntityFields() as $field) {
- $method = 'get' . Inflector::camelize($field);
- if (method_exists($this, $method) && is_callable(array($this, $method))) {
- $data[$field] = $this->{$method}();
- } elseif ($allProperties && property_exists($this, $field)) {
- $data[$field] = $this->{$field};
- }
- }
- if (isset($name)) {
- return array_key_exists($name, $data) ? $data[$name] : null;
- }
- return $data;
- }
+ * Allows several properties to be assigned at once, i.e.:
+ * {{{
+ * $record->set(array('title' => 'Lorem Ipsum', 'value' => 42));
+ * }}}
+ *
+ * @see IModel::validates()
+ * @param array $data An associative array of fields and values to assign to this instance.
+ */
+ public function set(array $data) {
+ if (empty($data)) {
+ return;
+ }
+
+ foreach($data as $field => $value) {
+ if (!is_string($field) || !in_array($field, $this->_getEntityFields())) {
+ continue;
+ }
+ $method = 'set' . Inflector::camelize($field);
+ if (method_exists($this, $method) && is_callable(array($this, $method))) {
+ $this->{$method}($value);
+ } else if (property_exists($this, $field)) {
+ $this->{$field} = $value;
+ }
+ }
+ }
+
+ /**
+ * Access the data fields of the record. Can also access a $named field.
+ * Only returns data for fields that have a getter method defined.
+ *
+ * @see IModel::validates()
+ * @param string $name Optionally included field name.
+ * @return mixed Entire data array if $name is empty, otherwise the value from the named field.
+ */
+ public function data($name = null) {
+ $data = $this->_getData();
+ if (isset($name)) {
+ return array_key_exists($name, $data) ? $data[$name] : null;
+ }
+ return $data;
+ }
+
+ /**
+ * Get the entity fields
+ *
+ * @return array
+ */
+ protected function _getEntityFields() {
+ static $entityFields;
+ if (!isset($entityFields)) {
+ $entityFields = array_values(static::getEntityManager()->getClassMetadata(get_called_class())->fieldNames);
+ if (empty($entityFields)) {
+ throw new \Exception($class . ' does not seem to have fields defined');
+ }
+ }
+ return $entityFields;
+ }
+
+ /**
+ * Get record data as an array
+ *
+ * @param bool $allProperties If true, get also properties without getter methods
+ * @return array Data
+ */
+ protected function _getData($allProperties = false) {
+ $data = array();
+ foreach($this->_getEntityFields() as $field) {
+ $method = 'get' . Inflector::camelize($field);
+ if (method_exists($this, $method) && is_callable(array($this, $method))) {
+ $data[$field] = $this->{$method}();
+ } elseif ($allProperties && property_exists($this, $field)) {
+ $data[$field] = $this->{$field};
+ }
+ }
+ if (isset($name)) {
+ return array_key_exists($name, $data) ? $data[$name] : null;
+ }
+ return $data;
+ }
}
?>
View
152 models/BaseSession.php
@@ -2,8 +2,8 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_doctrine2\models;
@@ -19,88 +19,88 @@
* can extend from this class to provide your own session model.
*/
abstract class BaseSession implements ISession {
- /**
- * Class dependencies.
- *
- * @var array
- */
- protected static $_classes = array(
- 'connections' => 'lithium\data\Connections'
- );
+ /**
+ * Class dependencies.
+ *
+ * @var array
+ */
+ protected static $_classes = array(
+ 'connections' => 'lithium\data\Connections'
+ );
- /**
- * Connection name used for persisting / loading this record
- *
- * @var string
- */
- protected static $connectionName = 'default';
+ /**
+ * Connection name used for persisting / loading this record
+ *
+ * @var string
+ */
+ protected static $connectionName = 'default';
- /**
- * @Id
- * @Column(type="string")
- */
- protected $id;
+ /**
+ * @Id
+ * @Column(type="string")
+ */
+ protected $id;
- /**
- * @Column(type="text",nullable=true)
- */
- protected $data;
+ /**
+ * @Column(type="text",nullable=true)
+ */
+ protected $data;
- /**
- * @Column(type="datetime")
- */
- protected $expires;
+ /**
+ * @Column(type="datetime")
+ */
+ protected $expires;
- /**
- * Get the entity manager linked to the connection defined in the property
- * `$connectionName`
- *
- * @see IModel::getEntityManager()
- * @return EntityManager entity manager
- */
- public static function getEntityManager() {
- static $entityManager;
- if (!isset($entityManager)) {
- $connections = static::$_classes['connections'];
- $entityManager = $connections::get(static::$connectionName)->getEntityManager();
- }
- return $entityManager;
- }
+ /**
+ * Get the entity manager linked to the connection defined in the property
+ * `$connectionName`
+ *
+ * @see IModel::getEntityManager()
+ * @return EntityManager entity manager
+ */
+ public static function getEntityManager() {
+ static $entityManager;
+ if (!isset($entityManager)) {
+ $connections = static::$_classes['connections'];
+ $entityManager = $connections::get(static::$connectionName)->getEntityManager();
+ }
+ return $entityManager;
+ }
- /**
- * Set ID
- *
- * @param string $id ID
- */
- public function setId($id) {
- $this->id = $id;
- }
+ /**
+ * Set ID
+ *
+ * @param string $id ID
+ */
+ public function setId($id) {
+ $this->id = $id;
+ }
- /**
- * Get data
- *
- * @return string
- */
- public function getData() {
- return $this->data;
- }
+ /**
+ * Get data
+ *
+ * @return string
+ */
+ public function getData() {
+ return $this->data;
+ }
- /**
- * Set data
- *
- * @param string $data Data
- */
- public function setData($data) {
- $this->data = $data;
- }
+ /**
+ * Set data
+ *
+ * @param string $data Data
+ */
+ public function setData($data) {
+ $this->data = $data;
+ }
- /**
- * Set expiration date
- *
- * @param DateTime $expires Expiration date
- */
- public function setExpires(\DateTime $expires) {
- $this->expires = $expires;
- }
+ /**
+ * Set expiration date
+ *
+ * @param DateTime $expires Expiration date
+ */
+ public function setExpires(\DateTime $expires) {
+ $this->expires = $expires;
+ }
}
?>
View
128 models/IModel.php
@@ -2,8 +2,8 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_doctrine2\models;
@@ -13,75 +13,75 @@
* to be able to use Lithium's validation & form system.
*/
interface IModel {
- /**
- * Get the entity manager for this model
- *
- * @return EntityManager entity manager
- */
- public static function getEntityManager();
+ /**
+ * Get the entity manager for this model
+ *
+ * @return EntityManager entity manager
+ */
+ public static function getEntityManager();
- /**
- * Get the repository for this model
- *
- * @return EntityRepository entity repository
- */
- public static function getRepository();
+ /**
+ * Get the repository for this model
+ *
+ * @return EntityRepository entity repository
+ */
+ public static function getRepository();
- /**
- * Returns the model which this entity is bound to.
- * Usually this would be the fully qualified class name this method belongs
- * to.
- *
- * @return string The fully qualified model class name.
- */
- public function model();
+ /**
+ * Returns the model which this entity is bound to.
+ * Usually this would be the fully qualified class name this method belongs
+ * to.
+ *
+ * @return string The fully qualified model class name.
+ */
+ public function model();
- /**
- * A flag indicating whether or not this record exists.
- *
- * @return boolean `True` if the record was `read` from the data-source, or has been `create`d
- * and `save`d. Otherwise `false`.
- */
- public function exists();
+ /**
+ * A flag indicating whether or not this record exists.
+ *
+ * @return boolean `True` if the record was `read` from the data-source, or has been `create`d
+ * and `save`d. Otherwise `false`.
+ */
+ public function exists();
/**
- * Allows several properties to be assigned at once, i.e.:
- * {{{
- * $record->set(array('title' => 'Lorem Ipsum', 'value' => 42));
- * }}}
- *
- * @param array $data An associative array of fields and values to assign to this instance.
- */
- public function set(array $data);
+ * Allows several properties to be assigned at once, i.e.:
+ * {{{
+ * $record->set(array('title' => 'Lorem Ipsum', 'value' => 42));
+ * }}}
+ *
+ * @param array $data An associative array of fields and values to assign to this instance.
+ */
+ public function set(array $data);
- /**
- * Access the data fields of the record. Can also access a $named field.
- * Only returns data for fields that have a getter method defined.
- *
- * @param string $name Optionally included field name.
- * @return mixed Entire data array if $name is empty, otherwise the value from the named field.
- */
- public function data($name = null);
+ /**
+ * Access the data fields of the record. Can also access a $named field.
+ * Only returns data for fields that have a getter method defined.
+ *
+ * @param string $name Optionally included field name.
+ * @return mixed Entire data array if $name is empty, otherwise the value from the named field.
+ */
+ public function data($name = null);
- /**
- * Perform validation
- *
- * @see lithium\data\Model::validates()
- * @param array $options Options
- * @return boolean Returns `true` if all validation rules on all fields succeed, otherwise
- * `false`. After validation, the messages for any validation failures
- * are accessible through the `errors()` method.
- */
- public function validates(array $options = array());
+ /**
+ * Perform validation
+ *
+ * @see lithium\data\Model::validates()
+ * @param array $options Options
+ * @return boolean Returns `true` if all validation rules on all fields succeed, otherwise
+ * `false`. After validation, the messages for any validation failures
+ * are accessible through the `errors()` method.
+ */
+ public function validates(array $options = array());
- /**
- * Access the errors of the record.
- *
- * @param array|string $field If an array, overwrites `$this->_errors`. If a string, and
- * `$value` is not `null`, sets the corresponding key in `$this->_errors` to `$value`.
- * @param string $value Value to set.
- * @return mixed Either the `$this->_errors` array, or single value from it.
- */
- public function errors($field = null, $value = null);
+ /**
+ * Access the errors of the record.
+ *
+ * @param array|string $field If an array, overwrites `$this->_errors`. If a string, and
+ * `$value` is not `null`, sets the corresponding key in `$this->_errors` to `$value`.
+ * @param string $value Value to set.
+ * @return mixed Either the `$this->_errors` array, or single value from it.
+ */
+ public function errors($field = null, $value = null);
}
?>
View
52 models/ISession.php
@@ -2,8 +2,8 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_doctrine2\models;
@@ -13,32 +13,32 @@
* to be able to be used as the model for li3_doctrine2's session adapter.
*/
interface ISession {
- /**
- * Set ID
- *
- * @param string $id ID
- */
- public function setId($id);
+ /**
+ * Set ID
+ *
+ * @param string $id ID
+ */
+ public function setId($id);
- /**
- * Get data
- *
- * @return string
- */
- public function getData();
+ /**
+ * Get data
+ *
+ * @return string
+ */
+ public function getData();
- /**
- * Set data
- *
- * @param string $data Data
- */
- public function setData($data);
+ /**
+ * Set data
+ *
+ * @param string $data Data
+ */
+ public function setData($data);
- /**
- * Set expiration date
- *
- * @param DateTime $expires Expiration date
- */
- public function setExpires(\DateTime $expires);
+ /**
+ * Set expiration date
+ *
+ * @param DateTime $expires Expiration date
+ */
+ public function setExpires(\DateTime $expires);
}
?>
View
20 models/IUser.php
@@ -2,8 +2,8 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ * @copyright Copyright 2012, Mariano Iglesias (http://marianoiglesias.com.ar)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace li3_doctrine2\models;
@@ -13,13 +13,13 @@
* to be able to be used as the model for the Form auth session adapter.
*/
interface IUser {
- /**
- * Access the data fields of the record. Can also access a $named field.
- * Only returns data for fields that have a getter method defined.
- *
- * @param string $name Optionally included field name.
- * @return mixed Entire data array if $name is empty, otherwise the value from the named field.
- */