MDL-20632 crippling the Zend framework, only stuff needed for our /webservice/* kept :-(
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,64 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Acl
|
||||
*/
|
||||
require_once 'Zend/Acl.php';
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Acl_Role_Interface
|
||||
*/
|
||||
require_once 'Zend/Acl/Role/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Acl_Resource_Interface
|
||||
*/
|
||||
require_once 'Zend/Acl/Resource/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Acl_Assert_Interface
|
||||
{
|
||||
/**
|
||||
* Returns true if and only if the assertion conditions are met
|
||||
*
|
||||
* This method is passed the ACL, Role, Resource, and privilege to which the authorization query applies. If the
|
||||
* $role, $resource, or $privilege parameters are null, it means that the query applies to all Roles, Resources, or
|
||||
* privileges, respectively.
|
||||
*
|
||||
* @param Zend_Acl $acl
|
||||
* @param Zend_Acl_Role_Interface $role
|
||||
* @param Zend_Acl_Resource_Interface $resource
|
||||
* @param string $privilege
|
||||
* @return boolean
|
||||
*/
|
||||
public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $role = null, Zend_Acl_Resource_Interface $resource = null,
|
||||
$privilege = null);
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Exception
|
||||
*/
|
||||
require_once 'Zend/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Acl_Exception extends Zend_Exception
|
||||
{}
|
||||
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Acl_Resource_Interface
|
||||
*/
|
||||
require_once 'Zend/Acl/Resource/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Acl_Resource implements Zend_Acl_Resource_Interface
|
||||
{
|
||||
/**
|
||||
* Unique id of Resource
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_resourceId;
|
||||
|
||||
/**
|
||||
* Sets the Resource identifier
|
||||
*
|
||||
* @param string $resourceId
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($resourceId)
|
||||
{
|
||||
$this->_resourceId = (string) $resourceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Acl_Resource_Interface; returns the Resource identifier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getResourceId()
|
||||
{
|
||||
return $this->_resourceId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Acl_Resource_Interface
|
||||
{
|
||||
/**
|
||||
* Returns the string identifier of the Resource
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getResourceId();
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Acl_Role_Interface
|
||||
*/
|
||||
require_once 'Zend/Acl/Role/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Acl_Role implements Zend_Acl_Role_Interface
|
||||
{
|
||||
/**
|
||||
* Unique id of Role
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_roleId;
|
||||
|
||||
/**
|
||||
* Sets the Role identifier
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($roleId)
|
||||
{
|
||||
$this->_roleId = (string) $roleId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Acl_Role_Interface; returns the Role identifier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRoleId()
|
||||
{
|
||||
return $this->_roleId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Acl_Role_Interface
|
||||
{
|
||||
/**
|
||||
* Returns the string identifier of the Role
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRoleId();
|
||||
}
|
||||
@@ -1,271 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Acl_Role_Interface
|
||||
*/
|
||||
require_once 'Zend/Acl/Role/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Acl_Role_Registry
|
||||
{
|
||||
/**
|
||||
* Internal Role registry data storage
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_roles = array();
|
||||
|
||||
/**
|
||||
* Adds a Role having an identifier unique to the registry
|
||||
*
|
||||
* The $parents parameter may be a reference to, or the string identifier for,
|
||||
* a Role existing in the registry, or $parents may be passed as an array of
|
||||
* these - mixing string identifiers and objects is ok - to indicate the Roles
|
||||
* from which the newly added Role will directly inherit.
|
||||
*
|
||||
* In order to resolve potential ambiguities with conflicting rules inherited
|
||||
* from different parents, the most recently added parent takes precedence over
|
||||
* parents that were previously added. In other words, the first parent added
|
||||
* will have the least priority, and the last parent added will have the
|
||||
* highest priority.
|
||||
*
|
||||
* @param Zend_Acl_Role_Interface $role
|
||||
* @param Zend_Acl_Role_Interface|string|array $parents
|
||||
* @throws Zend_Acl_Role_Registry_Exception
|
||||
* @return Zend_Acl_Role_Registry Provides a fluent interface
|
||||
*/
|
||||
public function add(Zend_Acl_Role_Interface $role, $parents = null)
|
||||
{
|
||||
$roleId = $role->getRoleId();
|
||||
|
||||
if ($this->has($roleId)) {
|
||||
/**
|
||||
* @see Zend_Acl_Role_Registry_Exception
|
||||
*/
|
||||
require_once 'Zend/Acl/Role/Registry/Exception.php';
|
||||
throw new Zend_Acl_Role_Registry_Exception("Role id '$roleId' already exists in the registry");
|
||||
}
|
||||
|
||||
$roleParents = array();
|
||||
|
||||
if (null !== $parents) {
|
||||
if (!is_array($parents)) {
|
||||
$parents = array($parents);
|
||||
}
|
||||
/**
|
||||
* @see Zend_Acl_Role_Registry_Exception
|
||||
*/
|
||||
require_once 'Zend/Acl/Role/Registry/Exception.php';
|
||||
foreach ($parents as $parent) {
|
||||
try {
|
||||
if ($parent instanceof Zend_Acl_Role_Interface) {
|
||||
$roleParentId = $parent->getRoleId();
|
||||
} else {
|
||||
$roleParentId = $parent;
|
||||
}
|
||||
$roleParent = $this->get($roleParentId);
|
||||
} catch (Zend_Acl_Role_Registry_Exception $e) {
|
||||
throw new Zend_Acl_Role_Registry_Exception("Parent Role id '$roleParentId' does not exist");
|
||||
}
|
||||
$roleParents[$roleParentId] = $roleParent;
|
||||
$this->_roles[$roleParentId]['children'][$roleId] = $role;
|
||||
}
|
||||
}
|
||||
|
||||
$this->_roles[$roleId] = array(
|
||||
'instance' => $role,
|
||||
'parents' => $roleParents,
|
||||
'children' => array()
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the identified Role
|
||||
*
|
||||
* The $role parameter can either be a Role or a Role identifier.
|
||||
*
|
||||
* @param Zend_Acl_Role_Interface|string $role
|
||||
* @throws Zend_Acl_Role_Registry_Exception
|
||||
* @return Zend_Acl_Role_Interface
|
||||
*/
|
||||
public function get($role)
|
||||
{
|
||||
if ($role instanceof Zend_Acl_Role_Interface) {
|
||||
$roleId = $role->getRoleId();
|
||||
} else {
|
||||
$roleId = (string) $role;
|
||||
}
|
||||
|
||||
if (!$this->has($role)) {
|
||||
/**
|
||||
* @see Zend_Acl_Role_Registry_Exception
|
||||
*/
|
||||
require_once 'Zend/Acl/Role/Registry/Exception.php';
|
||||
throw new Zend_Acl_Role_Registry_Exception("Role '$roleId' not found");
|
||||
}
|
||||
|
||||
return $this->_roles[$roleId]['instance'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if and only if the Role exists in the registry
|
||||
*
|
||||
* The $role parameter can either be a Role or a Role identifier.
|
||||
*
|
||||
* @param Zend_Acl_Role_Interface|string $role
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($role)
|
||||
{
|
||||
if ($role instanceof Zend_Acl_Role_Interface) {
|
||||
$roleId = $role->getRoleId();
|
||||
} else {
|
||||
$roleId = (string) $role;
|
||||
}
|
||||
|
||||
return isset($this->_roles[$roleId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of an existing Role's parents
|
||||
*
|
||||
* The array keys are the identifiers of the parent Roles, and the values are
|
||||
* the parent Role instances. The parent Roles are ordered in this array by
|
||||
* ascending priority. The highest priority parent Role, last in the array,
|
||||
* corresponds with the parent Role most recently added.
|
||||
*
|
||||
* If the Role does not have any parents, then an empty array is returned.
|
||||
*
|
||||
* @param Zend_Acl_Role_Interface|string $role
|
||||
* @uses Zend_Acl_Role_Registry::get()
|
||||
* @return array
|
||||
*/
|
||||
public function getParents($role)
|
||||
{
|
||||
$roleId = $this->get($role)->getRoleId();
|
||||
|
||||
return $this->_roles[$roleId]['parents'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if and only if $role inherits from $inherit
|
||||
*
|
||||
* Both parameters may be either a Role or a Role identifier. If
|
||||
* $onlyParents is true, then $role must inherit directly from
|
||||
* $inherit in order to return true. By default, this method looks
|
||||
* through the entire inheritance DAG to determine whether $role
|
||||
* inherits from $inherit through its ancestor Roles.
|
||||
*
|
||||
* @param Zend_Acl_Role_Interface|string $role
|
||||
* @param Zend_Acl_Role_Interface|string $inherit
|
||||
* @param boolean $onlyParents
|
||||
* @throws Zend_Acl_Role_Registry_Exception
|
||||
* @return boolean
|
||||
*/
|
||||
public function inherits($role, $inherit, $onlyParents = false)
|
||||
{
|
||||
/**
|
||||
* @see Zend_Acl_Role_Registry_Exception
|
||||
*/
|
||||
require_once 'Zend/Acl/Role/Registry/Exception.php';
|
||||
try {
|
||||
$roleId = $this->get($role)->getRoleId();
|
||||
$inheritId = $this->get($inherit)->getRoleId();
|
||||
} catch (Zend_Acl_Role_Registry_Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$inherits = isset($this->_roles[$roleId]['parents'][$inheritId]);
|
||||
|
||||
if ($inherits || $onlyParents) {
|
||||
return $inherits;
|
||||
}
|
||||
|
||||
foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
|
||||
if ($this->inherits($parentId, $inheritId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the Role from the registry
|
||||
*
|
||||
* The $role parameter can either be a Role or a Role identifier.
|
||||
*
|
||||
* @param Zend_Acl_Role_Interface|string $role
|
||||
* @throws Zend_Acl_Role_Registry_Exception
|
||||
* @return Zend_Acl_Role_Registry Provides a fluent interface
|
||||
*/
|
||||
public function remove($role)
|
||||
{
|
||||
/**
|
||||
* @see Zend_Acl_Role_Registry_Exception
|
||||
*/
|
||||
require_once 'Zend/Acl/Role/Registry/Exception.php';
|
||||
try {
|
||||
$roleId = $this->get($role)->getRoleId();
|
||||
} catch (Zend_Acl_Role_Registry_Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
foreach ($this->_roles[$roleId]['children'] as $childId => $child) {
|
||||
unset($this->_roles[$childId]['parents'][$roleId]);
|
||||
}
|
||||
foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
|
||||
unset($this->_roles[$parentId]['children'][$roleId]);
|
||||
}
|
||||
|
||||
unset($this->_roles[$roleId]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all Roles from the registry
|
||||
*
|
||||
* @return Zend_Acl_Role_Registry Provides a fluent interface
|
||||
*/
|
||||
public function removeAll()
|
||||
{
|
||||
$this->_roles = array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRoles()
|
||||
{
|
||||
return $this->_roles;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Acl_Exception
|
||||
*/
|
||||
require_once 'Zend/Acl/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Acl
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Acl_Role_Registry_Exception extends Zend_Acl_Exception
|
||||
{}
|
||||
@@ -1,397 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application
|
||||
{
|
||||
/**
|
||||
* Autoloader to use
|
||||
*
|
||||
* @var Zend_Loader_Autoloader
|
||||
*/
|
||||
protected $_autoloader;
|
||||
|
||||
/**
|
||||
* Bootstrap
|
||||
*
|
||||
* @var Zend_Application_Bootstrap_BootstrapAbstract
|
||||
*/
|
||||
protected $_bootstrap;
|
||||
|
||||
/**
|
||||
* Application environment
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_environment;
|
||||
|
||||
/**
|
||||
* Flattened (lowercase) option keys
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_optionKeys = array();
|
||||
|
||||
/**
|
||||
* Options for Zend_Application
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_options = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Initialize application. Potentially initializes include_paths, PHP
|
||||
* settings, and bootstrap class.
|
||||
*
|
||||
* @param string $environment
|
||||
* @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
|
||||
* @throws Zend_Application_Exception When invalid options are provided
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($environment, $options = null)
|
||||
{
|
||||
$this->_environment = (string) $environment;
|
||||
|
||||
require_once 'Zend/Loader/Autoloader.php';
|
||||
$this->_autoloader = Zend_Loader_Autoloader::getInstance();
|
||||
|
||||
if (null !== $options) {
|
||||
if (is_string($options)) {
|
||||
$options = $this->_loadConfig($options);
|
||||
} elseif ($options instanceof Zend_Config) {
|
||||
$options = $options->toArray();
|
||||
} elseif (!is_array($options)) {
|
||||
throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
|
||||
}
|
||||
|
||||
$this->setOptions($options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve current environment
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvironment()
|
||||
{
|
||||
return $this->_environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve autoloader instance
|
||||
*
|
||||
* @return Zend_Loader_Autoloader
|
||||
*/
|
||||
public function getAutoloader()
|
||||
{
|
||||
return $this->_autoloader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set application options
|
||||
*
|
||||
* @param array $options
|
||||
* @throws Zend_Application_Exception When no bootstrap path is provided
|
||||
* @throws Zend_Application_Exception When invalid bootstrap information are provided
|
||||
* @return Zend_Application
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
if (!empty($options['config'])) {
|
||||
$options = $this->mergeOptions($options, $this->_loadConfig($options['config']));
|
||||
}
|
||||
|
||||
$this->_options = $options;
|
||||
|
||||
$options = array_change_key_case($options, CASE_LOWER);
|
||||
|
||||
$this->_optionKeys = array_keys($options);
|
||||
|
||||
if (!empty($options['phpsettings'])) {
|
||||
$this->setPhpSettings($options['phpsettings']);
|
||||
}
|
||||
|
||||
if (!empty($options['includepaths'])) {
|
||||
$this->setIncludePaths($options['includepaths']);
|
||||
}
|
||||
|
||||
if (!empty($options['autoloadernamespaces'])) {
|
||||
$this->setAutoloaderNamespaces($options['autoloadernamespaces']);
|
||||
}
|
||||
|
||||
if (!empty($options['autoloaderzfpath'])) {
|
||||
$autoloader = $this->getAutoloader();
|
||||
if (method_exists($autoloader, 'setZfPath')) {
|
||||
$zfPath = $options['autoloaderzfpath'];
|
||||
$zfVersion = !empty($options['autoloaderzfversion'])
|
||||
? $options['autoloaderzfversion']
|
||||
: 'latest';
|
||||
$autoloader->setZfPath($zfPath, $zfVersion);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($options['bootstrap'])) {
|
||||
$bootstrap = $options['bootstrap'];
|
||||
|
||||
if (is_string($bootstrap)) {
|
||||
$this->setBootstrap($bootstrap);
|
||||
} elseif (is_array($bootstrap)) {
|
||||
if (empty($bootstrap['path'])) {
|
||||
throw new Zend_Application_Exception('No bootstrap path provided');
|
||||
}
|
||||
|
||||
$path = $bootstrap['path'];
|
||||
$class = null;
|
||||
|
||||
if (!empty($bootstrap['class'])) {
|
||||
$class = $bootstrap['class'];
|
||||
}
|
||||
|
||||
$this->setBootstrap($path, $class);
|
||||
} else {
|
||||
throw new Zend_Application_Exception('Invalid bootstrap information provided');
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve application options (for caching)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is an option present?
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function hasOption($key)
|
||||
{
|
||||
return in_array(strtolower($key), $this->_optionKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a single option
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOption($key)
|
||||
{
|
||||
if ($this->hasOption($key)) {
|
||||
$options = $this->getOptions();
|
||||
$options = array_change_key_case($options, CASE_LOWER);
|
||||
return $options[strtolower($key)];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge options recursively
|
||||
*
|
||||
* @param array $array1
|
||||
* @param mixed $array2
|
||||
* @return array
|
||||
*/
|
||||
public function mergeOptions(array $array1, $array2 = null)
|
||||
{
|
||||
if (is_array($array2)) {
|
||||
foreach ($array2 as $key => $val) {
|
||||
if (is_array($array2[$key])) {
|
||||
$array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
|
||||
? $this->mergeOptions($array1[$key], $array2[$key])
|
||||
: $array2[$key];
|
||||
} else {
|
||||
$array1[$key] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $array1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set PHP configuration settings
|
||||
*
|
||||
* @param array $settings
|
||||
* @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)
|
||||
* @return Zend_Application
|
||||
*/
|
||||
public function setPhpSettings(array $settings, $prefix = '')
|
||||
{
|
||||
foreach ($settings as $key => $value) {
|
||||
$key = empty($prefix) ? $key : $prefix . $key;
|
||||
if (is_scalar($value)) {
|
||||
ini_set($key, $value);
|
||||
} elseif (is_array($value)) {
|
||||
$this->setPhpSettings($value, $key . '.');
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set include path
|
||||
*
|
||||
* @param array $paths
|
||||
* @return Zend_Application
|
||||
*/
|
||||
public function setIncludePaths(array $paths)
|
||||
{
|
||||
$path = implode(PATH_SEPARATOR, $paths);
|
||||
set_include_path($path . PATH_SEPARATOR . get_include_path());
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set autoloader namespaces
|
||||
*
|
||||
* @param array $namespaces
|
||||
* @return Zend_Application
|
||||
*/
|
||||
public function setAutoloaderNamespaces(array $namespaces)
|
||||
{
|
||||
$autoloader = $this->getAutoloader();
|
||||
|
||||
foreach ($namespaces as $namespace) {
|
||||
$autoloader->registerNamespace($namespace);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set bootstrap path/class
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $class
|
||||
* @return Zend_Application
|
||||
*/
|
||||
public function setBootstrap($path, $class = null)
|
||||
{
|
||||
// setOptions() can potentially send a null value; specify default
|
||||
// here
|
||||
if (null === $class) {
|
||||
$class = 'Bootstrap';
|
||||
}
|
||||
|
||||
if (!class_exists($class, false)) {
|
||||
require_once $path;
|
||||
if (!class_exists($class, false)) {
|
||||
throw new Zend_Application_Exception('Bootstrap class not found');
|
||||
}
|
||||
}
|
||||
$this->_bootstrap = new $class($this);
|
||||
|
||||
if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
|
||||
throw new Zend_Application_Exception('Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bootstrap object
|
||||
*
|
||||
* @return Zend_Application_Bootstrap_BootstrapAbstract
|
||||
*/
|
||||
public function getBootstrap()
|
||||
{
|
||||
if (null === $this->_bootstrap) {
|
||||
$this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
|
||||
}
|
||||
return $this->_bootstrap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap application
|
||||
*
|
||||
* @param null|string|array $resource
|
||||
* @return Zend_Application
|
||||
*/
|
||||
public function bootstrap($resource = null)
|
||||
{
|
||||
$this->getBootstrap()->bootstrap($resource);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the application
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$this->getBootstrap()->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load configuration file of options
|
||||
*
|
||||
* @param string $file
|
||||
* @throws Zend_Application_Exception When invalid configuration file is provided
|
||||
* @return array
|
||||
*/
|
||||
protected function _loadConfig($file)
|
||||
{
|
||||
$environment = $this->getEnvironment();
|
||||
$suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
||||
|
||||
switch ($suffix) {
|
||||
case 'ini':
|
||||
$config = new Zend_Config_Ini($file, $environment);
|
||||
break;
|
||||
|
||||
case 'xml':
|
||||
$config = new Zend_Config_Xml($file, $environment);
|
||||
break;
|
||||
|
||||
case 'php':
|
||||
case 'inc':
|
||||
$config = include $file;
|
||||
if (!is_array($config)) {
|
||||
throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');
|
||||
}
|
||||
return $config;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');
|
||||
}
|
||||
|
||||
return $config->toArray();
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Bootstrap
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Concrete base class for bootstrap classes
|
||||
*
|
||||
* Registers and utilizes Zend_Controller_Front by default.
|
||||
*
|
||||
* @uses Zend_Application_Bootstrap_Bootstrap
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Bootstrap
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Bootstrap_Bootstrap
|
||||
extends Zend_Application_Bootstrap_BootstrapAbstract
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Ensure FrontController resource is registered
|
||||
*
|
||||
* @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($application)
|
||||
{
|
||||
parent::__construct($application);
|
||||
if (!$this->hasPluginResource('FrontController')) {
|
||||
$this->registerPluginResource('FrontController');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the application
|
||||
*
|
||||
* Checks to see that we have a default controller directory. If not, an
|
||||
* exception is thrown.
|
||||
*
|
||||
* If so, it registers the bootstrap with the 'bootstrap' parameter of
|
||||
* the front controller, and dispatches the front controller.
|
||||
*
|
||||
* @return void
|
||||
* @throws Zend_Application_Bootstrap_Exception
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$front = $this->getResource('FrontController');
|
||||
$default = $front->getDefaultModule();
|
||||
if (null === $front->getControllerDirectory($default)) {
|
||||
throw new Zend_Application_Bootstrap_Exception(
|
||||
'No default controller directory registered with front controller'
|
||||
);
|
||||
}
|
||||
|
||||
$front->setParam('bootstrap', $this);
|
||||
$front->dispatch();
|
||||
}
|
||||
}
|
||||
@@ -1,764 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Bootstrap
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract base class for bootstrap classes
|
||||
*
|
||||
* @uses Zend_Application_Bootstrap_Bootstrapper
|
||||
* @uses Zend_Application_Bootstrap_ResourceBootstrapper
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Bootstrap
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Application_Bootstrap_BootstrapAbstract
|
||||
implements Zend_Application_Bootstrap_Bootstrapper,
|
||||
Zend_Application_Bootstrap_ResourceBootstrapper
|
||||
{
|
||||
/**
|
||||
* @var Zend_Application|Zend_Application_Bootstrap_Bootstrapper
|
||||
*/
|
||||
protected $_application;
|
||||
|
||||
/**
|
||||
* @var array Internal resource methods (resource/method pairs)
|
||||
*/
|
||||
protected $_classResources;
|
||||
|
||||
/**
|
||||
* @var object Resource container
|
||||
*/
|
||||
protected $_container;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_environment;
|
||||
|
||||
/**
|
||||
* Flattened (lowercase) option keys used for lookups
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_optionKeys = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_options = array();
|
||||
|
||||
/**
|
||||
* @var Zend_Loader_PluginLoader_Interface
|
||||
*/
|
||||
protected $_pluginLoader;
|
||||
|
||||
/**
|
||||
* @var array Class-based resource plugins
|
||||
*/
|
||||
protected $_pluginResources = array();
|
||||
|
||||
/**
|
||||
* @var array Initializers that have been run
|
||||
*/
|
||||
protected $_run = array();
|
||||
|
||||
/**
|
||||
* @var array Initializers that have been started but not yet completed (circular dependency detection)
|
||||
*/
|
||||
protected $_started = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Sets application object, initializes options, and prepares list of
|
||||
* initializer methods.
|
||||
*
|
||||
* @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
|
||||
* @return void
|
||||
* @throws Zend_Application_Bootstrap_Exception When invalid applicaiton is provided
|
||||
*/
|
||||
public function __construct($application)
|
||||
{
|
||||
$this->setApplication($application);
|
||||
$options = $application->getOptions();
|
||||
$this->setOptions($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set class state
|
||||
*
|
||||
* @param array $options
|
||||
* @return Zend_Application_Bootstrap_BootstrapAbstract
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->_options = $this->mergeOptions($this->_options, $options);
|
||||
|
||||
$options = array_change_key_case($options, CASE_LOWER);
|
||||
$this->_optionKeys = array_merge($this->_optionKeys, array_keys($options));
|
||||
|
||||
$methods = get_class_methods($this);
|
||||
foreach ($methods as $key => $method) {
|
||||
$methods[$key] = strtolower($method);
|
||||
}
|
||||
|
||||
if (array_key_exists('pluginpaths', $options)) {
|
||||
$pluginLoader = $this->getPluginLoader();
|
||||
|
||||
foreach ($options['pluginpaths'] as $prefix => $path) {
|
||||
$pluginLoader->addPrefixPath($prefix, $path);
|
||||
}
|
||||
unset($options['pluginpaths']);
|
||||
}
|
||||
|
||||
foreach ($options as $key => $value) {
|
||||
$method = 'set' . strtolower($key);
|
||||
|
||||
if (in_array($method, $methods)) {
|
||||
$this->$method($value);
|
||||
} elseif ('resources' == $key) {
|
||||
foreach ($value as $resource => $resourceOptions) {
|
||||
$this->registerPluginResource($resource, $resourceOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current options from bootstrap
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is an option present?
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function hasOption($key)
|
||||
{
|
||||
return in_array($key, $this->_optionKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a single option
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOption($key)
|
||||
{
|
||||
if ($this->hasOption($key)) {
|
||||
$options = $this->getOptions();
|
||||
$options = array_change_key_case($options, CASE_LOWER);
|
||||
return $options[strtolower($key)];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge options recursively
|
||||
*
|
||||
* @param array $array1
|
||||
* @param mixed $array2
|
||||
* @return array
|
||||
*/
|
||||
public function mergeOptions(array $array1, $array2 = null)
|
||||
{
|
||||
if (is_array($array2)) {
|
||||
foreach ($array2 as $key => $val) {
|
||||
if (is_array($array2[$key])) {
|
||||
$array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
|
||||
? $this->mergeOptions($array1[$key], $array2[$key])
|
||||
: $array2[$key];
|
||||
} else {
|
||||
$array1[$key] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $array1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get class resources (as resource/method pairs)
|
||||
*
|
||||
* Uses get_class_methods() by default, reflection on prior to 5.2.6,
|
||||
* as a bug prevents the usage of get_class_methods() there.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getClassResources()
|
||||
{
|
||||
if (null === $this->_classResources) {
|
||||
if (version_compare(PHP_VERSION, '5.2.6') === -1) {
|
||||
$class = new ReflectionObject($this);
|
||||
$classMethods = $class->getMethods();
|
||||
$methodNames = array();
|
||||
|
||||
foreach ($classMethods as $method) {
|
||||
$methodNames[] = $method->getName();
|
||||
}
|
||||
} else {
|
||||
$methodNames = get_class_methods($this);
|
||||
}
|
||||
|
||||
$this->_classResources = array();
|
||||
foreach ($methodNames as $method) {
|
||||
if (5 < strlen($method) && '_init' === substr($method, 0, 5)) {
|
||||
$this->_classResources[strtolower(substr($method, 5))] = $method;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_classResources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get class resource names
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getClassResourceNames()
|
||||
{
|
||||
$resources = $this->getClassResources();
|
||||
return array_keys($resources);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new resource plugin
|
||||
*
|
||||
* @param string|Zend_Application_Resource_Resource $resource
|
||||
* @param mixed $options
|
||||
* @return Zend_Application_Bootstrap_BootstrapAbstract
|
||||
* @throws Zend_Application_Bootstrap_Exception When invalid resource is provided
|
||||
*/
|
||||
public function registerPluginResource($resource, $options = null)
|
||||
{
|
||||
if ($resource instanceof Zend_Application_Resource_Resource) {
|
||||
$resource->setBootstrap($this);
|
||||
$pluginName = $this->_resolvePluginResourceName($resource);
|
||||
$this->_pluginResources[$pluginName] = $resource;
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (!is_string($resource)) {
|
||||
throw new Zend_Application_Bootstrap_Exception('Invalid resource provided to ' . __METHOD__);
|
||||
}
|
||||
|
||||
$this->_pluginResources[$resource] = $options;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a resource from the bootstrap
|
||||
*
|
||||
* @param string|Zend_Application_Resource_Resource $resource
|
||||
* @return Zend_Application_Bootstrap_BootstrapAbstract
|
||||
* @throws Zend_Application_Bootstrap_Exception When unknown resource type is provided
|
||||
*/
|
||||
public function unregisterPluginResource($resource)
|
||||
{
|
||||
if ($resource instanceof Zend_Application_Resource_Resource) {
|
||||
if ($index = array_search($resource, $this->_pluginResources, true)) {
|
||||
unset($this->_pluginResources[$index]);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (!is_string($resource)) {
|
||||
throw new Zend_Application_Bootstrap_Exception('Unknown resource type provided to ' . __METHOD__);
|
||||
}
|
||||
|
||||
$resource = strtolower($resource);
|
||||
if (array_key_exists($resource, $this->_pluginResources)) {
|
||||
unset($this->_pluginResources[$resource]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the requested plugin resource registered?
|
||||
*
|
||||
* @param string $resource
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPluginResource($resource)
|
||||
{
|
||||
return (null !== $this->getPluginResource($resource));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a registered plugin resource
|
||||
*
|
||||
* @param string $resourceName
|
||||
* @return Zend_Application_Resource_Resource
|
||||
*/
|
||||
public function getPluginResource($resource)
|
||||
{
|
||||
if (array_key_exists(strtolower($resource), $this->_pluginResources)) {
|
||||
$resource = strtolower($resource);
|
||||
if (!$this->_pluginResources[$resource] instanceof Zend_Application_Resource_Resource) {
|
||||
$resourceName = $this->_loadPluginResource($resource, $this->_pluginResources[$resource]);
|
||||
if (!$resourceName) {
|
||||
throw new Zend_Application_Bootstrap_Exception(sprintf('Unable to resolve plugin "%s"; no corresponding plugin with that name', $resource));
|
||||
}
|
||||
$resource = $resourceName;
|
||||
}
|
||||
return $this->_pluginResources[$resource];
|
||||
}
|
||||
|
||||
foreach ($this->_pluginResources as $plugin => $spec) {
|
||||
if ($spec instanceof Zend_Application_Resource_Resource) {
|
||||
$pluginName = $this->_resolvePluginResourceName($spec);
|
||||
if (0 === strcasecmp($resource, $pluginName)) {
|
||||
unset($this->_pluginResources[$plugin]);
|
||||
$this->_pluginResources[$pluginName] = $spec;
|
||||
return $spec;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (false !== $pluginName = $this->_loadPluginResource($plugin, $spec)) {
|
||||
if (0 === strcasecmp($resource, $pluginName)) {
|
||||
return $this->_pluginResources[$pluginName];
|
||||
}
|
||||
}
|
||||
|
||||
if (class_exists($plugin)) {
|
||||
$spec = (array) $spec;
|
||||
$spec['bootstrap'] = $this;
|
||||
$instance = new $plugin($spec);
|
||||
$pluginName = $this->_resolvePluginResourceName($instance);
|
||||
unset($this->_pluginResources[$plugin]);
|
||||
$this->_pluginResources[$pluginName] = $instance;
|
||||
|
||||
if (0 === strcasecmp($resource, $pluginName)) {
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all plugin resources
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPluginResources()
|
||||
{
|
||||
foreach (array_keys($this->_pluginResources) as $resource) {
|
||||
$this->getPluginResource($resource);
|
||||
}
|
||||
return $this->_pluginResources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve plugin resource names
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPluginResourceNames()
|
||||
{
|
||||
$this->getPluginResources();
|
||||
return array_keys($this->_pluginResources);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set plugin loader for loading resources
|
||||
*
|
||||
* @param Zend_Loader_PluginLoader_Interface $loader
|
||||
* @return Zend_Application_Bootstrap_BootstrapAbstract
|
||||
*/
|
||||
public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
|
||||
{
|
||||
$this->_pluginLoader = $loader;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin loader for resources
|
||||
*
|
||||
* @return Zend_Loader_PluginLoader_Interface
|
||||
*/
|
||||
public function getPluginLoader()
|
||||
{
|
||||
if ($this->_pluginLoader === null) {
|
||||
$options = array(
|
||||
'Zend_Application_Resource' => 'Zend/Application/Resource'
|
||||
);
|
||||
|
||||
$this->_pluginLoader = new Zend_Loader_PluginLoader($options);
|
||||
}
|
||||
|
||||
return $this->_pluginLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set application/parent bootstrap
|
||||
*
|
||||
* @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
|
||||
* @return Zend_Application_Bootstrap_BootstrapAbstract
|
||||
*/
|
||||
public function setApplication($application)
|
||||
{
|
||||
if (($application instanceof Zend_Application)
|
||||
|| ($application instanceof Zend_Application_Bootstrap_Bootstrapper)
|
||||
) {
|
||||
$this->_application = $application;
|
||||
} else {
|
||||
throw new Zend_Application_Bootstrap_Exception('Invalid application provided to bootstrap constructor (received "' . get_class($application) . '" instance)');
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve parent application instance
|
||||
*
|
||||
* @return Zend_Application|Zend_Application_Bootstrap_Bootstrapper
|
||||
*/
|
||||
public function getApplication()
|
||||
{
|
||||
return $this->_application;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve application environment
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvironment()
|
||||
{
|
||||
if (null === $this->_environment) {
|
||||
$this->_environment = $this->getApplication()->getEnvironment();
|
||||
}
|
||||
return $this->_environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set resource container
|
||||
*
|
||||
* By default, if a resource callback has a non-null return value, this
|
||||
* value will be stored in a container using the resource name as the
|
||||
* key.
|
||||
*
|
||||
* Containers must be objects, and must allow setting public properties.
|
||||
*
|
||||
* @param object $container
|
||||
* @return Zend_Application_Bootstrap_BootstrapAbstract
|
||||
*/
|
||||
public function setContainer($container)
|
||||
{
|
||||
if (!is_object($container)) {
|
||||
throw new Zend_Application_Bootstrap_Exception('Resource containers must be objects');
|
||||
}
|
||||
$this->_container = $container;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve resource container
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function getContainer()
|
||||
{
|
||||
if (null === $this->_container) {
|
||||
$this->setContainer(new Zend_Registry());
|
||||
}
|
||||
return $this->_container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a resource has been stored in the container
|
||||
*
|
||||
* During bootstrap resource initialization, you may return a value. If
|
||||
* you do, it will be stored in the {@link setContainer() container}.
|
||||
* You can use this method to determine if a value was stored.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasResource($name)
|
||||
{
|
||||
$resource = strtolower($name);
|
||||
$container = $this->getContainer();
|
||||
return isset($container->{$resource});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a resource from the container
|
||||
*
|
||||
* During bootstrap resource initialization, you may return a value. If
|
||||
* you do, it will be stored in the {@link setContainer() container}.
|
||||
* You can use this method to retrieve that value.
|
||||
*
|
||||
* If no value was returned, this will return a null value.
|
||||
*
|
||||
* @param string $name
|
||||
* @return null|mixed
|
||||
*/
|
||||
public function getResource($name)
|
||||
{
|
||||
$resource = strtolower($name);
|
||||
$container = $this->getContainer();
|
||||
if ($this->hasResource($resource)) {
|
||||
return $container->{$resource};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement PHP's magic to retrieve a ressource
|
||||
* in the bootstrap
|
||||
*
|
||||
* @param string $prop
|
||||
* @return null|mixed
|
||||
*/
|
||||
public function __get($prop)
|
||||
{
|
||||
return $this->getResource($prop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement PHP's magic to ask for the
|
||||
* existence of a ressource in the bootstrap
|
||||
*
|
||||
* @param string $prop
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($prop)
|
||||
{
|
||||
return $this->hasResource($prop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap individual, all, or multiple resources
|
||||
*
|
||||
* Marked as final to prevent issues when subclassing and naming the
|
||||
* child class 'Bootstrap' (in which case, overriding this method
|
||||
* would result in it being treated as a constructor).
|
||||
*
|
||||
* If you need to override this functionality, override the
|
||||
* {@link _bootstrap()} method.
|
||||
*
|
||||
* @param null|string|array $resource
|
||||
* @return Zend_Application_Bootstrap_BootstrapAbstract
|
||||
* @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
|
||||
*/
|
||||
final public function bootstrap($resource = null)
|
||||
{
|
||||
$this->_bootstrap($resource);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overloading: intercept calls to bootstrap<resourcename>() methods
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $args
|
||||
* @return void
|
||||
* @throws Zend_Application_Bootstrap_Exception On invalid method name
|
||||
*/
|
||||
public function __call($method, $args)
|
||||
{
|
||||
if (9 < strlen($method) && 'bootstrap' === substr($method, 0, 9)) {
|
||||
$resource = substr($method, 9);
|
||||
return $this->bootstrap($resource);
|
||||
}
|
||||
|
||||
throw new Zend_Application_Bootstrap_Exception('Invalid method "' . $method . '"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap implementation
|
||||
*
|
||||
* This method may be overridden to provide custom bootstrapping logic.
|
||||
* It is the sole method called by {@link bootstrap()}.
|
||||
*
|
||||
* @param null|string|array $resource
|
||||
* @return void
|
||||
* @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
|
||||
*/
|
||||
protected function _bootstrap($resource = null)
|
||||
{
|
||||
if (null === $resource) {
|
||||
foreach ($this->getClassResourceNames() as $resource) {
|
||||
$this->_executeResource($resource);
|
||||
}
|
||||
|
||||
foreach ($this->getPluginResourceNames() as $resource) {
|
||||
$this->_executeResource($resource);
|
||||
}
|
||||
} elseif (is_string($resource)) {
|
||||
$this->_executeResource($resource);
|
||||
} elseif (is_array($resource)) {
|
||||
foreach ($resource as $r) {
|
||||
$this->_executeResource($r);
|
||||
}
|
||||
} else {
|
||||
throw new Zend_Application_Bootstrap_Exception('Invalid argument passed to ' . __METHOD__);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a resource
|
||||
*
|
||||
* Checks to see if the resource has already been run. If not, it searches
|
||||
* first to see if a local method matches the resource, and executes that.
|
||||
* If not, it checks to see if a plugin resource matches, and executes that
|
||||
* if found.
|
||||
*
|
||||
* Finally, if not found, it throws an exception.
|
||||
*
|
||||
* @param string $resource
|
||||
* @return void
|
||||
* @throws Zend_Application_Bootstrap_Exception When resource not found
|
||||
*/
|
||||
protected function _executeResource($resource)
|
||||
{
|
||||
$resourceName = strtolower($resource);
|
||||
|
||||
if (in_array($resourceName, $this->_run)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($this->_started[$resourceName]) && $this->_started[$resourceName]) {
|
||||
throw new Zend_Application_Bootstrap_Exception('Circular resource dependency detected');
|
||||
}
|
||||
|
||||
$classResources = $this->getClassResources();
|
||||
if (array_key_exists($resourceName, $classResources)) {
|
||||
$this->_started[$resourceName] = true;
|
||||
$method = $classResources[$resourceName];
|
||||
$return = $this->$method();
|
||||
unset($this->_started[$resourceName]);
|
||||
$this->_markRun($resourceName);
|
||||
|
||||
if (null !== $return) {
|
||||
$this->getContainer()->{$resourceName} = $return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->hasPluginResource($resource)) {
|
||||
$this->_started[$resourceName] = true;
|
||||
$plugin = $this->getPluginResource($resource);
|
||||
$return = $plugin->init();
|
||||
unset($this->_started[$resourceName]);
|
||||
$this->_markRun($resourceName);
|
||||
|
||||
if (null !== $return) {
|
||||
$this->getContainer()->{$resourceName} = $return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Zend_Application_Bootstrap_Exception('Resource matching "' . $resource . '" not found');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a plugin resource
|
||||
*
|
||||
* @param string $resource
|
||||
* @param array|object|null $options
|
||||
* @return string|false
|
||||
*/
|
||||
protected function _loadPluginResource($resource, $options)
|
||||
{
|
||||
$options = (array) $options;
|
||||
$options['bootstrap'] = $this;
|
||||
$className = $this->getPluginLoader()->load(strtolower($resource), false);
|
||||
|
||||
if (!$className) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$instance = new $className($options);
|
||||
|
||||
unset($this->_pluginResources[$resource]);
|
||||
|
||||
if (isset($instance->_explicitType)) {
|
||||
$resource = $instance->_explicitType;
|
||||
}
|
||||
$resource = strtolower($resource);
|
||||
$this->_pluginResources[$resource] = $instance;
|
||||
|
||||
return $resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a resource as having run
|
||||
*
|
||||
* @param string $resource
|
||||
* @return void
|
||||
*/
|
||||
protected function _markRun($resource)
|
||||
{
|
||||
if (!in_array($resource, $this->_run)) {
|
||||
$this->_run[] = $resource;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a plugin resource name
|
||||
*
|
||||
* Uses, in order of preference
|
||||
* - $_explicitType property of resource
|
||||
* - Short name of resource (if a matching prefix path is found)
|
||||
* - class name (if none of the above are true)
|
||||
*
|
||||
* The name is then cast to lowercase.
|
||||
*
|
||||
* @param Zend_Application_Resource_Resource $resource
|
||||
* @return string
|
||||
*/
|
||||
protected function _resolvePluginResourceName($resource)
|
||||
{
|
||||
if (isset($resource->_explicitType)) {
|
||||
$pluginName = $resource->_explicitType;
|
||||
} else {
|
||||
$className = get_class($resource);
|
||||
$pluginName = $className;
|
||||
$loader = $this->getPluginLoader();
|
||||
foreach ($loader->getPaths() as $prefix => $paths) {
|
||||
if (0 === strpos($className, $prefix)) {
|
||||
$pluginName = substr($className, strlen($prefix));
|
||||
$pluginName = trim($pluginName, '_');
|
||||
}
|
||||
}
|
||||
}
|
||||
$pluginName = strtolower($pluginName);
|
||||
return $pluginName;
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Bootstrap
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface for bootstrap classes
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Bootstrap
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Application_Bootstrap_Bootstrapper
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Zend_Application $application
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($application);
|
||||
|
||||
/**
|
||||
* Set bootstrap options
|
||||
*
|
||||
* @param array $options
|
||||
* @return Zend_Application_Bootstrap_Bootstrapper
|
||||
*/
|
||||
public function setOptions(array $options);
|
||||
|
||||
/**
|
||||
* Retrieve application object
|
||||
*
|
||||
* @return Zend_Application|Zend_Application_Bootstrap_Bootstrapper
|
||||
*/
|
||||
public function getApplication();
|
||||
|
||||
/**
|
||||
* Retrieve application environment
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvironment();
|
||||
|
||||
/**
|
||||
* Retrieve list of class resource initializers (_init* methods). Returns
|
||||
* as resource/method pairs.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getClassResources();
|
||||
|
||||
/**
|
||||
* Retrieve list of class resource initializer names (resource names only,
|
||||
* no method names)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getClassResourceNames();
|
||||
|
||||
/**
|
||||
* Bootstrap application or individual resource
|
||||
*
|
||||
* @param null|string $resource
|
||||
* @return mixed
|
||||
*/
|
||||
public function bootstrap($resource = null);
|
||||
|
||||
/**
|
||||
* Run the application
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run();
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Application_Exception
|
||||
*/
|
||||
require_once 'Zend/Application/Exception.php';
|
||||
|
||||
/**
|
||||
* Exception class for Zend_Application
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @uses Zend_Application_Exception
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Bootstrap_Exception extends Zend_Application_Exception
|
||||
{
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Bootstrap
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface for bootstrap classes that utilize resource plugins
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Bootstrap
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Application_Bootstrap_ResourceBootstrapper
|
||||
{
|
||||
/**
|
||||
* Register a resource with the bootstrap
|
||||
*
|
||||
* @param string|Zend_Application_Resource_Resource $resource
|
||||
* @param null|array|Zend_Config $options
|
||||
* @return Zend_Application_Bootstrap_ResourceBootstrapper
|
||||
*/
|
||||
public function registerPluginResource($resource, $options = null);
|
||||
|
||||
/**
|
||||
* Unregister a resource from the bootstrap
|
||||
*
|
||||
* @param string|Zend_Application_Resource_Resource $resource
|
||||
* @return Zend_Application_Bootstrap_ResourceBootstrapper
|
||||
*/
|
||||
public function unregisterPluginResource($resource);
|
||||
|
||||
/**
|
||||
* Is the requested resource registered?
|
||||
*
|
||||
* @param string $resource
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPluginResource($resource);
|
||||
|
||||
/**
|
||||
* Retrieve resource
|
||||
*
|
||||
* @param string $resource
|
||||
* @return Zend_Application_Resource_Resource
|
||||
*/
|
||||
public function getPluginResource($resource);
|
||||
|
||||
/**
|
||||
* Get all resources
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPluginResources();
|
||||
|
||||
/**
|
||||
* Get just resource names
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPluginResourceNames();
|
||||
|
||||
/**
|
||||
* Set plugin loader to use to fetch resources
|
||||
*
|
||||
* @param Zend_Loader_PluginLoader_Interface Zend_Loader_PluginLoader
|
||||
* @return Zend_Application_Bootstrap_ResourceBootstrapper
|
||||
*/
|
||||
public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader);
|
||||
|
||||
/**
|
||||
* Retrieve plugin loader for resources
|
||||
*
|
||||
* @return Zend_Loader_PluginLoader
|
||||
*/
|
||||
public function getPluginLoader();
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Exception
|
||||
*/
|
||||
require_once 'Zend/Exception.php';
|
||||
|
||||
/**
|
||||
* Exception class for Zend_Application
|
||||
*
|
||||
* @uses Zend_Exception
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Exception extends Zend_Exception
|
||||
{
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Module
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id$
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Loader_Autoloader_Resource */
|
||||
require_once 'Zend/Loader/Autoloader/Resource.php';
|
||||
|
||||
/**
|
||||
* Resource loader for application module classes
|
||||
*
|
||||
* @uses Zend_Loader_Autoloader_Resource
|
||||
* @package Zend_Application
|
||||
* @subpackage Module
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Module_Autoloader extends Zend_Loader_Autoloader_Resource
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array|Zend_Config $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options)
|
||||
{
|
||||
parent::__construct($options);
|
||||
$this->initDefaultResourceTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize default resource types for module resource classes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initDefaultResourceTypes()
|
||||
{
|
||||
$basePath = $this->getBasePath();
|
||||
$this->addResourceTypes(array(
|
||||
'dbtable' => array(
|
||||
'namespace' => 'Model_DbTable',
|
||||
'path' => 'models/DbTable',
|
||||
),
|
||||
'form' => array(
|
||||
'namespace' => 'Form',
|
||||
'path' => 'forms',
|
||||
),
|
||||
'model' => array(
|
||||
'namespace' => 'Model',
|
||||
'path' => 'models',
|
||||
),
|
||||
'plugin' => array(
|
||||
'namespace' => 'Plugin',
|
||||
'path' => 'plugins',
|
||||
),
|
||||
'service' => array(
|
||||
'namespace' => 'Service',
|
||||
'path' => 'services',
|
||||
),
|
||||
'viewhelper' => array(
|
||||
'namespace' => 'View_Helper',
|
||||
'path' => 'views/helpers',
|
||||
),
|
||||
'viewfilter' => array(
|
||||
'namespace' => 'View_Filter',
|
||||
'path' => 'views/filters',
|
||||
),
|
||||
));
|
||||
$this->setDefaultResourceType('model');
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Module
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id$
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Application_Bootstrap_Bootstrap
|
||||
*/
|
||||
require_once 'Zend/Application/Bootstrap/Bootstrap.php';
|
||||
|
||||
/**
|
||||
* Base bootstrap class for modules
|
||||
*
|
||||
* @uses Zend_Loader_Autoloader_Resource
|
||||
* @uses Zend_Application_Bootstrap_Bootstrap
|
||||
* @package Zend_Application
|
||||
* @subpackage Module
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Application_Module_Bootstrap
|
||||
extends Zend_Application_Bootstrap_Bootstrap
|
||||
{
|
||||
/**
|
||||
* @var Zend_Loader_Autoloader_Resource
|
||||
*/
|
||||
protected $_resourceLoader;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($application)
|
||||
{
|
||||
$this->setApplication($application);
|
||||
|
||||
// Use same plugin loader as parent bootstrap
|
||||
if ($application instanceof Zend_Application_Bootstrap_ResourceBootstrapper) {
|
||||
$this->setPluginLoader($application->getPluginLoader());
|
||||
}
|
||||
|
||||
$key = strtolower($this->getModuleName());
|
||||
if ($application->hasOption($key)) {
|
||||
// Don't run via setOptions() to prevent duplicate initialization
|
||||
$this->setOptions($application->getOption($key));
|
||||
}
|
||||
|
||||
if ($application->hasOption('resourceloader')) {
|
||||
$this->setOptions(array(
|
||||
'resourceloader' => $application->getOption('resourceloader')
|
||||
));
|
||||
}
|
||||
$this->initResourceLoader();
|
||||
|
||||
// ZF-6545: ensure front controller resource is loaded
|
||||
if (!$this->hasPluginResource('FrontController')) {
|
||||
$this->registerPluginResource('FrontController');
|
||||
}
|
||||
|
||||
// ZF-6545: prevent recursive registration of modules
|
||||
if ($this->hasPluginResource('modules')) {
|
||||
$this->unregisterPluginResource('modules');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set module resource loader
|
||||
*
|
||||
* @param Zend_Loader_Autoloader_Resource $loader
|
||||
* @return Zend_Application_Module_Bootstrap
|
||||
*/
|
||||
public function setResourceLoader(Zend_Loader_Autoloader_Resource $loader)
|
||||
{
|
||||
$this->_resourceLoader = $loader;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve module resource loader
|
||||
*
|
||||
* @return Zend_Loader_Autoloader_Resource
|
||||
*/
|
||||
public function getResourceLoader()
|
||||
{
|
||||
if (null === $this->_resourceLoader) {
|
||||
$r = new ReflectionClass($this);
|
||||
$path = $r->getFileName();
|
||||
$this->setResourceLoader(new Zend_Application_Module_Autoloader(array(
|
||||
'namespace' => $this->getModuleName(),
|
||||
'basePath' => dirname($path),
|
||||
)));
|
||||
}
|
||||
return $this->_resourceLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure resource loader is loaded
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initResourceLoader()
|
||||
{
|
||||
$this->getResourceLoader();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve module name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getModuleName()
|
||||
{
|
||||
if (empty($this->_moduleName)) {
|
||||
$class = get_class($this);
|
||||
if (preg_match('/^([a-z][a-z0-9]*)_/i', $class, $matches)) {
|
||||
$prefix = $matches[1];
|
||||
} else {
|
||||
$prefix = $class;
|
||||
}
|
||||
$this->_moduleName = $prefix;
|
||||
}
|
||||
return $this->_moduleName;
|
||||
}
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource for creating database adapter
|
||||
*
|
||||
* @uses Zend_Application_Resource_ResourceAbstract
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Db extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
/**
|
||||
* Adapter to use
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_adapter = null;
|
||||
|
||||
/**
|
||||
* @var Zend_Db_Adapter_Interface
|
||||
*/
|
||||
protected $_db;
|
||||
|
||||
/**
|
||||
* Parameters to use
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_params = array();
|
||||
|
||||
/**
|
||||
* Wether to register the created adapter as default table adapter
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_isDefaultTableAdapter = true;
|
||||
|
||||
/**
|
||||
* Set the adapter
|
||||
*
|
||||
* @param $adapter string
|
||||
* @return Zend_Application_Resource_Db
|
||||
*/
|
||||
public function setAdapter($adapter)
|
||||
{
|
||||
$this->_adapter = $adapter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapter type to use
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAdapter()
|
||||
{
|
||||
return $this->_adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the adapter params
|
||||
*
|
||||
* @param $adapter string
|
||||
* @return Zend_Application_Resource_Db
|
||||
*/
|
||||
public function setParams(array $params)
|
||||
{
|
||||
$this->_params = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapter parameters
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
return $this->_params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to use this as default table adapter
|
||||
*
|
||||
* @param boolean $defaultTableAdapter
|
||||
* @return Zend_Application_Resource_Db
|
||||
*/
|
||||
public function setIsDefaultTableAdapter($isDefaultTableAdapter)
|
||||
{
|
||||
$this->_isDefaultTableAdapter = $isDefaultTableAdapter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this adapter the default table adapter?
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function isDefaultTableAdapter()
|
||||
{
|
||||
return $this->_isDefaultTableAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve initialized DB connection
|
||||
*
|
||||
* @return null|Zend_Db_Adapter_Interface
|
||||
*/
|
||||
public function getDbAdapter()
|
||||
{
|
||||
if ((null === $this->_db)
|
||||
&& (null !== ($adapter = $this->getAdapter()))
|
||||
) {
|
||||
$this->_db = Zend_Db::factory($adapter, $this->getParams());
|
||||
}
|
||||
return $this->_db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Application_Resource_Resource
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract|null
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
if (null !== ($db = $this->getDbAdapter())) {
|
||||
if ($this->isDefaultTableAdapter()) {
|
||||
Zend_Db_Table::setDefaultAdapter($db);
|
||||
}
|
||||
return $db;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Exception class for Zend_Application
|
||||
*
|
||||
* @uses Zend_Application_Exception
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Exception extends Zend_Application_Exception
|
||||
{
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Front Controller resource
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Frontcontroller extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
/**
|
||||
* @var Zend_Controller_Front
|
||||
*/
|
||||
protected $_front;
|
||||
|
||||
/**
|
||||
* Initialize Front Controller
|
||||
*
|
||||
* @return Zend_Controller_Front
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$front = $this->getFrontController();
|
||||
|
||||
foreach ($this->getOptions() as $key => $value) {
|
||||
switch (strtolower($key)) {
|
||||
case 'controllerdirectory':
|
||||
if (is_string($value)) {
|
||||
$front->setControllerDirectory($value);
|
||||
} elseif (is_array($value)) {
|
||||
foreach ($value as $module => $directory) {
|
||||
$front->addControllerDirectory($directory, $module);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'modulecontrollerdirectoryname':
|
||||
$front->setModuleControllerDirectoryName($value);
|
||||
break;
|
||||
|
||||
case 'moduledirectory':
|
||||
$front->addModuleDirectory($value);
|
||||
break;
|
||||
|
||||
case 'defaultcontrollername':
|
||||
$front->setDefaultControllerName($value);
|
||||
break;
|
||||
|
||||
case 'defaultaction':
|
||||
$front->setDefaultAction($value);
|
||||
break;
|
||||
|
||||
case 'defaultmodule':
|
||||
$front->setDefaultModule($value);
|
||||
break;
|
||||
|
||||
case 'baseurl':
|
||||
if (!empty($value)) {
|
||||
$front->setBaseUrl($value);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'params':
|
||||
$front->setParams($value);
|
||||
break;
|
||||
|
||||
case 'plugins':
|
||||
foreach ((array) $value as $pluginClass) {
|
||||
$plugin = new $pluginClass();
|
||||
$front->registerPlugin($plugin);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'throwexceptions':
|
||||
$front->throwExceptions((bool) $value);
|
||||
break;
|
||||
|
||||
case 'actionhelperpaths':
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $helperPrefix => $helperPath) {
|
||||
Zend_Controller_Action_HelperBroker::addPath($helperPath, $helperPrefix);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
$front->setParam($key, $value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== ($bootstrap = $this->getBootstrap())) {
|
||||
$this->getBootstrap()->frontController = $front;
|
||||
}
|
||||
|
||||
return $front;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve front controller instance
|
||||
*
|
||||
* @return Zend_Controller_Front
|
||||
*/
|
||||
public function getFrontController()
|
||||
{
|
||||
if (null === $this->_front) {
|
||||
$this->_front = Zend_Controller_Front::getInstance();
|
||||
}
|
||||
return $this->_front;
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource for settings layout options
|
||||
*
|
||||
* @uses Zend_Application_Resource_ResourceAbstract
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Layout
|
||||
extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
/**
|
||||
* @var Zend_Layout
|
||||
*/
|
||||
protected $_layout;
|
||||
|
||||
/**
|
||||
* Defined by Zend_Application_Resource_Resource
|
||||
*
|
||||
* @return Zend_Layout
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->getBootstrap()->bootstrap('FrontController');
|
||||
return $this->getLayout();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve layout object
|
||||
*
|
||||
* @return Zend_Layout
|
||||
*/
|
||||
public function getLayout()
|
||||
{
|
||||
if (null === $this->_layout) {
|
||||
$this->_layout = Zend_Layout::startMvc($this->getOptions());
|
||||
}
|
||||
return $this->_layout;
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource for initializing the locale
|
||||
*
|
||||
* @uses Zend_Application_Resource_Base
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Locale
|
||||
extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
const DEFAULT_REGISTRY_KEY = 'Zend_Locale';
|
||||
|
||||
/**
|
||||
* @var Zend_Locale
|
||||
*/
|
||||
protected $_locale;
|
||||
|
||||
/**
|
||||
* Defined by Zend_Application_Resource_Resource
|
||||
*
|
||||
* @return Zend_Locale
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
return $this->getLocale();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve locale object
|
||||
*
|
||||
* @return Zend_Locale
|
||||
*/
|
||||
public function getLocale()
|
||||
{
|
||||
if (null === $this->_locale) {
|
||||
$options = $this->getOptions();
|
||||
if (!isset($options['default'])) {
|
||||
$this->_locale = new Zend_Locale();
|
||||
} else {
|
||||
Zend_Locale::setDefault($options['default']);
|
||||
$this->_locale = new Zend_Locale($options['default']);
|
||||
}
|
||||
|
||||
$key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
|
||||
? $options['registry_key']
|
||||
: self::DEFAULT_REGISTRY_KEY;
|
||||
Zend_Registry::set($key, $this->_locale);
|
||||
}
|
||||
return $this->_locale;
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module bootstrapping resource
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Modules extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
/**
|
||||
* @var ArrayObject
|
||||
*/
|
||||
protected $_bootstraps;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param mixed $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = null)
|
||||
{
|
||||
$this->_bootstraps = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize modules
|
||||
*
|
||||
* @return array
|
||||
* @throws Zend_Application_Resource_Exception When bootstrap class was not found
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$bootstrap = $this->getBootstrap();
|
||||
$bootstrap->bootstrap('FrontController');
|
||||
$front = $bootstrap->getResource('FrontController');
|
||||
|
||||
$modules = $front->getControllerDirectory();
|
||||
$default = $front->getDefaultModule();
|
||||
$curBootstrapClass = get_class($bootstrap);
|
||||
foreach ($modules as $module => $moduleDirectory) {
|
||||
$bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
|
||||
if (!class_exists($bootstrapClass, false)) {
|
||||
$bootstrapPath = dirname($moduleDirectory) . '/Bootstrap.php';
|
||||
if (file_exists($bootstrapPath)) {
|
||||
$eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
|
||||
include_once $bootstrapPath;
|
||||
if (($default != $module)
|
||||
&& !class_exists($bootstrapClass, false)
|
||||
) {
|
||||
throw new Zend_Application_Resource_Exception(sprintf(
|
||||
$eMsgTpl, $module, $bootstrapClass
|
||||
));
|
||||
} elseif ($default == $module) {
|
||||
if (!class_exists($bootstrapClass, false)) {
|
||||
$bootstrapClass = 'Bootstrap';
|
||||
if (!class_exists($bootstrapClass, false)) {
|
||||
throw new Zend_Application_Resource_Exception(sprintf(
|
||||
$eMsgTpl, $module, $bootstrapClass
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if ($bootstrapClass == $curBootstrapClass) {
|
||||
// If the found bootstrap class matches the one calling this
|
||||
// resource, don't re-execute.
|
||||
continue;
|
||||
}
|
||||
|
||||
$moduleBootstrap = new $bootstrapClass($bootstrap);
|
||||
$moduleBootstrap->bootstrap();
|
||||
$this->_bootstraps[$module] = $moduleBootstrap;
|
||||
}
|
||||
|
||||
return $this->_bootstraps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bootstraps that have been run
|
||||
*
|
||||
* @return ArrayObject
|
||||
*/
|
||||
public function getExecutedBootstraps()
|
||||
{
|
||||
return $this->_bootstraps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a module name to the module class prefix
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
protected function _formatModuleName($name)
|
||||
{
|
||||
$name = strtolower($name);
|
||||
$name = str_replace(array('-', '.'), ' ', $name);
|
||||
$name = ucwords($name);
|
||||
$name = str_replace(' ', '', $name);
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource for setting navigation structure
|
||||
*
|
||||
* @uses Zend_Application_Resource_ResourceAbstract
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @author Dolf Schimmel
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Navigation
|
||||
extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
const DEFAULT_REGISTRY_KEY = 'Zend_Navigation';
|
||||
|
||||
/**
|
||||
* @var Zend_Navigation
|
||||
*/
|
||||
protected $_container;
|
||||
|
||||
/**
|
||||
* Defined by Zend_Application_Resource_Resource
|
||||
*
|
||||
* @return Zend_Navigation
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
if (!$this->_container) {
|
||||
$options = $this->getOptions();
|
||||
$pages = isset($options['pages']) ? $options['pages'] : array();
|
||||
$this->_container = new Zend_Navigation($pages);
|
||||
}
|
||||
|
||||
$this->store();
|
||||
return $this->_container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores navigation container in registry or Navigation view helper
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function store()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
if (isset($options['storage']['registry']) &&
|
||||
$options['storage']['registry'] == true) {
|
||||
$this->_storeRegistry();
|
||||
} else {
|
||||
$this->_storeHelper();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores navigation container in the registry
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _storeRegistry()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
if(isset($options['storage']) &&
|
||||
isset($options['storage']['registry']) &&
|
||||
isset($options['storage']['registry']['key']))
|
||||
{
|
||||
$key = $options['storage']['registry']['key'];
|
||||
} else {
|
||||
$key = self::DEFAULT_REGISTRY_KEY;
|
||||
}
|
||||
|
||||
Zend_Registry::set($key,$this->getContainer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores navigation container in the Navigation helper
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _storeHelper()
|
||||
{
|
||||
$this->getBootstrap()->bootstrap('view');
|
||||
$view = $this->getBootstrap()->view;
|
||||
$view->getHelper('navigation')->navigation($this->getContainer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns navigation container
|
||||
*
|
||||
* @return Zend_Navigation
|
||||
*/
|
||||
public function getContainer()
|
||||
{
|
||||
return $this->_container;
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface for bootstrap resources
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Application_Resource_Resource
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Must take an optional single argument, $options.
|
||||
*
|
||||
* @param mixed $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = null);
|
||||
|
||||
/**
|
||||
* Set the bootstrap to which the resource is attached
|
||||
*
|
||||
* @param Zend_Application_Bootstrap_Bootstrapper $bootstrap
|
||||
* @return Zend_Application_Resource_Resource
|
||||
*/
|
||||
public function setBootstrap(Zend_Application_Bootstrap_Bootstrapper $bootstrap);
|
||||
|
||||
/**
|
||||
* Retrieve the bootstrap to which the resource is attached
|
||||
*
|
||||
* @return Zend_Application_Bootstrap_Bootstrapper
|
||||
*/
|
||||
public function getBootstrap();
|
||||
|
||||
/**
|
||||
* Set resource options
|
||||
*
|
||||
* @param array $options
|
||||
* @return Zend_Application_Resource_Resource
|
||||
*/
|
||||
public function setOptions(array $options);
|
||||
|
||||
/**
|
||||
* Retrieve resource options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions();
|
||||
|
||||
/**
|
||||
* Strategy pattern: initialize resource
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function init();
|
||||
}
|
||||
@@ -1,159 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Application_Resource_Resource
|
||||
*/
|
||||
require_once 'Zend/Application/Resource/Resource.php';
|
||||
|
||||
/**
|
||||
* Abstract class for bootstrap resources
|
||||
*
|
||||
* @uses Zend_Application_Resource_Resource
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Application_Resource_ResourceAbstract implements Zend_Application_Resource_Resource
|
||||
{
|
||||
/**
|
||||
* Parent bootstrap
|
||||
*
|
||||
* @var Zend_Application_Bootstrap_Bootstrapper
|
||||
*/
|
||||
protected $_bootstrap;
|
||||
|
||||
/**
|
||||
* Options for the resource
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_options = array();
|
||||
|
||||
/**
|
||||
* Option keys to skip when calling setOptions()
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_skipOptions = array(
|
||||
'options',
|
||||
'config',
|
||||
);
|
||||
|
||||
/**
|
||||
* Create a instance with options
|
||||
*
|
||||
* @param mixed $options
|
||||
*/
|
||||
public function __construct($options = null)
|
||||
{
|
||||
if (is_array($options)) {
|
||||
$this->setOptions($options);
|
||||
} else if ($options instanceof Zend_Config) {
|
||||
$this->setOptions($options->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set options from array
|
||||
*
|
||||
* @param array $options Configuration for resource
|
||||
* @return Zend_Application_Resource_ResourceAbstract
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $key => $value) {
|
||||
if (in_array(strtolower($key), $this->_skipOptions)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$method = 'set' . strtolower($key);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->$method($value);
|
||||
}
|
||||
if ('bootstrap' == $key) {
|
||||
unset($options[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->_options = $this->mergeOptions($this->_options, $options);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve resource options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge options recursively
|
||||
*
|
||||
* @param array $array1
|
||||
* @param mixed $array2
|
||||
* @return array
|
||||
*/
|
||||
public function mergeOptions(array $array1, $array2 = null)
|
||||
{
|
||||
if (is_array($array2)) {
|
||||
foreach ($array2 as $key => $val) {
|
||||
if (is_array($array2[$key])) {
|
||||
$array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
|
||||
? $this->mergeOptions($array1[$key], $array2[$key])
|
||||
: $array2[$key];
|
||||
} else {
|
||||
$array1[$key] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $array1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bootstrap to which the resource is attached
|
||||
*
|
||||
* @param Zend_Application_Bootstrap_Bootstrapper $bootstrap
|
||||
* @return Zend_Application_Resource_Resource
|
||||
*/
|
||||
public function setBootstrap(Zend_Application_Bootstrap_Bootstrapper $bootstrap)
|
||||
{
|
||||
$this->_bootstrap = $bootstrap;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the bootstrap to which the resource is attached
|
||||
*
|
||||
* @return null|Zend_Application_Bootstrap_Bootstrapper
|
||||
*/
|
||||
public function getBootstrap()
|
||||
{
|
||||
return $this->_bootstrap;
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource for initializing the locale
|
||||
*
|
||||
* @uses Zend_Application_Resource_Base
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Router
|
||||
extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
/**
|
||||
* @var Zend_Controller_Router_Rewrite
|
||||
*/
|
||||
protected $_router;
|
||||
|
||||
/**
|
||||
* Defined by Zend_Application_Resource_Resource
|
||||
*
|
||||
* @return Zend_Controller_Router_Rewrite
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
return $this->getRouter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve router object
|
||||
*
|
||||
* @return Zend_Controller_Router_Rewrite
|
||||
*/
|
||||
public function getRouter()
|
||||
{
|
||||
if (null === $this->_router) {
|
||||
$bootstrap = $this->getBootstrap();
|
||||
$bootstrap->bootstrap('FrontController');
|
||||
$this->_router = $bootstrap->getContainer()->frontcontroller->getRouter();
|
||||
|
||||
$options = $this->getOptions();
|
||||
if (!isset($options['routes'])) {
|
||||
$options['routes'] = array();
|
||||
}
|
||||
|
||||
|
||||
if (isset($options['chainNameSeparator'])) {
|
||||
$this->_router->setChainNameSeparator($options['chainNameSeparator']);
|
||||
}
|
||||
|
||||
|
||||
$this->_router->addConfig(new Zend_Config($options['routes']));
|
||||
}
|
||||
return $this->_router;
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource for setting session options
|
||||
*
|
||||
* @uses Zend_Application_Resource_ResourceAbstract
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Session extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
/**
|
||||
* Save handler to use
|
||||
*
|
||||
* @var Zend_Session_SaveHandler_Interface
|
||||
*/
|
||||
protected $_saveHandler = null;
|
||||
|
||||
/**
|
||||
* Set session save handler
|
||||
*
|
||||
* @param array|string|Zend_Session_SaveHandler_Interface $saveHandler
|
||||
* @return Zend_Application_Resource_Session
|
||||
* @throws Zend_Application_Resource_Exception When $saveHandler is no valid save handler
|
||||
*/
|
||||
public function setSaveHandler($saveHandler)
|
||||
{
|
||||
$this->_saveHandler = $saveHandler;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get session save handler
|
||||
*
|
||||
* @return Zend_Session_SaveHandler_Interface
|
||||
*/
|
||||
public function getSaveHandler()
|
||||
{
|
||||
if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
|
||||
if (is_array($this->_saveHandler)) {
|
||||
if (!array_key_exists('class', $this->_saveHandler)) {
|
||||
throw new Zend_Application_Resource_Exception('Session save handler class not provided in options');
|
||||
}
|
||||
$options = array();
|
||||
if (array_key_exists('options', $this->_saveHandler)) {
|
||||
$options = $this->_saveHandler['options'];
|
||||
}
|
||||
$this->_saveHandler = $this->_saveHandler['class'];
|
||||
$this->_saveHandler = new $this->_saveHandler($options);
|
||||
} elseif (is_string($this->_saveHandler)) {
|
||||
$this->_saveHandler = new $this->_saveHandler();
|
||||
}
|
||||
|
||||
if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
|
||||
throw new Zend_Application_Resource_Exception('Invalid session save handler');
|
||||
}
|
||||
}
|
||||
return $this->_saveHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function _hasSaveHandler()
|
||||
{
|
||||
return ($this->_saveHandler !== null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Application_Resource_Resource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$options = array_change_key_case($this->getOptions(), CASE_LOWER);
|
||||
if (isset($options['savehandler'])) {
|
||||
unset($options['savehandler']);
|
||||
}
|
||||
|
||||
if (count($options) > 0) {
|
||||
Zend_Session::setOptions($options);
|
||||
}
|
||||
|
||||
if ($this->_hasSaveHandler()) {
|
||||
Zend_Session::setSaveHandler($this->getSaveHandler());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource for setting translation options
|
||||
*
|
||||
* @uses Zend_Application_Resource_ResourceAbstract
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_Translate extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
const DEFAULT_REGISTRY_KEY = 'Zend_Translate';
|
||||
|
||||
/**
|
||||
* @var Zend_Translate
|
||||
*/
|
||||
protected $_translate;
|
||||
|
||||
/**
|
||||
* Defined by Zend_Application_Resource_Resource
|
||||
*
|
||||
* @return Zend_Translate
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
return $this->getTranslate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve translate object
|
||||
*
|
||||
* @return Zend_Translate
|
||||
*/
|
||||
public function getTranslate()
|
||||
{
|
||||
if (null === $this->_translate) {
|
||||
$options = $this->getOptions();
|
||||
|
||||
if (!isset($options['data'])) {
|
||||
throw new Zend_Application_Resource_Exception('No translation source data provided.');
|
||||
}
|
||||
|
||||
$adapter = isset($options['adapter']) ? $options['adapter'] : Zend_Translate::AN_ARRAY;
|
||||
$locale = isset($options['locale']) ? $options['locale'] : null;
|
||||
$translateOptions = isset($options['options']) ? $options['options'] : array();
|
||||
|
||||
$this->_translate = new Zend_Translate(
|
||||
$adapter, $options['data'], $locale, $translateOptions
|
||||
);
|
||||
|
||||
$key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
|
||||
? $options['registry_key']
|
||||
: self::DEFAULT_REGISTRY_KEY;
|
||||
|
||||
Zend_Registry::set($key, $this->_translate);
|
||||
}
|
||||
|
||||
return $this->_translate;
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource for settings view options
|
||||
*
|
||||
* @uses Zend_Application_Resource_ResourceAbstract
|
||||
* @category Zend
|
||||
* @package Zend_Application
|
||||
* @subpackage Resource
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Application_Resource_View extends Zend_Application_Resource_ResourceAbstract
|
||||
{
|
||||
/**
|
||||
* @var Zend_View_Interface
|
||||
*/
|
||||
protected $_view;
|
||||
|
||||
/**
|
||||
* Defined by Zend_Application_Resource_Resource
|
||||
*
|
||||
* @return Zend_View
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$view = $this->getView();
|
||||
|
||||
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
|
||||
$viewRenderer->setView($view);
|
||||
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
|
||||
return $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve view object
|
||||
*
|
||||
* @return Zend_View
|
||||
*/
|
||||
public function getView()
|
||||
{
|
||||
if (null === $this->_view) {
|
||||
$this->_view = new Zend_View($this->getOptions());
|
||||
}
|
||||
return $this->_view;
|
||||
}
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth
|
||||
{
|
||||
/**
|
||||
* Singleton instance
|
||||
*
|
||||
* @var Zend_Auth
|
||||
*/
|
||||
protected static $_instance = null;
|
||||
|
||||
/**
|
||||
* Persistent storage handler
|
||||
*
|
||||
* @var Zend_Auth_Storage_Interface
|
||||
*/
|
||||
protected $_storage = null;
|
||||
|
||||
/**
|
||||
* Singleton pattern implementation makes "new" unavailable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function __construct()
|
||||
{}
|
||||
|
||||
/**
|
||||
* Singleton pattern implementation makes "clone" unavailable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function __clone()
|
||||
{}
|
||||
|
||||
/**
|
||||
* Returns an instance of Zend_Auth
|
||||
*
|
||||
* Singleton pattern implementation
|
||||
*
|
||||
* @return Zend_Auth Provides a fluent interface
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (null === self::$_instance) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the persistent storage handler
|
||||
*
|
||||
* Session storage is used by default unless a different storage adapter has been set.
|
||||
*
|
||||
* @return Zend_Auth_Storage_Interface
|
||||
*/
|
||||
public function getStorage()
|
||||
{
|
||||
if (null === $this->_storage) {
|
||||
/**
|
||||
* @see Zend_Auth_Storage_Session
|
||||
*/
|
||||
require_once 'Zend/Auth/Storage/Session.php';
|
||||
$this->setStorage(new Zend_Auth_Storage_Session());
|
||||
}
|
||||
|
||||
return $this->_storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the persistent storage handler
|
||||
*
|
||||
* @param Zend_Auth_Storage_Interface $storage
|
||||
* @return Zend_Auth Provides a fluent interface
|
||||
*/
|
||||
public function setStorage(Zend_Auth_Storage_Interface $storage)
|
||||
{
|
||||
$this->_storage = $storage;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticates against the supplied adapter
|
||||
*
|
||||
* @param Zend_Auth_Adapter_Interface $adapter
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
public function authenticate(Zend_Auth_Adapter_Interface $adapter)
|
||||
{
|
||||
$result = $adapter->authenticate();
|
||||
|
||||
/**
|
||||
* ZF-7546 - prevent multiple succesive calls from storing inconsistent results
|
||||
* Ensure storage has clean state
|
||||
*/
|
||||
if ($this->hasIdentity()) {
|
||||
$this->clearIdentity();
|
||||
}
|
||||
|
||||
if ($result->isValid()) {
|
||||
$this->getStorage()->write($result->getIdentity());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if and only if an identity is available from storage
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasIdentity()
|
||||
{
|
||||
return !$this->getStorage()->isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the identity from storage or null if no identity is available
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getIdentity()
|
||||
{
|
||||
$storage = $this->getStorage();
|
||||
|
||||
if ($storage->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $storage->read();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the identity from persistent storage
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clearIdentity()
|
||||
{
|
||||
$this->getStorage()->clear();
|
||||
}
|
||||
}
|
||||
@@ -1,484 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Interface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Db_Adapter_Abstract
|
||||
*/
|
||||
require_once 'Zend/Db/Adapter/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Result
|
||||
*/
|
||||
require_once 'Zend/Auth/Result.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Adapter_DbTable implements Zend_Auth_Adapter_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* Database Connection
|
||||
*
|
||||
* @var Zend_Db_Adapter_Abstract
|
||||
*/
|
||||
protected $_zendDb = null;
|
||||
|
||||
/**
|
||||
* @var Zend_Db_Select
|
||||
*/
|
||||
protected $_dbSelect = null;
|
||||
|
||||
/**
|
||||
* $_tableName - the table name to check
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_tableName = null;
|
||||
|
||||
/**
|
||||
* $_identityColumn - the column to use as the identity
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_identityColumn = null;
|
||||
|
||||
/**
|
||||
* $_credentialColumns - columns to be used as the credentials
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_credentialColumn = null;
|
||||
|
||||
/**
|
||||
* $_identity - Identity value
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_identity = null;
|
||||
|
||||
/**
|
||||
* $_credential - Credential values
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_credential = null;
|
||||
|
||||
/**
|
||||
* $_credentialTreatment - Treatment applied to the credential, such as MD5() or PASSWORD()
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_credentialTreatment = null;
|
||||
|
||||
/**
|
||||
* $_authenticateResultInfo
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_authenticateResultInfo = null;
|
||||
|
||||
/**
|
||||
* $_resultRow - Results of database authentication query
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_resultRow = null;
|
||||
|
||||
/**
|
||||
* __construct() - Sets configuration options
|
||||
*
|
||||
* @param Zend_Db_Adapter_Abstract $zendDb
|
||||
* @param string $tableName
|
||||
* @param string $identityColumn
|
||||
* @param string $credentialColumn
|
||||
* @param string $credentialTreatment
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Zend_Db_Adapter_Abstract $zendDb, $tableName = null, $identityColumn = null,
|
||||
$credentialColumn = null, $credentialTreatment = null)
|
||||
{
|
||||
$this->_zendDb = $zendDb;
|
||||
|
||||
if (null !== $tableName) {
|
||||
$this->setTableName($tableName);
|
||||
}
|
||||
|
||||
if (null !== $identityColumn) {
|
||||
$this->setIdentityColumn($identityColumn);
|
||||
}
|
||||
|
||||
if (null !== $credentialColumn) {
|
||||
$this->setCredentialColumn($credentialColumn);
|
||||
}
|
||||
|
||||
if (null !== $credentialTreatment) {
|
||||
$this->setCredentialTreatment($credentialTreatment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* setTableName() - set the table name to be used in the select query
|
||||
*
|
||||
* @param string $tableName
|
||||
* @return Zend_Auth_Adapter_DbTable Provides a fluent interface
|
||||
*/
|
||||
public function setTableName($tableName)
|
||||
{
|
||||
$this->_tableName = $tableName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setIdentityColumn() - set the column name to be used as the identity column
|
||||
*
|
||||
* @param string $identityColumn
|
||||
* @return Zend_Auth_Adapter_DbTable Provides a fluent interface
|
||||
*/
|
||||
public function setIdentityColumn($identityColumn)
|
||||
{
|
||||
$this->_identityColumn = $identityColumn;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setCredentialColumn() - set the column name to be used as the credential column
|
||||
*
|
||||
* @param string $credentialColumn
|
||||
* @return Zend_Auth_Adapter_DbTable Provides a fluent interface
|
||||
*/
|
||||
public function setCredentialColumn($credentialColumn)
|
||||
{
|
||||
$this->_credentialColumn = $credentialColumn;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setCredentialTreatment() - allows the developer to pass a parameterized string that is
|
||||
* used to transform or treat the input credential data
|
||||
*
|
||||
* In many cases, passwords and other sensitive data are encrypted, hashed, encoded,
|
||||
* obscured, or otherwise treated through some function or algorithm. By specifying a
|
||||
* parameterized treatment string with this method, a developer may apply arbitrary SQL
|
||||
* upon input credential data.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* 'PASSWORD(?)'
|
||||
* 'MD5(?)'
|
||||
*
|
||||
* @param string $treatment
|
||||
* @return Zend_Auth_Adapter_DbTable Provides a fluent interface
|
||||
*/
|
||||
public function setCredentialTreatment($treatment)
|
||||
{
|
||||
$this->_credentialTreatment = $treatment;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setIdentity() - set the value to be used as the identity
|
||||
*
|
||||
* @param string $value
|
||||
* @return Zend_Auth_Adapter_DbTable Provides a fluent interface
|
||||
*/
|
||||
public function setIdentity($value)
|
||||
{
|
||||
$this->_identity = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setCredential() - set the credential value to be used, optionally can specify a treatment
|
||||
* to be used, should be supplied in parameterized form, such as 'MD5(?)' or 'PASSWORD(?)'
|
||||
*
|
||||
* @param string $credential
|
||||
* @return Zend_Auth_Adapter_DbTable Provides a fluent interface
|
||||
*/
|
||||
public function setCredential($credential)
|
||||
{
|
||||
$this->_credential = $credential;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDbSelect() - Return the preauthentication Db Select object for userland select query modification
|
||||
*
|
||||
* @return Zend_Db_Select
|
||||
*/
|
||||
public function getDbSelect()
|
||||
{
|
||||
if ($this->_dbSelect == null) {
|
||||
$this->_dbSelect = $this->_zendDb->select();
|
||||
}
|
||||
|
||||
return $this->_dbSelect;
|
||||
}
|
||||
|
||||
/**
|
||||
* getResultRowObject() - Returns the result row as a stdClass object
|
||||
*
|
||||
* @param string|array $returnColumns
|
||||
* @param string|array $omitColumns
|
||||
* @return stdClass|boolean
|
||||
*/
|
||||
public function getResultRowObject($returnColumns = null, $omitColumns = null)
|
||||
{
|
||||
if (!$this->_resultRow) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$returnObject = new stdClass();
|
||||
|
||||
if (null !== $returnColumns) {
|
||||
|
||||
$availableColumns = array_keys($this->_resultRow);
|
||||
foreach ( (array) $returnColumns as $returnColumn) {
|
||||
if (in_array($returnColumn, $availableColumns)) {
|
||||
$returnObject->{$returnColumn} = $this->_resultRow[$returnColumn];
|
||||
}
|
||||
}
|
||||
return $returnObject;
|
||||
|
||||
} elseif (null !== $omitColumns) {
|
||||
|
||||
$omitColumns = (array) $omitColumns;
|
||||
foreach ($this->_resultRow as $resultColumn => $resultValue) {
|
||||
if (!in_array($resultColumn, $omitColumns)) {
|
||||
$returnObject->{$resultColumn} = $resultValue;
|
||||
}
|
||||
}
|
||||
return $returnObject;
|
||||
|
||||
} else {
|
||||
|
||||
foreach ($this->_resultRow as $resultColumn => $resultValue) {
|
||||
$returnObject->{$resultColumn} = $resultValue;
|
||||
}
|
||||
return $returnObject;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* authenticate() - defined by Zend_Auth_Adapter_Interface. This method is called to
|
||||
* attempt an authenication. Previous to this call, this adapter would have already
|
||||
* been configured with all nessissary information to successfully connect to a database
|
||||
* table and attempt to find a record matching the provided identity.
|
||||
*
|
||||
* @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
public function authenticate()
|
||||
{
|
||||
$this->_authenticateSetup();
|
||||
$dbSelect = $this->_authenticateCreateSelect();
|
||||
$resultIdentities = $this->_authenticateQuerySelect($dbSelect);
|
||||
|
||||
if ( ($authResult = $this->_authenticateValidateResultset($resultIdentities)) instanceof Zend_Auth_Result) {
|
||||
return $authResult;
|
||||
}
|
||||
|
||||
$authResult = $this->_authenticateValidateResult(array_shift($resultIdentities));
|
||||
return $authResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* _authenticateSetup() - This method abstracts the steps involved with making sure
|
||||
* that this adapter was indeed setup properly with all required peices of information.
|
||||
*
|
||||
* @throws Zend_Auth_Adapter_Exception - in the event that setup was not done properly
|
||||
* @return true
|
||||
*/
|
||||
protected function _authenticateSetup()
|
||||
{
|
||||
$exception = null;
|
||||
|
||||
if ($this->_tableName == '') {
|
||||
$exception = 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
|
||||
} elseif ($this->_identityColumn == '') {
|
||||
$exception = 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
|
||||
} elseif ($this->_credentialColumn == '') {
|
||||
$exception = 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
|
||||
} elseif ($this->_identity == '') {
|
||||
$exception = 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
|
||||
} elseif ($this->_credential === null) {
|
||||
$exception = 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
|
||||
}
|
||||
|
||||
if (null !== $exception) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception($exception);
|
||||
}
|
||||
|
||||
$this->_authenticateResultInfo = array(
|
||||
'code' => Zend_Auth_Result::FAILURE,
|
||||
'identity' => $this->_identity,
|
||||
'messages' => array()
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* _authenticateCreateSelect() - This method creates a Zend_Db_Select object that
|
||||
* is completely configured to be queried against the database.
|
||||
*
|
||||
* @return Zend_Db_Select
|
||||
*/
|
||||
protected function _authenticateCreateSelect()
|
||||
{
|
||||
// build credential expression
|
||||
if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, '?') === false)) {
|
||||
$this->_credentialTreatment = '?';
|
||||
}
|
||||
|
||||
$credentialExpression = new Zend_Db_Expr(
|
||||
'(CASE WHEN ' .
|
||||
$this->_zendDb->quoteInto(
|
||||
$this->_zendDb->quoteIdentifier($this->_credentialColumn, true)
|
||||
. ' = ' . $this->_credentialTreatment, $this->_credential
|
||||
)
|
||||
. ' THEN 1 ELSE 0 END) AS '
|
||||
. $this->_zendDb->quoteIdentifier(
|
||||
$this->_zendDb->foldCase('zend_auth_credential_match')
|
||||
)
|
||||
);
|
||||
|
||||
// get select
|
||||
$dbSelect = clone $this->getDbSelect();
|
||||
$dbSelect->from($this->_tableName, array('*', $credentialExpression))
|
||||
->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
|
||||
|
||||
return $dbSelect;
|
||||
}
|
||||
|
||||
/**
|
||||
* _authenticateQuerySelect() - This method accepts a Zend_Db_Select object and
|
||||
* performs a query against the database with that object.
|
||||
*
|
||||
* @param Zend_Db_Select $dbSelect
|
||||
* @throws Zend_Auth_Adapter_Exception - when a invalid select object is encoutered
|
||||
* @return array
|
||||
*/
|
||||
protected function _authenticateQuerySelect(Zend_Db_Select $dbSelect)
|
||||
{
|
||||
try {
|
||||
if ($this->_zendDb->getFetchMode() != Zend_DB::FETCH_ASSOC) {
|
||||
$origDbFetchMode = $this->_zendDb->getFetchMode();
|
||||
$this->_zendDb->setFetchMode(Zend_DB::FETCH_ASSOC);
|
||||
}
|
||||
$resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString());
|
||||
if (isset($origDbFetchMode)) {
|
||||
$this->_zendDb->setFetchMode($origDbFetchMode);
|
||||
unset($origDbFetchMode);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('The supplied parameters to Zend_Auth_Adapter_DbTable failed to '
|
||||
. 'produce a valid sql statement, please check table and column names '
|
||||
. 'for validity.');
|
||||
}
|
||||
return $resultIdentities;
|
||||
}
|
||||
|
||||
/**
|
||||
* _authenticateValidateResultSet() - This method attempts to make certian that only one
|
||||
* record was returned in the result set
|
||||
*
|
||||
* @param array $resultIdentities
|
||||
* @return true|Zend_Auth_Result
|
||||
*/
|
||||
protected function _authenticateValidateResultSet(array $resultIdentities)
|
||||
{
|
||||
|
||||
if (count($resultIdentities) < 1) {
|
||||
$this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
|
||||
$this->_authenticateResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
|
||||
return $this->_authenticateCreateAuthResult();
|
||||
} elseif (count($resultIdentities) > 1) {
|
||||
$this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
|
||||
$this->_authenticateResultInfo['messages'][] = 'More than one record matches the supplied identity.';
|
||||
return $this->_authenticateCreateAuthResult();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* _authenticateValidateResult() - This method attempts to validate that the record in the
|
||||
* result set is indeed a record that matched the identity provided to this adapter.
|
||||
*
|
||||
* @param array $resultIdentity
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
protected function _authenticateValidateResult($resultIdentity)
|
||||
{
|
||||
$zendAuthCredentialMatchColumn = $this->_zendDb->foldCase('zend_auth_credential_match');
|
||||
|
||||
if ($resultIdentity[$zendAuthCredentialMatchColumn] != '1') {
|
||||
$this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
|
||||
$this->_authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
|
||||
return $this->_authenticateCreateAuthResult();
|
||||
}
|
||||
|
||||
unset($resultIdentity[$zendAuthCredentialMatchColumn]);
|
||||
$this->_resultRow = $resultIdentity;
|
||||
|
||||
$this->_authenticateResultInfo['code'] = Zend_Auth_Result::SUCCESS;
|
||||
$this->_authenticateResultInfo['messages'][] = 'Authentication successful.';
|
||||
return $this->_authenticateCreateAuthResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* _authenticateCreateAuthResult() - This method creates a Zend_Auth_Result object
|
||||
* from the information that has been collected during the authenticate() attempt.
|
||||
*
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
protected function _authenticateCreateAuthResult()
|
||||
{
|
||||
return new Zend_Auth_Result(
|
||||
$this->_authenticateResultInfo['code'],
|
||||
$this->_authenticateResultInfo['identity'],
|
||||
$this->_authenticateResultInfo['messages']
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,230 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Adapter_Digest implements Zend_Auth_Adapter_Interface
|
||||
{
|
||||
/**
|
||||
* Filename against which authentication queries are performed
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_filename;
|
||||
|
||||
/**
|
||||
* Digest authentication realm
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_realm;
|
||||
|
||||
/**
|
||||
* Digest authentication user
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_username;
|
||||
|
||||
/**
|
||||
* Password for the user of the realm
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_password;
|
||||
|
||||
/**
|
||||
* Sets adapter options
|
||||
*
|
||||
* @param mixed $filename
|
||||
* @param mixed $realm
|
||||
* @param mixed $username
|
||||
* @param mixed $password
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($filename = null, $realm = null, $username = null, $password = null)
|
||||
{
|
||||
$options = array('filename', 'realm', 'username', 'password');
|
||||
foreach ($options as $option) {
|
||||
if (null !== $$option) {
|
||||
$methodName = 'set' . ucfirst($option);
|
||||
$this->$methodName($$option);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the filename option value or null if it has not yet been set
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getFilename()
|
||||
{
|
||||
return $this->_filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the filename option value
|
||||
*
|
||||
* @param mixed $filename
|
||||
* @return Zend_Auth_Adapter_Digest Provides a fluent interface
|
||||
*/
|
||||
public function setFilename($filename)
|
||||
{
|
||||
$this->_filename = (string) $filename;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the realm option value or null if it has not yet been set
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRealm()
|
||||
{
|
||||
return $this->_realm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the realm option value
|
||||
*
|
||||
* @param mixed $realm
|
||||
* @return Zend_Auth_Adapter_Digest Provides a fluent interface
|
||||
*/
|
||||
public function setRealm($realm)
|
||||
{
|
||||
$this->_realm = (string) $realm;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the username option value or null if it has not yet been set
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->_username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the username option value
|
||||
*
|
||||
* @param mixed $username
|
||||
* @return Zend_Auth_Adapter_Digest Provides a fluent interface
|
||||
*/
|
||||
public function setUsername($username)
|
||||
{
|
||||
$this->_username = (string) $username;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the password option value or null if it has not yet been set
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPassword()
|
||||
{
|
||||
return $this->_password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the password option value
|
||||
*
|
||||
* @param mixed $password
|
||||
* @return Zend_Auth_Adapter_Digest Provides a fluent interface
|
||||
*/
|
||||
public function setPassword($password)
|
||||
{
|
||||
$this->_password = (string) $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Auth_Adapter_Interface
|
||||
*
|
||||
* @throws Zend_Auth_Adapter_Exception
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
public function authenticate()
|
||||
{
|
||||
$optionsRequired = array('filename', 'realm', 'username', 'password');
|
||||
foreach ($optionsRequired as $optionRequired) {
|
||||
if (null === $this->{"_$optionRequired"}) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception("Option '$optionRequired' must be set before authentication");
|
||||
}
|
||||
}
|
||||
|
||||
if (false === ($fileHandle = @fopen($this->_filename, 'r'))) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception("Cannot open '$this->_filename' for reading");
|
||||
}
|
||||
|
||||
$id = "$this->_username:$this->_realm";
|
||||
$idLength = strlen($id);
|
||||
|
||||
$result = array(
|
||||
'code' => Zend_Auth_Result::FAILURE,
|
||||
'identity' => array(
|
||||
'realm' => $this->_realm,
|
||||
'username' => $this->_username,
|
||||
),
|
||||
'messages' => array()
|
||||
);
|
||||
|
||||
while ($line = trim(fgets($fileHandle))) {
|
||||
if (substr($line, 0, $idLength) === $id) {
|
||||
if (substr($line, -32) === md5("$this->_username:$this->_realm:$this->_password")) {
|
||||
$result['code'] = Zend_Auth_Result::SUCCESS;
|
||||
} else {
|
||||
$result['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
|
||||
$result['messages'][] = 'Password incorrect';
|
||||
}
|
||||
return new Zend_Auth_Result($result['code'], $result['identity'], $result['messages']);
|
||||
}
|
||||
}
|
||||
|
||||
$result['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
|
||||
$result['messages'][] = "Username '$this->_username' and realm '$this->_realm' combination not found";
|
||||
return new Zend_Auth_Result($result['code'], $result['identity'], $result['messages']);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Zend_Auth_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Adapter_Exception extends Zend_Auth_Exception
|
||||
{}
|
||||
@@ -1,847 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter_Http
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* HTTP Authentication Adapter
|
||||
*
|
||||
* Implements a pretty good chunk of RFC 2617.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter_Http
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @todo Support auth-int
|
||||
* @todo Track nonces, nonce-count, opaque for replay protection and stale support
|
||||
* @todo Support Authentication-Info header
|
||||
*/
|
||||
class Zend_Auth_Adapter_Http implements Zend_Auth_Adapter_Interface
|
||||
{
|
||||
/**
|
||||
* Reference to the HTTP Request object
|
||||
*
|
||||
* @var Zend_Controller_Request_Http
|
||||
*/
|
||||
protected $_request;
|
||||
|
||||
/**
|
||||
* Reference to the HTTP Response object
|
||||
*
|
||||
* @var Zend_Controller_Response_Http
|
||||
*/
|
||||
protected $_response;
|
||||
|
||||
/**
|
||||
* Object that looks up user credentials for the Basic scheme
|
||||
*
|
||||
* @var Zend_Auth_Adapter_Http_Resolver_Interface
|
||||
*/
|
||||
protected $_basicResolver;
|
||||
|
||||
/**
|
||||
* Object that looks up user credentials for the Digest scheme
|
||||
*
|
||||
* @var Zend_Auth_Adapter_Http_Resolver_Interface
|
||||
*/
|
||||
protected $_digestResolver;
|
||||
|
||||
/**
|
||||
* List of authentication schemes supported by this class
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_supportedSchemes = array('basic', 'digest');
|
||||
|
||||
/**
|
||||
* List of schemes this class will accept from the client
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_acceptSchemes;
|
||||
|
||||
/**
|
||||
* Space-delimited list of protected domains for Digest Auth
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_domains;
|
||||
|
||||
/**
|
||||
* The protection realm to use
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_realm;
|
||||
|
||||
/**
|
||||
* Nonce timeout period
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $_nonceTimeout;
|
||||
|
||||
/**
|
||||
* Whether to send the opaque value in the header. True by default
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_useOpaque;
|
||||
|
||||
/**
|
||||
* List of the supported digest algorithms. I want to support both MD5 and
|
||||
* MD5-sess, but MD5-sess won't make it into the first version.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_supportedAlgos = array('MD5');
|
||||
|
||||
/**
|
||||
* The actual algorithm to use. Defaults to MD5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_algo;
|
||||
|
||||
/**
|
||||
* List of supported qop options. My intetion is to support both 'auth' and
|
||||
* 'auth-int', but 'auth-int' won't make it into the first version.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_supportedQops = array('auth');
|
||||
|
||||
/**
|
||||
* Whether or not to do Proxy Authentication instead of origin server
|
||||
* authentication (send 407's instead of 401's). Off by default.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_imaProxy;
|
||||
|
||||
/**
|
||||
* Flag indicating the client is IE and didn't bother to return the opaque string
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_ieNoOpaque;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $config Configuration settings:
|
||||
* 'accept_schemes' => 'basic'|'digest'|'basic digest'
|
||||
* 'realm' => <string>
|
||||
* 'digest_domains' => <string> Space-delimited list of URIs
|
||||
* 'nonce_timeout' => <int>
|
||||
* 'use_opaque' => <bool> Whether to send the opaque value in the header
|
||||
* 'alogrithm' => <string> See $_supportedAlgos. Default: MD5
|
||||
* 'proxy_auth' => <bool> Whether to do authentication as a Proxy
|
||||
* @throws Zend_Auth_Adapter_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!extension_loaded('hash')) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception(__CLASS__ . ' requires the \'hash\' extension');
|
||||
}
|
||||
|
||||
$this->_request = null;
|
||||
$this->_response = null;
|
||||
$this->_ieNoOpaque = false;
|
||||
|
||||
|
||||
if (empty($config['accept_schemes'])) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Config key \'accept_schemes\' is required');
|
||||
}
|
||||
|
||||
$schemes = explode(' ', $config['accept_schemes']);
|
||||
$this->_acceptSchemes = array_intersect($schemes, $this->_supportedSchemes);
|
||||
if (empty($this->_acceptSchemes)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('No supported schemes given in \'accept_schemes\'. Valid values: '
|
||||
. implode(', ', $this->_supportedSchemes));
|
||||
}
|
||||
|
||||
// Double-quotes are used to delimit the realm string in the HTTP header,
|
||||
// and colons are field delimiters in the password file.
|
||||
if (empty($config['realm']) ||
|
||||
!ctype_print($config['realm']) ||
|
||||
strpos($config['realm'], ':') !== false ||
|
||||
strpos($config['realm'], '"') !== false) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Config key \'realm\' is required, and must contain only printable '
|
||||
. 'characters, excluding quotation marks and colons');
|
||||
} else {
|
||||
$this->_realm = $config['realm'];
|
||||
}
|
||||
|
||||
if (in_array('digest', $this->_acceptSchemes)) {
|
||||
if (empty($config['digest_domains']) ||
|
||||
!ctype_print($config['digest_domains']) ||
|
||||
strpos($config['digest_domains'], '"') !== false) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Config key \'digest_domains\' is required, and must contain '
|
||||
. 'only printable characters, excluding quotation marks');
|
||||
} else {
|
||||
$this->_domains = $config['digest_domains'];
|
||||
}
|
||||
|
||||
if (empty($config['nonce_timeout']) ||
|
||||
!is_numeric($config['nonce_timeout'])) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Config key \'nonce_timeout\' is required, and must be an '
|
||||
. 'integer');
|
||||
} else {
|
||||
$this->_nonceTimeout = (int) $config['nonce_timeout'];
|
||||
}
|
||||
|
||||
// We use the opaque value unless explicitly told not to
|
||||
if (isset($config['use_opaque']) && false == (bool) $config['use_opaque']) {
|
||||
$this->_useOpaque = false;
|
||||
} else {
|
||||
$this->_useOpaque = true;
|
||||
}
|
||||
|
||||
if (isset($config['algorithm']) && in_array($config['algorithm'], $this->_supportedAlgos)) {
|
||||
$this->_algo = $config['algorithm'];
|
||||
} else {
|
||||
$this->_algo = 'MD5';
|
||||
}
|
||||
}
|
||||
|
||||
// Don't be a proxy unless explicitly told to do so
|
||||
if (isset($config['proxy_auth']) && true == (bool) $config['proxy_auth']) {
|
||||
$this->_imaProxy = true; // I'm a Proxy
|
||||
} else {
|
||||
$this->_imaProxy = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the _basicResolver property
|
||||
*
|
||||
* @param Zend_Auth_Adapter_Http_Resolver_Interface $resolver
|
||||
* @return Zend_Auth_Adapter_Http Provides a fluent interface
|
||||
*/
|
||||
public function setBasicResolver(Zend_Auth_Adapter_Http_Resolver_Interface $resolver)
|
||||
{
|
||||
$this->_basicResolver = $resolver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the _basicResolver property
|
||||
*
|
||||
* @return Zend_Auth_Adapter_Http_Resolver_Interface
|
||||
*/
|
||||
public function getBasicResolver()
|
||||
{
|
||||
return $this->_basicResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the _digestResolver property
|
||||
*
|
||||
* @param Zend_Auth_Adapter_Http_Resolver_Interface $resolver
|
||||
* @return Zend_Auth_Adapter_Http Provides a fluent interface
|
||||
*/
|
||||
public function setDigestResolver(Zend_Auth_Adapter_Http_Resolver_Interface $resolver)
|
||||
{
|
||||
$this->_digestResolver = $resolver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the _digestResolver property
|
||||
*
|
||||
* @return Zend_Auth_Adapter_Http_Resolver_Interface
|
||||
*/
|
||||
public function getDigestResolver()
|
||||
{
|
||||
return $this->_digestResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the Request object
|
||||
*
|
||||
* @param Zend_Controller_Request_Http $request
|
||||
* @return Zend_Auth_Adapter_Http Provides a fluent interface
|
||||
*/
|
||||
public function setRequest(Zend_Controller_Request_Http $request)
|
||||
{
|
||||
$this->_request = $request;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the Request object
|
||||
*
|
||||
* @return Zend_Controller_Request_Http
|
||||
*/
|
||||
public function getRequest()
|
||||
{
|
||||
return $this->_request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the Response object
|
||||
*
|
||||
* @param Zend_Controller_Response_Http $response
|
||||
* @return Zend_Auth_Adapter_Http Provides a fluent interface
|
||||
*/
|
||||
public function setResponse(Zend_Controller_Response_Http $response)
|
||||
{
|
||||
$this->_response = $response;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the Response object
|
||||
*
|
||||
* @return Zend_Controller_Response_Http
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->_response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate
|
||||
*
|
||||
* @throws Zend_Auth_Adapter_Exception
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
public function authenticate()
|
||||
{
|
||||
if (empty($this->_request) ||
|
||||
empty($this->_response)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Request and Response objects must be set before calling '
|
||||
. 'authenticate()');
|
||||
}
|
||||
|
||||
if ($this->_imaProxy) {
|
||||
$getHeader = 'Proxy-Authorization';
|
||||
} else {
|
||||
$getHeader = 'Authorization';
|
||||
}
|
||||
|
||||
$authHeader = $this->_request->getHeader($getHeader);
|
||||
if (!$authHeader) {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
|
||||
list($clientScheme) = explode(' ', $authHeader);
|
||||
$clientScheme = strtolower($clientScheme);
|
||||
|
||||
// The server can issue multiple challenges, but the client should
|
||||
// answer with only the selected auth scheme.
|
||||
if (!in_array($clientScheme, $this->_supportedSchemes)) {
|
||||
$this->_response->setHttpResponseCode(400);
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE_UNCATEGORIZED,
|
||||
array(),
|
||||
array('Client requested an incorrect or unsupported authentication scheme')
|
||||
);
|
||||
}
|
||||
|
||||
// client sent a scheme that is not the one required
|
||||
if (!in_array($clientScheme, $this->_acceptSchemes)) {
|
||||
// challenge again the client
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
|
||||
switch ($clientScheme) {
|
||||
case 'basic':
|
||||
$result = $this->_basicAuth($authHeader);
|
||||
break;
|
||||
case 'digest':
|
||||
$result = $this->_digestAuth($authHeader);
|
||||
break;
|
||||
default:
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Unsupported authentication scheme');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Challenge Client
|
||||
*
|
||||
* Sets a 401 or 407 Unauthorized response code, and creates the
|
||||
* appropriate Authenticate header(s) to prompt for credentials.
|
||||
*
|
||||
* @return Zend_Auth_Result Always returns a non-identity Auth result
|
||||
*/
|
||||
protected function _challengeClient()
|
||||
{
|
||||
if ($this->_imaProxy) {
|
||||
$statusCode = 407;
|
||||
$headerName = 'Proxy-Authenticate';
|
||||
} else {
|
||||
$statusCode = 401;
|
||||
$headerName = 'WWW-Authenticate';
|
||||
}
|
||||
|
||||
$this->_response->setHttpResponseCode($statusCode);
|
||||
|
||||
// Send a challenge in each acceptable authentication scheme
|
||||
if (in_array('basic', $this->_acceptSchemes)) {
|
||||
$this->_response->setHeader($headerName, $this->_basicHeader());
|
||||
}
|
||||
if (in_array('digest', $this->_acceptSchemes)) {
|
||||
$this->_response->setHeader($headerName, $this->_digestHeader());
|
||||
}
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
|
||||
array(),
|
||||
array('Invalid or absent credentials; challenging client')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic Header
|
||||
*
|
||||
* Generates a Proxy- or WWW-Authenticate header value in the Basic
|
||||
* authentication scheme.
|
||||
*
|
||||
* @return string Authenticate header value
|
||||
*/
|
||||
protected function _basicHeader()
|
||||
{
|
||||
return 'Basic realm="' . $this->_realm . '"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Digest Header
|
||||
*
|
||||
* Generates a Proxy- or WWW-Authenticate header value in the Digest
|
||||
* authentication scheme.
|
||||
*
|
||||
* @return string Authenticate header value
|
||||
*/
|
||||
protected function _digestHeader()
|
||||
{
|
||||
$wwwauth = 'Digest realm="' . $this->_realm . '", '
|
||||
. 'domain="' . $this->_domains . '", '
|
||||
. 'nonce="' . $this->_calcNonce() . '", '
|
||||
. ($this->_useOpaque ? 'opaque="' . $this->_calcOpaque() . '", ' : '')
|
||||
. 'algorithm="' . $this->_algo . '", '
|
||||
. 'qop="' . implode(',', $this->_supportedQops) . '"';
|
||||
|
||||
return $wwwauth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic Authentication
|
||||
*
|
||||
* @param string $header Client's Authorization header
|
||||
* @throws Zend_Auth_Adapter_Exception
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
protected function _basicAuth($header)
|
||||
{
|
||||
if (empty($header)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('The value of the client Authorization header is required');
|
||||
}
|
||||
if (empty($this->_basicResolver)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('A basicResolver object must be set before doing Basic '
|
||||
. 'authentication');
|
||||
}
|
||||
|
||||
// Decode the Authorization header
|
||||
$auth = substr($header, strlen('Basic '));
|
||||
$auth = base64_decode($auth);
|
||||
if (!$auth) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Unable to base64_decode Authorization header value');
|
||||
}
|
||||
|
||||
// See ZF-1253. Validate the credentials the same way the digest
|
||||
// implementation does. If invalid credentials are detected,
|
||||
// re-challenge the client.
|
||||
if (!ctype_print($auth)) {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
// Fix for ZF-1515: Now re-challenges on empty username or password
|
||||
$creds = array_filter(explode(':', $auth));
|
||||
if (count($creds) != 2) {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
|
||||
$password = $this->_basicResolver->resolve($creds[0], $this->_realm);
|
||||
if ($password && $password == $creds[1]) {
|
||||
$identity = array('username'=>$creds[0], 'realm'=>$this->_realm);
|
||||
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
|
||||
} else {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Digest Authentication
|
||||
*
|
||||
* @param string $header Client's Authorization header
|
||||
* @throws Zend_Auth_Adapter_Exception
|
||||
* @return Zend_Auth_Result Valid auth result only on successful auth
|
||||
*/
|
||||
protected function _digestAuth($header)
|
||||
{
|
||||
if (empty($header)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('The value of the client Authorization header is required');
|
||||
}
|
||||
if (empty($this->_digestResolver)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('A digestResolver object must be set before doing Digest authentication');
|
||||
}
|
||||
|
||||
$data = $this->_parseDigestAuth($header);
|
||||
if ($data === false) {
|
||||
$this->_response->setHttpResponseCode(400);
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE_UNCATEGORIZED,
|
||||
array(),
|
||||
array('Invalid Authorization header format')
|
||||
);
|
||||
}
|
||||
|
||||
// See ZF-1052. This code was a bit too unforgiving of invalid
|
||||
// usernames. Now, if the username is bad, we re-challenge the client.
|
||||
if ('::invalid::' == $data['username']) {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
|
||||
// Verify that the client sent back the same nonce
|
||||
if ($this->_calcNonce() != $data['nonce']) {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
// The opaque value is also required to match, but of course IE doesn't
|
||||
// play ball.
|
||||
if (!$this->_ieNoOpaque && $this->_calcOpaque() != $data['opaque']) {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
|
||||
// Look up the user's password hash. If not found, deny access.
|
||||
// This makes no assumptions about how the password hash was
|
||||
// constructed beyond that it must have been built in such a way as
|
||||
// to be recreatable with the current settings of this object.
|
||||
$ha1 = $this->_digestResolver->resolve($data['username'], $data['realm']);
|
||||
if ($ha1 === false) {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
|
||||
// If MD5-sess is used, a1 value is made of the user's password
|
||||
// hash with the server and client nonce appended, separated by
|
||||
// colons.
|
||||
if ($this->_algo == 'MD5-sess') {
|
||||
$ha1 = hash('md5', $ha1 . ':' . $data['nonce'] . ':' . $data['cnonce']);
|
||||
}
|
||||
|
||||
// Calculate h(a2). The value of this hash depends on the qop
|
||||
// option selected by the client and the supported hash functions
|
||||
switch ($data['qop']) {
|
||||
case 'auth':
|
||||
$a2 = $this->_request->getMethod() . ':' . $data['uri'];
|
||||
break;
|
||||
case 'auth-int':
|
||||
// Should be REQUEST_METHOD . ':' . uri . ':' . hash(entity-body),
|
||||
// but this isn't supported yet, so fall through to default case
|
||||
default:
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Client requested an unsupported qop option');
|
||||
}
|
||||
// Using hash() should make parameterizing the hash algorithm
|
||||
// easier
|
||||
$ha2 = hash('md5', $a2);
|
||||
|
||||
|
||||
// Calculate the server's version of the request-digest. This must
|
||||
// match $data['response']. See RFC 2617, section 3.2.2.1
|
||||
$message = $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' . $ha2;
|
||||
$digest = hash('md5', $ha1 . ':' . $message);
|
||||
|
||||
// If our digest matches the client's let them in, otherwise return
|
||||
// a 401 code and exit to prevent access to the protected resource.
|
||||
if ($digest == $data['response']) {
|
||||
$identity = array('username'=>$data['username'], 'realm'=>$data['realm']);
|
||||
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
|
||||
} else {
|
||||
return $this->_challengeClient();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Nonce
|
||||
*
|
||||
* @return string The nonce value
|
||||
*/
|
||||
protected function _calcNonce()
|
||||
{
|
||||
// Once subtle consequence of this timeout calculation is that it
|
||||
// actually divides all of time into _nonceTimeout-sized sections, such
|
||||
// that the value of timeout is the point in time of the next
|
||||
// approaching "boundary" of a section. This allows the server to
|
||||
// consistently generate the same timeout (and hence the same nonce
|
||||
// value) across requests, but only as long as one of those
|
||||
// "boundaries" is not crossed between requests. If that happens, the
|
||||
// nonce will change on its own, and effectively log the user out. This
|
||||
// would be surprising if the user just logged in.
|
||||
$timeout = ceil(time() / $this->_nonceTimeout) * $this->_nonceTimeout;
|
||||
|
||||
$nonce = hash('md5', $timeout . ':' . $this->_request->getServer('HTTP_USER_AGENT') . ':' . __CLASS__);
|
||||
return $nonce;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Opaque
|
||||
*
|
||||
* The opaque string can be anything; the client must return it exactly as
|
||||
* it was sent. It may be useful to store data in this string in some
|
||||
* applications. Ideally, a new value for this would be generated each time
|
||||
* a WWW-Authenticate header is sent (in order to reduce predictability),
|
||||
* but we would have to be able to create the same exact value across at
|
||||
* least two separate requests from the same client.
|
||||
*
|
||||
* @return string The opaque value
|
||||
*/
|
||||
protected function _calcOpaque()
|
||||
{
|
||||
return hash('md5', 'Opaque Data:' . __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse Digest Authorization header
|
||||
*
|
||||
* @param string $header Client's Authorization: HTTP header
|
||||
* @return array|false Data elements from header, or false if any part of
|
||||
* the header is invalid
|
||||
*/
|
||||
protected function _parseDigestAuth($header)
|
||||
{
|
||||
$temp = null;
|
||||
$data = array();
|
||||
|
||||
// See ZF-1052. Detect invalid usernames instead of just returning a
|
||||
// 400 code.
|
||||
$ret = preg_match('/username="([^"]+)"/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])
|
||||
|| !ctype_print($temp[1])
|
||||
|| strpos($temp[1], ':') !== false) {
|
||||
$data['username'] = '::invalid::';
|
||||
} else {
|
||||
$data['username'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
$ret = preg_match('/realm="([^"]+)"/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])) {
|
||||
return false;
|
||||
}
|
||||
if (!ctype_print($temp[1]) || strpos($temp[1], ':') !== false) {
|
||||
return false;
|
||||
} else {
|
||||
$data['realm'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
$ret = preg_match('/nonce="([^"]+)"/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])) {
|
||||
return false;
|
||||
}
|
||||
if (!ctype_xdigit($temp[1])) {
|
||||
return false;
|
||||
} else {
|
||||
$data['nonce'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
$ret = preg_match('/uri="([^"]+)"/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])) {
|
||||
return false;
|
||||
}
|
||||
// Section 3.2.2.5 in RFC 2617 says the authenticating server must
|
||||
// verify that the URI field in the Authorization header is for the
|
||||
// same resource requested in the Request Line.
|
||||
$rUri = @parse_url($this->_request->getRequestUri());
|
||||
$cUri = @parse_url($temp[1]);
|
||||
if (false === $rUri || false === $cUri) {
|
||||
return false;
|
||||
} else {
|
||||
// Make sure the path portion of both URIs is the same
|
||||
if ($rUri['path'] != $cUri['path']) {
|
||||
return false;
|
||||
}
|
||||
// Section 3.2.2.5 seems to suggest that the value of the URI
|
||||
// Authorization field should be made into an absolute URI if the
|
||||
// Request URI is absolute, but it's vague, and that's a bunch of
|
||||
// code I don't want to write right now.
|
||||
$data['uri'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
$ret = preg_match('/response="([^"]+)"/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])) {
|
||||
return false;
|
||||
}
|
||||
if (32 != strlen($temp[1]) || !ctype_xdigit($temp[1])) {
|
||||
return false;
|
||||
} else {
|
||||
$data['response'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
// The spec says this should default to MD5 if omitted. OK, so how does
|
||||
// that square with the algo we send out in the WWW-Authenticate header,
|
||||
// if it can easily be overridden by the client?
|
||||
$ret = preg_match('/algorithm="?(' . $this->_algo . ')"?/', $header, $temp);
|
||||
if ($ret && !empty($temp[1])
|
||||
&& in_array($temp[1], $this->_supportedAlgos)) {
|
||||
$data['algorithm'] = $temp[1];
|
||||
} else {
|
||||
$data['algorithm'] = 'MD5'; // = $this->_algo; ?
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
// Not optional in this implementation
|
||||
$ret = preg_match('/cnonce="([^"]+)"/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])) {
|
||||
return false;
|
||||
}
|
||||
if (!ctype_print($temp[1])) {
|
||||
return false;
|
||||
} else {
|
||||
$data['cnonce'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
// If the server sent an opaque value, the client must send it back
|
||||
if ($this->_useOpaque) {
|
||||
$ret = preg_match('/opaque="([^"]+)"/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])) {
|
||||
|
||||
// Big surprise: IE isn't RFC 2617-compliant.
|
||||
if (false !== strpos($this->_request->getHeader('User-Agent'), 'MSIE')) {
|
||||
$temp[1] = '';
|
||||
$this->_ieNoOpaque = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// This implementation only sends MD5 hex strings in the opaque value
|
||||
if (!$this->_ieNoOpaque &&
|
||||
(32 != strlen($temp[1]) || !ctype_xdigit($temp[1]))) {
|
||||
return false;
|
||||
} else {
|
||||
$data['opaque'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
}
|
||||
|
||||
// Not optional in this implementation, but must be one of the supported
|
||||
// qop types
|
||||
$ret = preg_match('/qop="?(' . implode('|', $this->_supportedQops) . ')"?/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])) {
|
||||
return false;
|
||||
}
|
||||
if (!in_array($temp[1], $this->_supportedQops)) {
|
||||
return false;
|
||||
} else {
|
||||
$data['qop'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
// Not optional in this implementation. The spec says this value
|
||||
// shouldn't be a quoted string, but apparently some implementations
|
||||
// quote it anyway. See ZF-1544.
|
||||
$ret = preg_match('/nc="?([0-9A-Fa-f]{8})"?/', $header, $temp);
|
||||
if (!$ret || empty($temp[1])) {
|
||||
return false;
|
||||
}
|
||||
if (8 != strlen($temp[1]) || !ctype_xdigit($temp[1])) {
|
||||
return false;
|
||||
} else {
|
||||
$data['nc'] = $temp[1];
|
||||
}
|
||||
$temp = null;
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter_Http
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* HTTP Auth Resolver Exception
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter_Http
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Adapter_Http_Resolver_Exception extends Zend_Auth_Exception
|
||||
{}
|
||||
@@ -1,167 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter_Http
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Http_Resolver_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Http/Resolver/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* HTTP Authentication File Resolver
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter_Http
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Adapter_Http_Resolver_File implements Zend_Auth_Adapter_Http_Resolver_Interface
|
||||
{
|
||||
/**
|
||||
* Path to credentials file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_file;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $path Complete filename where the credentials are stored
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($path = '')
|
||||
{
|
||||
if (!empty($path)) {
|
||||
$this->setFile($path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the path to the credentials file
|
||||
*
|
||||
* @param string $path
|
||||
* @throws Zend_Auth_Adapter_Http_Resolver_Exception
|
||||
* @return Zend_Auth_Adapter_Http_Resolver_File Provides a fluent interface
|
||||
*/
|
||||
public function setFile($path)
|
||||
{
|
||||
if (empty($path) || !is_readable($path)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Http_Resolver_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Http_Resolver_Exception('Path not readable: ' . $path);
|
||||
}
|
||||
$this->_file = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path to the credentials file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFile()
|
||||
{
|
||||
return $this->_file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve credentials
|
||||
*
|
||||
* Only the first matching username/realm combination in the file is
|
||||
* returned. If the file contains credentials for Digest authentication,
|
||||
* the returned string is the password hash, or h(a1) from RFC 2617. The
|
||||
* returned string is the plain-text password for Basic authentication.
|
||||
*
|
||||
* The expected format of the file is:
|
||||
* username:realm:sharedSecret
|
||||
*
|
||||
* That is, each line consists of the user's username, the applicable
|
||||
* authentication realm, and the password or hash, each delimited by
|
||||
* colons.
|
||||
*
|
||||
* @param string $username Username
|
||||
* @param string $realm Authentication Realm
|
||||
* @throws Zend_Auth_Adapter_Http_Resolver_Exception
|
||||
* @return string|false User's shared secret, if the user is found in the
|
||||
* realm, false otherwise.
|
||||
*/
|
||||
public function resolve($username, $realm)
|
||||
{
|
||||
if (empty($username)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Http_Resolver_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Http_Resolver_Exception('Username is required');
|
||||
} else if (!ctype_print($username) || strpos($username, ':') !== false) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Http_Resolver_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Http_Resolver_Exception('Username must consist only of printable characters, '
|
||||
. 'excluding the colon');
|
||||
}
|
||||
if (empty($realm)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Http_Resolver_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Http_Resolver_Exception('Realm is required');
|
||||
} else if (!ctype_print($realm) || strpos($realm, ':') !== false) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Http_Resolver_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Http_Resolver_Exception('Realm must consist only of printable characters, '
|
||||
. 'excluding the colon.');
|
||||
}
|
||||
|
||||
// Open file, read through looking for matching credentials
|
||||
$fp = @fopen($this->_file, 'r');
|
||||
if (!$fp) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Http_Resolver_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Http_Resolver_Exception('Unable to open password file: ' . $this->_file);
|
||||
}
|
||||
|
||||
// No real validation is done on the contents of the password file. The
|
||||
// assumption is that we trust the administrators to keep it secure.
|
||||
while (($line = fgetcsv($fp, 512, ':')) !== false) {
|
||||
if ($line[0] == $username && $line[1] == $realm) {
|
||||
$password = $line[2];
|
||||
fclose($fp);
|
||||
return $password;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($fp);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter_Http
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Auth HTTP Resolver Interface
|
||||
*
|
||||
* Defines an interace to resolve a username/realm combination into a shared
|
||||
* secret usable by HTTP Authentication.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter_Http
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Auth_Adapter_Http_Resolver_Interface
|
||||
{
|
||||
/**
|
||||
* Resolve username/realm to password/hash/etc.
|
||||
*
|
||||
* @param string $username Username
|
||||
* @param string $realm Authentication Realm
|
||||
* @return string|false User's shared secret, if the user is found in the
|
||||
* realm, false otherwise.
|
||||
*/
|
||||
public function resolve($username, $realm);
|
||||
}
|
||||
@@ -1,261 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Interface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Result
|
||||
*/
|
||||
require_once 'Zend/Auth/Result.php';
|
||||
|
||||
/**
|
||||
* @see Zend_InfoCard
|
||||
*/
|
||||
require_once 'Zend/InfoCard.php';
|
||||
|
||||
/**
|
||||
* A Zend_Auth Authentication Adapter allowing the use of Information Cards as an
|
||||
* authentication mechanism
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Adapter_InfoCard implements Zend_Auth_Adapter_Interface
|
||||
{
|
||||
/**
|
||||
* The XML Token being authenticated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_xmlToken;
|
||||
|
||||
/**
|
||||
* The instance of Zend_InfoCard
|
||||
*
|
||||
* @var Zend_InfoCard
|
||||
*/
|
||||
protected $_infoCard;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $strXmlDocument The XML Token provided by the client
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($strXmlDocument)
|
||||
{
|
||||
$this->_xmlToken = $strXmlDocument;
|
||||
$this->_infoCard = new Zend_InfoCard();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the InfoCard component Adapter to use
|
||||
*
|
||||
* @param Zend_InfoCard_Adapter_Interface $a
|
||||
* @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
|
||||
*/
|
||||
public function setAdapter(Zend_InfoCard_Adapter_Interface $a)
|
||||
{
|
||||
$this->_infoCard->setAdapter($a);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the InfoCard component adapter being used
|
||||
*
|
||||
* @return Zend_InfoCard_Adapter_Interface
|
||||
*/
|
||||
public function getAdapter()
|
||||
{
|
||||
return $this->_infoCard->getAdapter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the InfoCard public key cipher object being used
|
||||
*
|
||||
* @return Zend_InfoCard_Cipher_PKI_Interface
|
||||
*/
|
||||
public function getPKCipherObject()
|
||||
{
|
||||
return $this->_infoCard->getPKCipherObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the InfoCard public key cipher object to use
|
||||
*
|
||||
* @param Zend_InfoCard_Cipher_PKI_Interface $cipherObj
|
||||
* @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
|
||||
*/
|
||||
public function setPKICipherObject(Zend_InfoCard_Cipher_PKI_Interface $cipherObj)
|
||||
{
|
||||
$this->_infoCard->setPKICipherObject($cipherObj);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the Symmetric cipher object being used
|
||||
*
|
||||
* @return Zend_InfoCard_Cipher_Symmetric_Interface
|
||||
*/
|
||||
public function getSymCipherObject()
|
||||
{
|
||||
return $this->_infoCard->getSymCipherObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the InfoCard symmetric cipher object to use
|
||||
*
|
||||
* @param Zend_InfoCard_Cipher_Symmetric_Interface $cipherObj
|
||||
* @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
|
||||
*/
|
||||
public function setSymCipherObject(Zend_InfoCard_Cipher_Symmetric_Interface $cipherObj)
|
||||
{
|
||||
$this->_infoCard->setSymCipherObject($cipherObj);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a Certificate Pair by Key ID from the search list
|
||||
*
|
||||
* @param string $key_id The Certificate Key ID returned from adding the certificate pair
|
||||
* @throws Zend_InfoCard_Exception
|
||||
* @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
|
||||
*/
|
||||
public function removeCertificatePair($key_id)
|
||||
{
|
||||
$this->_infoCard->removeCertificatePair($key_id);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Certificate Pair to the list of certificates searched by the component
|
||||
*
|
||||
* @param string $private_key_file The path to the private key file for the pair
|
||||
* @param string $public_key_file The path to the certificate / public key for the pair
|
||||
* @param string $type (optional) The URI for the type of key pair this is (default RSA with OAEP padding)
|
||||
* @param string $password (optional) The password for the private key file if necessary
|
||||
* @throws Zend_InfoCard_Exception
|
||||
* @return string A key ID representing this key pair in the component
|
||||
*/
|
||||
public function addCertificatePair($private_key_file, $public_key_file, $type = Zend_InfoCard_Cipher::ENC_RSA_OAEP_MGF1P, $password = null)
|
||||
{
|
||||
return $this->_infoCard->addCertificatePair($private_key_file, $public_key_file, $type, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a Certificate Pair from a key ID
|
||||
*
|
||||
* @param string $key_id The Key ID of the certificate pair in the component
|
||||
* @throws Zend_InfoCard_Exception
|
||||
* @return array An array containing the path to the private/public key files,
|
||||
* the type URI and the password if provided
|
||||
*/
|
||||
public function getCertificatePair($key_id)
|
||||
{
|
||||
return $this->_infoCard->getCertificatePair($key_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the XML Token to be processed
|
||||
*
|
||||
* @param string $strXmlToken The XML token to process
|
||||
* @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
|
||||
*/
|
||||
public function setXmlToken($strXmlToken)
|
||||
{
|
||||
$this->_xmlToken = $strXmlToken;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the XML Token being processed
|
||||
*
|
||||
* @return string The XML token to be processed
|
||||
*/
|
||||
public function getXmlToken()
|
||||
{
|
||||
return $this->_xmlToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticates the XML token
|
||||
*
|
||||
* @return Zend_Auth_Result The result of the authentication
|
||||
*/
|
||||
public function authenticate()
|
||||
{
|
||||
try {
|
||||
$claims = $this->_infoCard->process($this->getXmlToken());
|
||||
} catch(Exception $e) {
|
||||
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE , null, array('Exception Thrown',
|
||||
$e->getMessage(),
|
||||
$e->getTraceAsString(),
|
||||
serialize($e)));
|
||||
}
|
||||
|
||||
if(!$claims->isValid()) {
|
||||
switch($claims->getCode()) {
|
||||
case Zend_infoCard_Claims::RESULT_PROCESSING_FAILURE:
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE,
|
||||
$claims,
|
||||
array(
|
||||
'Processing Failure',
|
||||
$claims->getErrorMsg()
|
||||
)
|
||||
);
|
||||
break;
|
||||
case Zend_InfoCard_Claims::RESULT_VALIDATION_FAILURE:
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
|
||||
$claims,
|
||||
array(
|
||||
'Validation Failure',
|
||||
$claims->getErrorMsg()
|
||||
)
|
||||
);
|
||||
break;
|
||||
default:
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE,
|
||||
$claims,
|
||||
array(
|
||||
'Unknown Failure',
|
||||
$claims->getErrorMsg()
|
||||
)
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::SUCCESS,
|
||||
$claims
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Result
|
||||
*/
|
||||
require_once 'Zend/Auth/Result.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Auth_Adapter_Interface
|
||||
{
|
||||
/**
|
||||
* Performs an authentication attempt
|
||||
*
|
||||
* @throws Zend_Auth_Adapter_Exception If authentication cannot be performed
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
public function authenticate();
|
||||
}
|
||||
@@ -1,512 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Interface.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Adapter_Ldap implements Zend_Auth_Adapter_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* The Zend_Ldap context.
|
||||
*
|
||||
* @var Zend_Ldap
|
||||
*/
|
||||
protected $_ldap = null;
|
||||
|
||||
/**
|
||||
* The array of arrays of Zend_Ldap options passed to the constructor.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_options = null;
|
||||
|
||||
/**
|
||||
* The username of the account being authenticated.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_username = null;
|
||||
|
||||
/**
|
||||
* The password of the account being authenticated.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_password = null;
|
||||
|
||||
/**
|
||||
* The DN of the authenticated account. Used to retrieve the account entry on request.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_authenticatedDn = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options An array of arrays of Zend_Ldap options
|
||||
* @param string $username The username of the account being authenticated
|
||||
* @param string $password The password of the account being authenticated
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array(), $username = null, $password = null)
|
||||
{
|
||||
$this->setOptions($options);
|
||||
if ($username !== null) {
|
||||
$this->setUsername($username);
|
||||
}
|
||||
if ($password !== null) {
|
||||
$this->setPassword($password);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the array of arrays of Zend_Ldap options of this adapter.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the array of arrays of Zend_Ldap options to be used by
|
||||
* this adapter.
|
||||
*
|
||||
* @param array $options The array of arrays of Zend_Ldap options
|
||||
* @return Zend_Auth_Adapter_Ldap Provides a fluent interface
|
||||
*/
|
||||
public function setOptions($options)
|
||||
{
|
||||
$this->_options = is_array($options) ? $options : array();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the username of the account being authenticated, or
|
||||
* NULL if none is set.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->_username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the username for binding
|
||||
*
|
||||
* @param string $username The username for binding
|
||||
* @return Zend_Auth_Adapter_Ldap Provides a fluent interface
|
||||
*/
|
||||
public function setUsername($username)
|
||||
{
|
||||
$this->_username = (string) $username;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the password of the account being authenticated, or
|
||||
* NULL if none is set.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPassword()
|
||||
{
|
||||
return $this->_password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the passwort for the account
|
||||
*
|
||||
* @param string $password The password of the account being authenticated
|
||||
* @return Zend_Auth_Adapter_Ldap Provides a fluent interface
|
||||
*/
|
||||
public function setPassword($password)
|
||||
{
|
||||
$this->_password = (string) $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setIdentity() - set the identity (username) to be used
|
||||
*
|
||||
* Proxies to {@see setPassword()}
|
||||
*
|
||||
* Closes ZF-6813
|
||||
*
|
||||
* @param string $identity
|
||||
* @return Zend_Auth_Adapter_Ldap Provides a fluent interface
|
||||
*/
|
||||
public function setIdentity($identity)
|
||||
{
|
||||
return $this->setUsername($identity);
|
||||
}
|
||||
|
||||
/**
|
||||
* setCredential() - set the credential (password) value to be used
|
||||
*
|
||||
* Proxies to {@see setPassword()}
|
||||
*
|
||||
* Closes ZF-6813
|
||||
*
|
||||
* @param string $credential
|
||||
* @return Zend_Auth_Adapter_Ldap Provides a fluent interface
|
||||
*/
|
||||
public function setCredential($credential)
|
||||
{
|
||||
return $this->setPassword($credential);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the LDAP Object
|
||||
*
|
||||
* @return Zend_Ldap The Zend_Ldap object used to authenticate the credentials
|
||||
*/
|
||||
public function getLdap()
|
||||
{
|
||||
if ($this->_ldap === null) {
|
||||
/**
|
||||
* @see Zend_Ldap
|
||||
*/
|
||||
require_once 'Zend/Ldap.php';
|
||||
$this->_ldap = new Zend_Ldap();
|
||||
}
|
||||
|
||||
return $this->_ldap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an Ldap connection
|
||||
*
|
||||
* @param Zend_Ldap $ldap An existing Ldap object
|
||||
* @return Zend_Auth_Adapter_Ldap Provides a fluent interface
|
||||
*/
|
||||
public function setLdap(Zend_Ldap $ldap)
|
||||
{
|
||||
$this->_ldap = $ldap;
|
||||
|
||||
$this->setOptions(array($ldap->getOptions()));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a domain name for the current LDAP options. This is used
|
||||
* for skipping redundant operations (e.g. authentications).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _getAuthorityName()
|
||||
{
|
||||
$options = $this->getLdap()->getOptions();
|
||||
$name = $options['accountDomainName'];
|
||||
if (!$name)
|
||||
$name = $options['accountDomainNameShort'];
|
||||
return $name ? $name : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate the user
|
||||
*
|
||||
* @throws Zend_Auth_Adapter_Exception
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
public function authenticate()
|
||||
{
|
||||
/**
|
||||
* @see Zend_Ldap_Exception
|
||||
*/
|
||||
require_once 'Zend/Ldap/Exception.php';
|
||||
|
||||
$messages = array();
|
||||
$messages[0] = ''; // reserved
|
||||
$messages[1] = ''; // reserved
|
||||
|
||||
$username = $this->_username;
|
||||
$password = $this->_password;
|
||||
|
||||
if (!$username) {
|
||||
$code = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
|
||||
$messages[0] = 'A username is required';
|
||||
return new Zend_Auth_Result($code, '', $messages);
|
||||
}
|
||||
if (!$password) {
|
||||
/* A password is required because some servers will
|
||||
* treat an empty password as an anonymous bind.
|
||||
*/
|
||||
$code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
|
||||
$messages[0] = 'A password is required';
|
||||
return new Zend_Auth_Result($code, '', $messages);
|
||||
}
|
||||
|
||||
$ldap = $this->getLdap();
|
||||
|
||||
$code = Zend_Auth_Result::FAILURE;
|
||||
$messages[0] = "Authority not found: $username";
|
||||
$failedAuthorities = array();
|
||||
|
||||
/* Iterate through each server and try to authenticate the supplied
|
||||
* credentials against it.
|
||||
*/
|
||||
foreach ($this->_options as $name => $options) {
|
||||
|
||||
if (!is_array($options)) {
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Exception.php';
|
||||
throw new Zend_Auth_Adapter_Exception('Adapter options array not in array');
|
||||
}
|
||||
$adapterOptions = $this->_prepareOptions($ldap, $options);
|
||||
$dname = '';
|
||||
|
||||
try {
|
||||
if ($messages[1])
|
||||
$messages[] = $messages[1];
|
||||
$messages[1] = '';
|
||||
$messages[] = $this->_optionsToString($options);
|
||||
|
||||
$dname = $this->_getAuthorityName();
|
||||
if (isset($failedAuthorities[$dname])) {
|
||||
/* If multiple sets of server options for the same domain
|
||||
* are supplied, we want to skip redundant authentications
|
||||
* where the identity or credentials where found to be
|
||||
* invalid with another server for the same domain. The
|
||||
* $failedAuthorities array tracks this condition (and also
|
||||
* serves to supply the original error message).
|
||||
* This fixes issue ZF-4093.
|
||||
*/
|
||||
$messages[1] = $failedAuthorities[$dname];
|
||||
$messages[] = "Skipping previously failed authority: $dname";
|
||||
continue;
|
||||
}
|
||||
|
||||
$canonicalName = $ldap->getCanonicalAccountName($username);
|
||||
$ldap->bind($canonicalName, $password);
|
||||
$dn = $ldap->getCanonicalAccountName($canonicalName, Zend_Ldap::ACCTNAME_FORM_DN);
|
||||
|
||||
$groupResult = $this->_checkGroupMembership($ldap, $canonicalName, $dn, $adapterOptions);
|
||||
if ($groupResult === true) {
|
||||
$this->_authenticatedDn = $dn;
|
||||
$messages[0] = '';
|
||||
$messages[1] = '';
|
||||
$messages[] = "$canonicalName authentication successful";
|
||||
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $canonicalName, $messages);
|
||||
} else {
|
||||
$messages[0] = 'Account is not a member of the specified group';
|
||||
$messages[1] = $groupResult;
|
||||
$failedAuthorities[$dname] = $groupResult;
|
||||
}
|
||||
} catch (Zend_Ldap_Exception $zle) {
|
||||
|
||||
/* LDAP based authentication is notoriously difficult to diagnose. Therefore
|
||||
* we bend over backwards to capture and record every possible bit of
|
||||
* information when something goes wrong.
|
||||
*/
|
||||
|
||||
$err = $zle->getCode();
|
||||
|
||||
if ($err == Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH) {
|
||||
/* This error indicates that the domain supplied in the
|
||||
* username did not match the domains in the server options
|
||||
* and therefore we should just skip to the next set of
|
||||
* server options.
|
||||
*/
|
||||
continue;
|
||||
} else if ($err == Zend_Ldap_Exception::LDAP_NO_SUCH_OBJECT) {
|
||||
$code = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
|
||||
$messages[0] = "Account not found: $username";
|
||||
$failedAuthorities[$dname] = $zle->getMessage();
|
||||
} else if ($err == Zend_Ldap_Exception::LDAP_INVALID_CREDENTIALS) {
|
||||
$code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
|
||||
$messages[0] = 'Invalid credentials';
|
||||
$failedAuthorities[$dname] = $zle->getMessage();
|
||||
} else {
|
||||
$line = $zle->getLine();
|
||||
$messages[] = $zle->getFile() . "($line): " . $zle->getMessage();
|
||||
$messages[] = str_replace($password, '*****', $zle->getTraceAsString());
|
||||
$messages[0] = 'An unexpected failure occurred';
|
||||
}
|
||||
$messages[1] = $zle->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
$msg = isset($messages[1]) ? $messages[1] : $messages[0];
|
||||
$messages[] = "$username authentication failed: $msg";
|
||||
|
||||
return new Zend_Auth_Result($code, $username, $messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the LDAP specific options on the Zend_Ldap instance
|
||||
*
|
||||
* @param Zend_Ldap $ldap
|
||||
* @param array $options
|
||||
* @return array of auth-adapter specific options
|
||||
*/
|
||||
protected function _prepareOptions(Zend_Ldap $ldap, array $options)
|
||||
{
|
||||
$adapterOptions = array(
|
||||
'group' => null,
|
||||
'groupDn' => $ldap->getBaseDn(),
|
||||
'groupScope' => Zend_Ldap::SEARCH_SCOPE_SUB,
|
||||
'groupAttr' => 'cn',
|
||||
'groupFilter' => 'objectClass=groupOfUniqueNames',
|
||||
'memberAttr' => 'uniqueMember',
|
||||
'memberIsDn' => true
|
||||
);
|
||||
foreach ($adapterOptions as $key => $value) {
|
||||
if (array_key_exists($key, $options)) {
|
||||
$value = $options[$key];
|
||||
unset($options[$key]);
|
||||
switch ($key) {
|
||||
case 'groupScope':
|
||||
$value = (int)$value;
|
||||
if (in_array($value, array(Zend_Ldap::SEARCH_SCOPE_BASE,
|
||||
Zend_Ldap::SEARCH_SCOPE_ONE, Zend_Ldap::SEARCH_SCOPE_SUB), true)) {
|
||||
$adapterOptions[$key] = $value;
|
||||
}
|
||||
break;
|
||||
case 'memberIsDn':
|
||||
$adapterOptions[$key] = ($value === true ||
|
||||
$value === '1' || strcasecmp($value, 'true') == 0);
|
||||
break;
|
||||
default:
|
||||
$adapterOptions[$key] = trim($value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ldap->setOptions($options);
|
||||
return $adapterOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the group membership of the bound user
|
||||
*
|
||||
* @param Zend_Ldap $ldap
|
||||
* @param string $canonicalName
|
||||
* @param string $dn
|
||||
* @param array $adapterOptions
|
||||
* @return string|true
|
||||
*/
|
||||
protected function _checkGroupMembership(Zend_Ldap $ldap, $canonicalName, $dn, array $adapterOptions)
|
||||
{
|
||||
if ($adapterOptions['group'] === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($adapterOptions['memberIsDn'] === false) {
|
||||
$user = $canonicalName;
|
||||
} else {
|
||||
$user = $dn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Zend_Ldap_Filter
|
||||
*/
|
||||
require_once 'Zend/Ldap/Filter.php';
|
||||
$groupName = Zend_Ldap_Filter::equals($adapterOptions['groupAttr'], $adapterOptions['group']);
|
||||
$membership = Zend_Ldap_Filter::equals($adapterOptions['memberAttr'], $user);
|
||||
$group = Zend_Ldap_Filter::andFilter($groupName, $membership);
|
||||
$groupFilter = $adapterOptions['groupFilter'];
|
||||
if (!empty($groupFilter)) {
|
||||
$group = $group->addAnd($groupFilter);
|
||||
}
|
||||
|
||||
/*
|
||||
* Fixes problem when authenticated user is not allowed to retrieve
|
||||
* group-membership information.
|
||||
* This requires that the user specified with "username" and "password"
|
||||
* in the Zend_Ldap options is able to retrieve the required information.
|
||||
*/
|
||||
$ldap->bind();
|
||||
|
||||
$result = $ldap->count($group, $adapterOptions['groupDn'], $adapterOptions['groupScope']);
|
||||
|
||||
if ($result === 1) {
|
||||
return true;
|
||||
} else {
|
||||
return 'Failed to verify group membership with ' . $group->toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getAccountObject() - Returns the result entry as a stdClass object
|
||||
*
|
||||
* This ressembles the feature {@see Zend_Auth_Adapter_DbTable::getResultRowObject()}.
|
||||
* Closes ZF-6813
|
||||
*
|
||||
* @param array $returnAttribs
|
||||
* @return stdClass|boolean
|
||||
*/
|
||||
public function getAccountObject(array $returnAttribs = array())
|
||||
{
|
||||
if (!$this->_authenticatedDn) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$returnObject = new stdClass();
|
||||
|
||||
$entry = $this->getLdap()->getEntry($this->_authenticatedDn, $returnAttribs, true);
|
||||
foreach ($entry as $attr => $value) {
|
||||
if (is_array($value)) {
|
||||
$returnObject->$attr = (count($value) > 1) ? $value : $value[0];
|
||||
} else {
|
||||
$returnObject->$attr = $value;
|
||||
}
|
||||
}
|
||||
return $returnObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts options to string
|
||||
*
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
private function _optionsToString(array $options)
|
||||
{
|
||||
$str = '';
|
||||
foreach ($options as $key => $val) {
|
||||
if ($key === 'password')
|
||||
$val = '*****';
|
||||
if ($str)
|
||||
$str .= ',';
|
||||
$str .= $key . '=' . $val;
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
}
|
||||
@@ -1,284 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Adapter/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_OpenId_Consumer
|
||||
*/
|
||||
require_once 'Zend/OpenId/Consumer.php';
|
||||
|
||||
|
||||
/**
|
||||
* A Zend_Auth Authentication Adapter allowing the use of OpenID protocol as an
|
||||
* authentication mechanism
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Adapter_OpenId implements Zend_Auth_Adapter_Interface
|
||||
{
|
||||
/**
|
||||
* The identity value being authenticated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_id = null;
|
||||
|
||||
/**
|
||||
* Reference to an implementation of a storage object
|
||||
*
|
||||
* @var Zend_OpenId_Consumer_Storage
|
||||
*/
|
||||
private $_storage = null;
|
||||
|
||||
/**
|
||||
* The URL to redirect response from server to
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_returnTo = null;
|
||||
|
||||
/**
|
||||
* The HTTP URL to identify consumer on server
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_root = null;
|
||||
|
||||
/**
|
||||
* Extension object or array of extensions objects
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_extensions = null;
|
||||
|
||||
/**
|
||||
* The response object to perform HTTP or HTML form redirection
|
||||
*
|
||||
* @var Zend_Controller_Response_Abstract
|
||||
*/
|
||||
private $_response = null;
|
||||
|
||||
/**
|
||||
* Enables or disables interaction with user during authentication on
|
||||
* OpenID provider.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_check_immediate = false;
|
||||
|
||||
/**
|
||||
* HTTP client to make HTTP requests
|
||||
*
|
||||
* @var Zend_Http_Client $_httpClient
|
||||
*/
|
||||
private $_httpClient = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $id the identity value
|
||||
* @param Zend_OpenId_Consumer_Storage $storage an optional implementation
|
||||
* of a storage object
|
||||
* @param string $returnTo HTTP URL to redirect response from server to
|
||||
* @param string $root HTTP URL to identify consumer on server
|
||||
* @param mixed $extensions extension object or array of extensions objects
|
||||
* @param Zend_Controller_Response_Abstract $response an optional response
|
||||
* object to perform HTTP or HTML form redirection
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($id = null,
|
||||
Zend_OpenId_Consumer_Storage $storage = null,
|
||||
$returnTo = null,
|
||||
$root = null,
|
||||
$extensions = null,
|
||||
Zend_Controller_Response_Abstract $response = null) {
|
||||
$this->_id = $id;
|
||||
$this->_storage = $storage;
|
||||
$this->_returnTo = $returnTo;
|
||||
$this->_root = $root;
|
||||
$this->_extensions = $extensions;
|
||||
$this->_response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value to be used as the identity
|
||||
*
|
||||
* @param string $id the identity value
|
||||
* @return Zend_Auth_Adapter_OpenId Provides a fluent interface
|
||||
*/
|
||||
public function setIdentity($id)
|
||||
{
|
||||
$this->_id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the storage implementation which will be use by OpenId
|
||||
*
|
||||
* @param Zend_OpenId_Consumer_Storage $storage
|
||||
* @return Zend_Auth_Adapter_OpenId Provides a fluent interface
|
||||
*/
|
||||
public function setStorage(Zend_OpenId_Consumer_Storage $storage)
|
||||
{
|
||||
$this->_storage = $storage;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the HTTP URL to redirect response from server to
|
||||
*
|
||||
* @param string $returnTo
|
||||
* @return Zend_Auth_Adapter_OpenId Provides a fluent interface
|
||||
*/
|
||||
public function setReturnTo($returnTo)
|
||||
{
|
||||
$this->_returnTo = $returnTo;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets HTTP URL to identify consumer on server
|
||||
*
|
||||
* @param string $root
|
||||
* @return Zend_Auth_Adapter_OpenId Provides a fluent interface
|
||||
*/
|
||||
public function setRoot($root)
|
||||
{
|
||||
$this->_root = $root;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets OpenID extension(s)
|
||||
*
|
||||
* @param mixed $extensions
|
||||
* @return Zend_Auth_Adapter_OpenId Provides a fluent interface
|
||||
*/
|
||||
public function setExtensions($extensions)
|
||||
{
|
||||
$this->_extensions = $extensions;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an optional response object to perform HTTP or HTML form redirection
|
||||
*
|
||||
* @param string $root
|
||||
* @return Zend_Auth_Adapter_OpenId Provides a fluent interface
|
||||
*/
|
||||
public function setResponse($response)
|
||||
{
|
||||
$this->_response = $response;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables interaction with user during authentication on
|
||||
* OpenID provider.
|
||||
*
|
||||
* @param bool $check_immediate
|
||||
* @return Zend_Auth_Adapter_OpenId Provides a fluent interface
|
||||
*/
|
||||
public function setCheckImmediate($check_immediate)
|
||||
{
|
||||
$this->_check_immediate = $check_immediate;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets HTTP client object to make HTTP requests
|
||||
*
|
||||
* @param Zend_Http_Client $client HTTP client object to be used
|
||||
*/
|
||||
public function setHttpClient($client) {
|
||||
$this->_httpClient = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticates the given OpenId identity.
|
||||
* Defined by Zend_Auth_Adapter_Interface.
|
||||
*
|
||||
* @throws Zend_Auth_Adapter_Exception If answering the authentication query is impossible
|
||||
* @return Zend_Auth_Result
|
||||
*/
|
||||
public function authenticate() {
|
||||
$id = $this->_id;
|
||||
if (!empty($id)) {
|
||||
$consumer = new Zend_OpenId_Consumer($this->_storage);
|
||||
$consumer->setHttpClient($this->_httpClient);
|
||||
/* login() is never returns on success */
|
||||
if (!$this->_check_immediate) {
|
||||
if (!$consumer->login($id,
|
||||
$this->_returnTo,
|
||||
$this->_root,
|
||||
$this->_extensions,
|
||||
$this->_response)) {
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE,
|
||||
$id,
|
||||
array("Authentication failed", $consumer->getError()));
|
||||
}
|
||||
} else {
|
||||
if (!$consumer->check($id,
|
||||
$this->_returnTo,
|
||||
$this->_root,
|
||||
$this->_extensions,
|
||||
$this->_response)) {
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE,
|
||||
$id,
|
||||
array("Authentication failed", $consumer->getError()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$params = (isset($_SERVER['REQUEST_METHOD']) &&
|
||||
$_SERVER['REQUEST_METHOD']=='POST') ? $_POST: $_GET;
|
||||
$consumer = new Zend_OpenId_Consumer($this->_storage);
|
||||
$consumer->setHttpClient($this->_httpClient);
|
||||
if ($consumer->verify(
|
||||
$params,
|
||||
$id,
|
||||
$this->_extensions)) {
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::SUCCESS,
|
||||
$id,
|
||||
array("Authentication successful"));
|
||||
} else {
|
||||
return new Zend_Auth_Result(
|
||||
Zend_Auth_Result::FAILURE,
|
||||
$id,
|
||||
array("Authentication failed", $consumer->getError()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Exception
|
||||
*/
|
||||
require_once 'Zend/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Exception extends Zend_Exception
|
||||
{}
|
||||
@@ -1,148 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Result
|
||||
{
|
||||
/**
|
||||
* General Failure
|
||||
*/
|
||||
const FAILURE = 0;
|
||||
|
||||
/**
|
||||
* Failure due to identity not being found.
|
||||
*/
|
||||
const FAILURE_IDENTITY_NOT_FOUND = -1;
|
||||
|
||||
/**
|
||||
* Failure due to identity being ambiguous.
|
||||
*/
|
||||
const FAILURE_IDENTITY_AMBIGUOUS = -2;
|
||||
|
||||
/**
|
||||
* Failure due to invalid credential being supplied.
|
||||
*/
|
||||
const FAILURE_CREDENTIAL_INVALID = -3;
|
||||
|
||||
/**
|
||||
* Failure due to uncategorized reasons.
|
||||
*/
|
||||
const FAILURE_UNCATEGORIZED = -4;
|
||||
|
||||
/**
|
||||
* Authentication success.
|
||||
*/
|
||||
const SUCCESS = 1;
|
||||
|
||||
/**
|
||||
* Authentication result code
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_code;
|
||||
|
||||
/**
|
||||
* The identity used in the authentication attempt
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_identity;
|
||||
|
||||
/**
|
||||
* An array of string reasons why the authentication attempt was unsuccessful
|
||||
*
|
||||
* If authentication was successful, this should be an empty array.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_messages;
|
||||
|
||||
/**
|
||||
* Sets the result code, identity, and failure messages
|
||||
*
|
||||
* @param int $code
|
||||
* @param mixed $identity
|
||||
* @param array $messages
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($code, $identity, array $messages = array())
|
||||
{
|
||||
$code = (int) $code;
|
||||
|
||||
if ($code < self::FAILURE_UNCATEGORIZED) {
|
||||
$code = self::FAILURE;
|
||||
} elseif ($code > self::SUCCESS ) {
|
||||
$code = 1;
|
||||
}
|
||||
|
||||
$this->_code = $code;
|
||||
$this->_identity = $identity;
|
||||
$this->_messages = $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the result represents a successful authentication attempt
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isValid()
|
||||
{
|
||||
return ($this->_code > 0) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* getCode() - Get the result code for this authentication attempt
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getCode()
|
||||
{
|
||||
return $this->_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the identity used in the authentication attempt
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getIdentity()
|
||||
{
|
||||
return $this->_identity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of string reasons why the authentication attempt was unsuccessful
|
||||
*
|
||||
* If authentication was successful, this method returns an empty array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getMessages()
|
||||
{
|
||||
return $this->_messages;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Storage
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Exception
|
||||
*/
|
||||
require_once 'Zend/Auth/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Storage
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Storage_Exception extends Zend_Auth_Exception
|
||||
{}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Storage
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Storage
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Auth_Storage_Interface
|
||||
{
|
||||
/**
|
||||
* Returns true if and only if storage is empty
|
||||
*
|
||||
* @throws Zend_Auth_Storage_Exception If it is impossible to determine whether storage is empty
|
||||
* @return boolean
|
||||
*/
|
||||
public function isEmpty();
|
||||
|
||||
/**
|
||||
* Returns the contents of storage
|
||||
*
|
||||
* Behavior is undefined when storage is empty.
|
||||
*
|
||||
* @throws Zend_Auth_Storage_Exception If reading contents from storage is impossible
|
||||
* @return mixed
|
||||
*/
|
||||
public function read();
|
||||
|
||||
/**
|
||||
* Writes $contents to storage
|
||||
*
|
||||
* @param mixed $contents
|
||||
* @throws Zend_Auth_Storage_Exception If writing $contents to storage is impossible
|
||||
* @return void
|
||||
*/
|
||||
public function write($contents);
|
||||
|
||||
/**
|
||||
* Clears contents from storage
|
||||
*
|
||||
* @throws Zend_Auth_Storage_Exception If clearing contents from storage is impossible
|
||||
* @return void
|
||||
*/
|
||||
public function clear();
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Storage
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Storage_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Storage/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* Non-Persistent Auth Storage
|
||||
*
|
||||
* Since HTTP Authentication happens again on each request, this will always be
|
||||
* re-populated. So there's no need to use sessions, this simple value class
|
||||
* will hold the data for rest of the current request.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Storage
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Storage_NonPersistent implements Zend_Auth_Storage_Interface
|
||||
{
|
||||
/**
|
||||
* Holds the actual auth data
|
||||
*/
|
||||
protected $_data;
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if and only if storage is empty
|
||||
*
|
||||
* @throws Zend_Auth_Storage_Exception If it is impossible to determine whether storage is empty
|
||||
* @return boolean
|
||||
*/
|
||||
public function isEmpty()
|
||||
{
|
||||
return empty($this->_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of storage
|
||||
* Behavior is undefined when storage is empty.
|
||||
*
|
||||
* @throws Zend_Auth_Storage_Exception If reading contents from storage is impossible
|
||||
* @return mixed
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
return $this->_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes $contents to storage
|
||||
*
|
||||
* @param mixed $contents
|
||||
* @throws Zend_Auth_Storage_Exception If writing $contents to storage is impossible
|
||||
* @return void
|
||||
*/
|
||||
public function write($contents)
|
||||
{
|
||||
$this->_data = $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears contents from storage
|
||||
*
|
||||
* @throws Zend_Auth_Storage_Exception If clearing contents from storage is impossible
|
||||
* @return void
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->_data = null;
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Storage
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Auth_Storage_Interface
|
||||
*/
|
||||
require_once 'Zend/Auth/Storage/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Session
|
||||
*/
|
||||
require_once 'Zend/Session.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Auth
|
||||
* @subpackage Zend_Auth_Storage
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Auth_Storage_Session implements Zend_Auth_Storage_Interface
|
||||
{
|
||||
/**
|
||||
* Default session namespace
|
||||
*/
|
||||
const NAMESPACE_DEFAULT = 'Zend_Auth';
|
||||
|
||||
/**
|
||||
* Default session object member name
|
||||
*/
|
||||
const MEMBER_DEFAULT = 'storage';
|
||||
|
||||
/**
|
||||
* Object to proxy $_SESSION storage
|
||||
*
|
||||
* @var Zend_Session_Namespace
|
||||
*/
|
||||
protected $_session;
|
||||
|
||||
/**
|
||||
* Session namespace
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_namespace;
|
||||
|
||||
/**
|
||||
* Session object member
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_member;
|
||||
|
||||
/**
|
||||
* Sets session storage options and initializes session namespace object
|
||||
*
|
||||
* @param mixed $namespace
|
||||
* @param mixed $member
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($namespace = self::NAMESPACE_DEFAULT, $member = self::MEMBER_DEFAULT)
|
||||
{
|
||||
$this->_namespace = $namespace;
|
||||
$this->_member = $member;
|
||||
$this->_session = new Zend_Session_Namespace($this->_namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the session namespace
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNamespace()
|
||||
{
|
||||
return $this->_namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the session object member
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMember()
|
||||
{
|
||||
return $this->_member;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Auth_Storage_Interface
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isEmpty()
|
||||
{
|
||||
return !isset($this->_session->{$this->_member});
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Auth_Storage_Interface
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
return $this->_session->{$this->_member};
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Auth_Storage_Interface
|
||||
*
|
||||
* @param mixed $contents
|
||||
* @return void
|
||||
*/
|
||||
public function write($contents)
|
||||
{
|
||||
$this->_session->{$this->_member} = $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Auth_Storage_Interface
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
unset($this->_session->{$this->_member});
|
||||
}
|
||||
}
|
||||
@@ -1,245 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Cache
|
||||
{
|
||||
|
||||
/**
|
||||
* Standard frontends
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $standardFrontends = array('Core', 'Output', 'Class', 'File', 'Function', 'Page');
|
||||
|
||||
/**
|
||||
* Standard backends
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $standardBackends = array('File', 'Sqlite', 'Memcached', 'Apc', 'ZendPlatform', 'Xcache', 'TwoLevels');
|
||||
|
||||
/**
|
||||
* Standard backends which implement the ExtendedInterface
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $standardExtendedBackends = array('File', 'Apc', 'TwoLevels', 'Memcached', 'Sqlite');
|
||||
|
||||
/**
|
||||
* Only for backward compatibily (may be removed in next major release)
|
||||
*
|
||||
* @var array
|
||||
* @deprecated
|
||||
*/
|
||||
public static $availableFrontends = array('Core', 'Output', 'Class', 'File', 'Function', 'Page');
|
||||
|
||||
/**
|
||||
* Only for backward compatibily (may be removed in next major release)
|
||||
*
|
||||
* @var array
|
||||
* @deprecated
|
||||
*/
|
||||
public static $availableBackends = array('File', 'Sqlite', 'Memcached', 'Apc', 'ZendPlatform', 'Xcache', 'TwoLevels');
|
||||
|
||||
/**
|
||||
* Consts for clean() method
|
||||
*/
|
||||
const CLEANING_MODE_ALL = 'all';
|
||||
const CLEANING_MODE_OLD = 'old';
|
||||
const CLEANING_MODE_MATCHING_TAG = 'matchingTag';
|
||||
const CLEANING_MODE_NOT_MATCHING_TAG = 'notMatchingTag';
|
||||
const CLEANING_MODE_MATCHING_ANY_TAG = 'matchingAnyTag';
|
||||
|
||||
/**
|
||||
* Factory
|
||||
*
|
||||
* @param mixed $frontend frontend name (string) or Zend_Cache_Frontend_ object
|
||||
* @param mixed $backend backend name (string) or Zend_Cache_Backend_ object
|
||||
* @param array $frontendOptions associative array of options for the corresponding frontend constructor
|
||||
* @param array $backendOptions associative array of options for the corresponding backend constructor
|
||||
* @param boolean $customFrontendNaming if true, the frontend argument is used as a complete class name ; if false, the frontend argument is used as the end of "Zend_Cache_Frontend_[...]" class name
|
||||
* @param boolean $customBackendNaming if true, the backend argument is used as a complete class name ; if false, the backend argument is used as the end of "Zend_Cache_Backend_[...]" class name
|
||||
* @param boolean $autoload if true, there will no require_once for backend and frontend (usefull only for custom backends/frontends)
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return Zend_Cache_Core|Zend_Cache_Frontend
|
||||
*/
|
||||
public static function factory($frontend, $backend, $frontendOptions = array(), $backendOptions = array(), $customFrontendNaming = false, $customBackendNaming = false, $autoload = false)
|
||||
{
|
||||
if (is_string($backend)) {
|
||||
$backendObject = self::_makeBackend($backend, $backendOptions, $customBackendNaming, $autoload);
|
||||
} else {
|
||||
if ((is_object($backend)) && (in_array('Zend_Cache_Backend_Interface', class_implements($backend)))) {
|
||||
$backendObject = $backend;
|
||||
} else {
|
||||
self::throwException('backend must be a backend name (string) or an object which implements Zend_Cache_Backend_Interface');
|
||||
}
|
||||
}
|
||||
if (is_string($frontend)) {
|
||||
$frontendObject = self::_makeFrontend($frontend, $frontendOptions, $customFrontendNaming, $autoload);
|
||||
} else {
|
||||
if (is_object($frontend)) {
|
||||
$frontendObject = $frontend;
|
||||
} else {
|
||||
self::throwException('frontend must be a frontend name (string) or an object');
|
||||
}
|
||||
}
|
||||
$frontendObject->setBackend($backendObject);
|
||||
return $frontendObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Frontend Constructor
|
||||
*
|
||||
* @param string $backend
|
||||
* @param array $backendOptions
|
||||
* @param boolean $customBackendNaming
|
||||
* @param boolean $autoload
|
||||
* @return Zend_Cache_Backend
|
||||
*/
|
||||
public static function _makeBackend($backend, $backendOptions, $customBackendNaming = false, $autoload = false)
|
||||
{
|
||||
if (!$customBackendNaming) {
|
||||
$backend = self::_normalizeName($backend);
|
||||
}
|
||||
if (in_array($backend, Zend_Cache::$standardBackends)) {
|
||||
// we use a standard backend
|
||||
$backendClass = 'Zend_Cache_Backend_' . $backend;
|
||||
// security controls are explicit
|
||||
require_once str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
|
||||
} else {
|
||||
// we use a custom backend
|
||||
if (!preg_match('~^[\w]+$~D', $backend)) {
|
||||
Zend_Cache::throwException("Invalid backend name [$backend]");
|
||||
}
|
||||
if (!$customBackendNaming) {
|
||||
// we use this boolean to avoid an API break
|
||||
$backendClass = 'Zend_Cache_Backend_' . $backend;
|
||||
} else {
|
||||
$backendClass = $backend;
|
||||
}
|
||||
if (!$autoload) {
|
||||
$file = str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
|
||||
if (!(self::_isReadable($file))) {
|
||||
self::throwException("file $file not found in include_path");
|
||||
}
|
||||
require_once $file;
|
||||
}
|
||||
}
|
||||
return new $backendClass($backendOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Backend Constructor
|
||||
*
|
||||
* @param string $frontend
|
||||
* @param array $frontendOptions
|
||||
* @param boolean $customFrontendNaming
|
||||
* @param boolean $autoload
|
||||
* @return Zend_Cache_Core|Zend_Cache_Frontend
|
||||
*/
|
||||
public static function _makeFrontend($frontend, $frontendOptions = array(), $customFrontendNaming = false, $autoload = false)
|
||||
{
|
||||
if (!$customFrontendNaming) {
|
||||
$frontend = self::_normalizeName($frontend);
|
||||
}
|
||||
if (in_array($frontend, self::$standardFrontends)) {
|
||||
// we use a standard frontend
|
||||
// For perfs reasons, with frontend == 'Core', we can interact with the Core itself
|
||||
$frontendClass = 'Zend_Cache_' . ($frontend != 'Core' ? 'Frontend_' : '') . $frontend;
|
||||
// security controls are explicit
|
||||
require_once str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
|
||||
} else {
|
||||
// we use a custom frontend
|
||||
if (!preg_match('~^[\w]+$~D', $frontend)) {
|
||||
Zend_Cache::throwException("Invalid frontend name [$frontend]");
|
||||
}
|
||||
if (!$customFrontendNaming) {
|
||||
// we use this boolean to avoid an API break
|
||||
$frontendClass = 'Zend_Cache_Frontend_' . $frontend;
|
||||
} else {
|
||||
$frontendClass = $frontend;
|
||||
}
|
||||
if (!$autoload) {
|
||||
$file = str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
|
||||
if (!(self::_isReadable($file))) {
|
||||
self::throwException("file $file not found in include_path");
|
||||
}
|
||||
require_once $file;
|
||||
}
|
||||
}
|
||||
return new $frontendClass($frontendOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw an exception
|
||||
*
|
||||
* Note : for perf reasons, the "load" of Zend/Cache/Exception is dynamic
|
||||
* @param string $msg Message for the exception
|
||||
* @throws Zend_Cache_Exception
|
||||
*/
|
||||
public static function throwException($msg)
|
||||
{
|
||||
// For perfs reasons, we use this dynamic inclusion
|
||||
require_once 'Zend/Cache/Exception.php';
|
||||
throw new Zend_Cache_Exception($msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize frontend and backend names to allow multiple words TitleCased
|
||||
*
|
||||
* @param string $name Name to normalize
|
||||
* @return string
|
||||
*/
|
||||
protected static function _normalizeName($name)
|
||||
{
|
||||
$name = ucfirst(strtolower($name));
|
||||
$name = str_replace(array('-', '_', '.'), ' ', $name);
|
||||
$name = ucwords($name);
|
||||
$name = str_replace(' ', '', $name);
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns TRUE if the $filename is readable, or FALSE otherwise.
|
||||
* This function uses the PHP include_path, where PHP's is_readable()
|
||||
* does not.
|
||||
*
|
||||
* Note : this method comes from Zend_Loader (see #ZF-2891 for details)
|
||||
*
|
||||
* @param string $filename
|
||||
* @return boolean
|
||||
*/
|
||||
private static function _isReadable($filename)
|
||||
{
|
||||
if (!$fh = @fopen($filename, 'r', true)) {
|
||||
return false;
|
||||
}
|
||||
@fclose($fh);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,272 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend
|
||||
{
|
||||
/**
|
||||
* Frontend or Core directives
|
||||
*
|
||||
* =====> (int) lifetime :
|
||||
* - Cache lifetime (in seconds)
|
||||
* - If null, the cache is valid forever
|
||||
*
|
||||
* =====> (int) logging :
|
||||
* - if set to true, a logging is activated throw Zend_Log
|
||||
*
|
||||
* @var array directives
|
||||
*/
|
||||
protected $_directives = array(
|
||||
'lifetime' => 3600,
|
||||
'logging' => false,
|
||||
'logger' => null
|
||||
);
|
||||
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
while (list($name, $value) = each($options)) {
|
||||
$this->setOption($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the frontend directives
|
||||
*
|
||||
* @param array $directives Assoc of directives
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function setDirectives($directives)
|
||||
{
|
||||
if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array');
|
||||
while (list($name, $value) = each($directives)) {
|
||||
if (!is_string($name)) {
|
||||
Zend_Cache::throwException("Incorrect option name : $name");
|
||||
}
|
||||
$name = strtolower($name);
|
||||
if (array_key_exists($name, $this->_directives)) {
|
||||
$this->_directives[$name] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->_loggerSanity();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an option
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function setOption($name, $value)
|
||||
{
|
||||
if (!is_string($name)) {
|
||||
Zend_Cache::throwException("Incorrect option name : $name");
|
||||
}
|
||||
$name = strtolower($name);
|
||||
if (array_key_exists($name, $this->_options)) {
|
||||
$this->_options[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the life time
|
||||
*
|
||||
* if $specificLifetime is not false, the given specific life time is used
|
||||
* else, the global lifetime is used
|
||||
*
|
||||
* @param int $specificLifetime
|
||||
* @return int Cache life time
|
||||
*/
|
||||
public function getLifetime($specificLifetime)
|
||||
{
|
||||
if ($specificLifetime === false) {
|
||||
return $this->_directives['lifetime'];
|
||||
}
|
||||
return $specificLifetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the automatic cleaning is available for the backend
|
||||
*
|
||||
* DEPRECATED : use getCapabilities() instead
|
||||
*
|
||||
* @deprecated
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAutomaticCleaningAvailable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine system TMP directory and detect if we have read access
|
||||
*
|
||||
* inspired from Zend_File_Transfer_Adapter_Abstract
|
||||
*
|
||||
* @return string
|
||||
* @throws Zend_Cache_Exception if unable to determine directory
|
||||
*/
|
||||
public function getTmpDir()
|
||||
{
|
||||
$tmpdir = array();
|
||||
foreach (array($_ENV, $_SERVER) as $tab) {
|
||||
foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
|
||||
if (isset($tab[$key])) {
|
||||
if (($key == 'windir') or ($key == 'SystemRoot')) {
|
||||
$dir = realpath($tab[$key] . '\\temp');
|
||||
} else {
|
||||
$dir = realpath($tab[$key]);
|
||||
}
|
||||
if ($this->_isGoodTmpDir($dir)) {
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$upload = ini_get('upload_tmp_dir');
|
||||
if ($upload) {
|
||||
$dir = realpath($upload);
|
||||
if ($this->_isGoodTmpDir($dir)) {
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
if (function_exists('sys_get_temp_dir')) {
|
||||
$dir = sys_get_temp_dir();
|
||||
if ($this->_isGoodTmpDir($dir)) {
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
// Attemp to detect by creating a temporary file
|
||||
$tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
|
||||
if ($tempFile) {
|
||||
$dir = realpath(dirname($tempFile));
|
||||
unlink($tempFile);
|
||||
if ($this->_isGoodTmpDir($dir)) {
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
if ($this->_isGoodTmpDir('/tmp')) {
|
||||
return '/tmp';
|
||||
}
|
||||
if ($this->_isGoodTmpDir('\\temp')) {
|
||||
return '\\temp';
|
||||
}
|
||||
Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the given temporary directory is readable and writable
|
||||
*
|
||||
* @param $dir temporary directory
|
||||
* @return boolean true if the directory is ok
|
||||
*/
|
||||
protected function _isGoodTmpDir($dir)
|
||||
{
|
||||
if (is_readable($dir)) {
|
||||
if (is_writable($dir)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure if we enable logging that the Zend_Log class
|
||||
* is available.
|
||||
* Create a default log object if none is set.
|
||||
*
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected function _loggerSanity()
|
||||
{
|
||||
if (!isset($this->_directives['logging']) || !$this->_directives['logging']) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
/**
|
||||
* @see Zend_Log
|
||||
*/
|
||||
require_once 'Zend/Log.php';
|
||||
} catch (Zend_Exception $e) {
|
||||
Zend_Cache::throwException('Logging feature is enabled but the Zend_Log class is not available');
|
||||
}
|
||||
if (isset($this->_directives['logger'])) {
|
||||
if ($this->_directives['logger'] instanceof Zend_Log) {
|
||||
return;
|
||||
} else {
|
||||
Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
|
||||
}
|
||||
}
|
||||
// Create a default logger to the standard output stream
|
||||
require_once 'Zend/Log/Writer/Stream.php';
|
||||
$logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
|
||||
$this->_directives['logger'] = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message at the WARN (4) priority.
|
||||
*
|
||||
* @param string $message
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected function _log($message, $priority = 4)
|
||||
{
|
||||
if (!$this->_directives['logging']) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($this->_directives['logger'])) {
|
||||
Zend_Cache::throwException('Logging is enabled but logger is not set.');
|
||||
}
|
||||
$logger = $this->_directives['logger'];
|
||||
if (!$logger instanceof Zend_Log) {
|
||||
Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
|
||||
}
|
||||
$logger->log($message, $priority);
|
||||
}
|
||||
}
|
||||
@@ -1,355 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/ExtendedInterface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_Apc extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
|
||||
{
|
||||
/**
|
||||
* Log message
|
||||
*/
|
||||
const TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend';
|
||||
const TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::save() : tags are unsupported by the Apc backend';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
if (!extension_loaded('apc')) {
|
||||
Zend_Cache::throwException('The apc extension must be loaded for using this backend !');
|
||||
}
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* WARNING $doNotTestCacheValidity=true is unsupported by the Apc backend
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
|
||||
* @return string cached datas (or false)
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
$tmp = apc_fetch($id);
|
||||
if (is_array($tmp)) {
|
||||
return $tmp[0];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
$tmp = apc_fetch($id);
|
||||
if (is_array($tmp)) {
|
||||
return $tmp[1];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data datas to cache
|
||||
* @param string $id cache id
|
||||
* @param array $tags array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false)
|
||||
{
|
||||
$lifetime = $this->getLifetime($specificLifetime);
|
||||
$result = apc_store($id, array($data, time(), $lifetime), $lifetime);
|
||||
if (count($tags) > 0) {
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
return apc_delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* 'all' (default) => remove all cache entries ($tags is not used)
|
||||
* 'old' => unsupported
|
||||
* 'matchingTag' => unsupported
|
||||
* 'notMatchingTag' => unsupported
|
||||
* 'matchingAnyTag' => unsupported
|
||||
*
|
||||
* @param string $mode clean mode
|
||||
* @param array $tags array of tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
switch ($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
return apc_clear_cache('user');
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_OLD:
|
||||
$this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
|
||||
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND);
|
||||
break;
|
||||
default:
|
||||
Zend_Cache::throwException('Invalid mode for clean() method');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the automatic cleaning is available for the backend
|
||||
*
|
||||
* DEPRECATED : use getCapabilities() instead
|
||||
*
|
||||
* @deprecated
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAutomaticCleaningAvailable()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the filling percentage of the backend storage
|
||||
*
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return int integer between 0 and 100
|
||||
*/
|
||||
public function getFillingPercentage()
|
||||
{
|
||||
$mem = apc_sma_info(true);
|
||||
$memSize = $mem['num_seg'] * $mem['seg_size'];
|
||||
$memAvailable= $mem['avail_mem'];
|
||||
$memUsed = $memSize - $memAvailable;
|
||||
if ($memSize == 0) {
|
||||
Zend_Cache::throwException('can\'t get apc memory size');
|
||||
}
|
||||
if ($memUsed > $memSize) {
|
||||
return 100;
|
||||
}
|
||||
return ((int) (100. * ($memUsed / $memSize)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
* @return array array of stored tags (string)
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingTags($tags = array())
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which don't match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical OR is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of not matching cache ids (string)
|
||||
*/
|
||||
public function getIdsNotMatchingTags($tags = array())
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match any given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of any matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingAnyTags($tags = array())
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds()
|
||||
{
|
||||
$res = array();
|
||||
$array = apc_cache_info('user', false);
|
||||
$records = $array['cache_list'];
|
||||
foreach ($records as $record) {
|
||||
$res[] = $record['info'];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of metadatas for the given cache id
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - expire : the expire timestamp
|
||||
* - tags : a string array of tags
|
||||
* - mtime : timestamp of last modification time
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return array array of metadatas (false if the cache id is not found)
|
||||
*/
|
||||
public function getMetadatas($id)
|
||||
{
|
||||
$tmp = apc_fetch($id);
|
||||
if (is_array($tmp)) {
|
||||
$data = $tmp[0];
|
||||
$mtime = $tmp[1];
|
||||
if (!isset($tmp[2])) {
|
||||
// because this record is only with 1.7 release
|
||||
// if old cache records are still there...
|
||||
return false;
|
||||
}
|
||||
$lifetime = $tmp[2];
|
||||
return array(
|
||||
'expire' => $mtime + $lifetime,
|
||||
'tags' => array(),
|
||||
'mtime' => $mtime
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param int $extraLifetime
|
||||
* @return boolean true if ok
|
||||
*/
|
||||
public function touch($id, $extraLifetime)
|
||||
{
|
||||
$tmp = apc_fetch($id);
|
||||
if (is_array($tmp)) {
|
||||
$data = $tmp[0];
|
||||
$mtime = $tmp[1];
|
||||
if (!isset($tmp[2])) {
|
||||
// because this record is only with 1.7 release
|
||||
// if old cache records are still there...
|
||||
return false;
|
||||
}
|
||||
$lifetime = $tmp[2];
|
||||
$newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
|
||||
if ($newLifetime <=0) {
|
||||
return false;
|
||||
}
|
||||
apc_store($id, array($data, time(), $newLifetime), $newLifetime);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an associative array of capabilities (booleans) of the backend
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - automatic_cleaning (is automating cleaning necessary)
|
||||
* - tags (are tags supported)
|
||||
* - expired_read (is it possible to read expired cache records
|
||||
* (for doNotTestCacheValidity option for example))
|
||||
* - priority does the backend deal with priority when saving
|
||||
* - infinite_lifetime (is infinite lifetime can work with this backend)
|
||||
* - get_list (is it possible to get the list of cache ids and the complete list of tags)
|
||||
*
|
||||
* @return array associative of with capabilities
|
||||
*/
|
||||
public function getCapabilities()
|
||||
{
|
||||
return array(
|
||||
'automatic_cleaning' => false,
|
||||
'tags' => false,
|
||||
'expired_read' => false,
|
||||
'priority' => false,
|
||||
'infinite_lifetime' => false,
|
||||
'get_list' => true
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/Interface.php';
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Cache_Backend_ExtendedInterface extends Zend_Cache_Backend_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds();
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
* @return array array of stored tags (string)
|
||||
*/
|
||||
public function getTags();
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingTags($tags = array());
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which don't match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical OR is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of not matching cache ids (string)
|
||||
*/
|
||||
public function getIdsNotMatchingTags($tags = array());
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match any given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of any matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingAnyTags($tags = array());
|
||||
|
||||
/**
|
||||
* Return the filling percentage of the backend storage
|
||||
*
|
||||
* @return int integer between 0 and 100
|
||||
*/
|
||||
public function getFillingPercentage();
|
||||
|
||||
/**
|
||||
* Return an array of metadatas for the given cache id
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - expire : the expire timestamp
|
||||
* - tags : a string array of tags
|
||||
* - mtime : timestamp of last modification time
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return array array of metadatas (false if the cache id is not found)
|
||||
*/
|
||||
public function getMetadatas($id);
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param int $extraLifetime
|
||||
* @return boolean true if ok
|
||||
*/
|
||||
public function touch($id, $extraLifetime);
|
||||
|
||||
/**
|
||||
* Return an associative array of capabilities (booleans) of the backend
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - automatic_cleaning (is automating cleaning necessary)
|
||||
* - tags (are tags supported)
|
||||
* - expired_read (is it possible to read expired cache records
|
||||
* (for doNotTestCacheValidity option for example))
|
||||
* - priority does the backend deal with priority when saving
|
||||
* - infinite_lifetime (is infinite lifetime can work with this backend)
|
||||
* - get_list (is it possible to get the list of cache ids and the complete list of tags)
|
||||
*
|
||||
* @return array associative of with capabilities
|
||||
*/
|
||||
public function getCapabilities();
|
||||
|
||||
}
|
||||
@@ -1,999 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/ExtendedInterface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_File extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
|
||||
{
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* =====> (string) cache_dir :
|
||||
* - Directory where to put the cache files
|
||||
*
|
||||
* =====> (boolean) file_locking :
|
||||
* - Enable / disable file_locking
|
||||
* - Can avoid cache corruption under bad circumstances but it doesn't work on multithread
|
||||
* webservers and on NFS filesystems for example
|
||||
*
|
||||
* =====> (boolean) read_control :
|
||||
* - Enable / disable read control
|
||||
* - If enabled, a control key is embeded in cache file and this key is compared with the one
|
||||
* calculated after the reading.
|
||||
*
|
||||
* =====> (string) read_control_type :
|
||||
* - Type of read control (only if read control is enabled). Available values are :
|
||||
* 'md5' for a md5 hash control (best but slowest)
|
||||
* 'crc32' for a crc32 hash control (lightly less safe but faster, better choice)
|
||||
* 'adler32' for an adler32 hash control (excellent choice too, faster than crc32)
|
||||
* 'strlen' for a length only test (fastest)
|
||||
*
|
||||
* =====> (int) hashed_directory_level :
|
||||
* - Hashed directory level
|
||||
* - Set the hashed directory structure level. 0 means "no hashed directory
|
||||
* structure", 1 means "one level of directory", 2 means "two levels"...
|
||||
* This option can speed up the cache only when you have many thousands of
|
||||
* cache file. Only specific benchs can help you to choose the perfect value
|
||||
* for you. Maybe, 1 or 2 is a good start.
|
||||
*
|
||||
* =====> (int) hashed_directory_umask :
|
||||
* - Umask for hashed directory structure
|
||||
*
|
||||
* =====> (string) file_name_prefix :
|
||||
* - prefix for cache files
|
||||
* - be really carefull with this option because a too generic value in a system cache dir
|
||||
* (like /tmp) can cause disasters when cleaning the cache
|
||||
*
|
||||
* =====> (int) cache_file_umask :
|
||||
* - Umask for cache files
|
||||
*
|
||||
* =====> (int) metatadatas_array_max_size :
|
||||
* - max size for the metadatas array (don't change this value unless you
|
||||
* know what you are doing)
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
'cache_dir' => null,
|
||||
'file_locking' => true,
|
||||
'read_control' => true,
|
||||
'read_control_type' => 'crc32',
|
||||
'hashed_directory_level' => 0,
|
||||
'hashed_directory_umask' => 0700,
|
||||
'file_name_prefix' => 'zend_cache',
|
||||
'cache_file_umask' => 0600,
|
||||
'metadatas_array_max_size' => 100
|
||||
);
|
||||
|
||||
/**
|
||||
* Array of metadatas (each item is an associative array)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_metadatasArray = array();
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
parent::__construct($options);
|
||||
if ($this->_options['cache_dir'] !== null) { // particular case for this option
|
||||
$this->setCacheDir($this->_options['cache_dir']);
|
||||
} else {
|
||||
$this->setCacheDir(self::getTmpDir() . DIRECTORY_SEPARATOR, false);
|
||||
}
|
||||
if (isset($this->_options['file_name_prefix'])) { // particular case for this option
|
||||
if (!preg_match('~^[\w]+$~', $this->_options['file_name_prefix'])) {
|
||||
Zend_Cache::throwException('Invalid file_name_prefix : must use only [a-zA-A0-9_]');
|
||||
}
|
||||
}
|
||||
if ($this->_options['metadatas_array_max_size'] < 10) {
|
||||
Zend_Cache::throwException('Invalid metadatas_array_max_size, must be > 10');
|
||||
}
|
||||
if (isset($options['hashed_directory_umask']) && is_string($options['hashed_directory_umask'])) {
|
||||
// See #ZF-4422
|
||||
$this->_options['hashed_directory_umask'] = octdec($this->_options['hashed_directory_umask']);
|
||||
}
|
||||
if (isset($options['cache_file_umask']) && is_string($options['cache_file_umask'])) {
|
||||
// See #ZF-4422
|
||||
$this->_options['cache_file_umask'] = octdec($this->_options['cache_file_umask']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cache_dir (particular case of setOption() method)
|
||||
*
|
||||
* @param string $value
|
||||
* @param boolean $trailingSeparator If true, add a trailing separator is necessary
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function setCacheDir($value, $trailingSeparator = true)
|
||||
{
|
||||
if (!is_dir($value)) {
|
||||
Zend_Cache::throwException('cache_dir must be a directory');
|
||||
}
|
||||
if (!is_writable($value)) {
|
||||
Zend_Cache::throwException('cache_dir is not writable');
|
||||
}
|
||||
if ($trailingSeparator) {
|
||||
// add a trailing DIRECTORY_SEPARATOR if necessary
|
||||
$value = rtrim(realpath($value), '\\/') . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
$this->_options['cache_dir'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
|
||||
* @return string|false cached datas
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
if (!($this->_test($id, $doNotTestCacheValidity))) {
|
||||
// The cache is not hit !
|
||||
return false;
|
||||
}
|
||||
$metadatas = $this->_getMetadatas($id);
|
||||
$file = $this->_file($id);
|
||||
$data = $this->_fileGetContents($file);
|
||||
if ($this->_options['read_control']) {
|
||||
$hashData = $this->_hash($data, $this->_options['read_control_type']);
|
||||
$hashControl = $metadatas['hash'];
|
||||
if ($hashData != $hashControl) {
|
||||
// Problem detected by the read control !
|
||||
$this->_log('Zend_Cache_Backend_File::load() / read_control : stored hash and computed hash do not match');
|
||||
$this->remove($id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
clearstatcache();
|
||||
return $this->_test($id, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data Datas to cache
|
||||
* @param string $id Cache id
|
||||
* @param array $tags Array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false)
|
||||
{
|
||||
clearstatcache();
|
||||
$file = $this->_file($id);
|
||||
$path = $this->_path($id);
|
||||
if ($this->_options['hashed_directory_level'] > 0) {
|
||||
if (!is_writable($path)) {
|
||||
// maybe, we just have to build the directory structure
|
||||
$this->_recursiveMkdirAndChmod($id);
|
||||
}
|
||||
if (!is_writable($path)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ($this->_options['read_control']) {
|
||||
$hash = $this->_hash($data, $this->_options['read_control_type']);
|
||||
} else {
|
||||
$hash = '';
|
||||
}
|
||||
$metadatas = array(
|
||||
'hash' => $hash,
|
||||
'mtime' => time(),
|
||||
'expire' => $this->_expireTime($this->getLifetime($specificLifetime)),
|
||||
'tags' => $tags
|
||||
);
|
||||
$res = $this->_setMetadatas($id, $metadatas);
|
||||
if (!$res) {
|
||||
$this->_log('Zend_Cache_Backend_File::save() / error on saving metadata');
|
||||
return false;
|
||||
}
|
||||
$res = $this->_filePutContents($file, $data);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
$file = $this->_file($id);
|
||||
$boolRemove = $this->_remove($file);
|
||||
$boolMetadata = $this->_delMetadatas($id);
|
||||
return $boolMetadata && $boolRemove;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* 'all' (default) => remove all cache entries ($tags is not used)
|
||||
* 'old' => remove too old cache entries ($tags is not used)
|
||||
* 'matchingTag' => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* 'notMatchingTag' => remove cache entries not matching one of the given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* 'matchingAnyTag' => remove cache entries matching any given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $mode clean mode
|
||||
* @param tags array $tags array of tags
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
// We use this protected method to hide the recursive stuff
|
||||
clearstatcache();
|
||||
return $this->_clean($this->_options['cache_dir'], $mode, $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds()
|
||||
{
|
||||
return $this->_get($this->_options['cache_dir'], 'ids', array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
* @return array array of stored tags (string)
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
return $this->_get($this->_options['cache_dir'], 'tags', array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingTags($tags = array())
|
||||
{
|
||||
return $this->_get($this->_options['cache_dir'], 'matching', $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which don't match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical OR is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of not matching cache ids (string)
|
||||
*/
|
||||
public function getIdsNotMatchingTags($tags = array())
|
||||
{
|
||||
return $this->_get($this->_options['cache_dir'], 'notMatching', $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match any given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of any matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingAnyTags($tags = array())
|
||||
{
|
||||
return $this->_get($this->_options['cache_dir'], 'matchingAny', $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the filling percentage of the backend storage
|
||||
*
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return int integer between 0 and 100
|
||||
*/
|
||||
public function getFillingPercentage()
|
||||
{
|
||||
$free = disk_free_space($this->_options['cache_dir']);
|
||||
$total = disk_total_space($this->_options['cache_dir']);
|
||||
if ($total == 0) {
|
||||
Zend_Cache::throwException('can\'t get disk_total_space');
|
||||
} else {
|
||||
if ($free >= $total) {
|
||||
return 100;
|
||||
}
|
||||
return ((int) (100. * ($total - $free) / $total));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of metadatas for the given cache id
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - expire : the expire timestamp
|
||||
* - tags : a string array of tags
|
||||
* - mtime : timestamp of last modification time
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return array array of metadatas (false if the cache id is not found)
|
||||
*/
|
||||
public function getMetadatas($id)
|
||||
{
|
||||
$metadatas = $this->_getMetadatas($id);
|
||||
if (!$metadatas) {
|
||||
return false;
|
||||
}
|
||||
if (time() > $metadatas['expire']) {
|
||||
return false;
|
||||
}
|
||||
return array(
|
||||
'expire' => $metadatas['expire'],
|
||||
'tags' => $metadatas['tags'],
|
||||
'mtime' => $metadatas['mtime']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param int $extraLifetime
|
||||
* @return boolean true if ok
|
||||
*/
|
||||
public function touch($id, $extraLifetime)
|
||||
{
|
||||
$metadatas = $this->_getMetadatas($id);
|
||||
if (!$metadatas) {
|
||||
return false;
|
||||
}
|
||||
if (time() > $metadatas['expire']) {
|
||||
return false;
|
||||
}
|
||||
$newMetadatas = array(
|
||||
'hash' => $metadatas['hash'],
|
||||
'mtime' => time(),
|
||||
'expire' => $metadatas['expire'] + $extraLifetime,
|
||||
'tags' => $metadatas['tags']
|
||||
);
|
||||
$res = $this->_setMetadatas($id, $newMetadatas);
|
||||
if (!$res) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an associative array of capabilities (booleans) of the backend
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - automatic_cleaning (is automating cleaning necessary)
|
||||
* - tags (are tags supported)
|
||||
* - expired_read (is it possible to read expired cache records
|
||||
* (for doNotTestCacheValidity option for example))
|
||||
* - priority does the backend deal with priority when saving
|
||||
* - infinite_lifetime (is infinite lifetime can work with this backend)
|
||||
* - get_list (is it possible to get the list of cache ids and the complete list of tags)
|
||||
*
|
||||
* @return array associative of with capabilities
|
||||
*/
|
||||
public function getCapabilities()
|
||||
{
|
||||
return array(
|
||||
'automatic_cleaning' => true,
|
||||
'tags' => true,
|
||||
'expired_read' => true,
|
||||
'priority' => false,
|
||||
'infinite_lifetime' => true,
|
||||
'get_list' => true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* PUBLIC METHOD FOR UNIT TESTING ONLY !
|
||||
*
|
||||
* Force a cache record to expire
|
||||
*
|
||||
* @param string $id cache id
|
||||
*/
|
||||
public function ___expire($id)
|
||||
{
|
||||
$metadatas = $this->_getMetadatas($id);
|
||||
if ($metadatas) {
|
||||
$metadatas['expire'] = 1;
|
||||
$this->_setMetadatas($id, $metadatas);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a metadatas record
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return array|false Associative array of metadatas
|
||||
*/
|
||||
protected function _getMetadatas($id)
|
||||
{
|
||||
if (isset($this->_metadatasArray[$id])) {
|
||||
return $this->_metadatasArray[$id];
|
||||
} else {
|
||||
$metadatas = $this->_loadMetadatas($id);
|
||||
if (!$metadatas) {
|
||||
return false;
|
||||
}
|
||||
$this->_setMetadatas($id, $metadatas, false);
|
||||
return $metadatas;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a metadatas record
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param array $metadatas Associative array of metadatas
|
||||
* @param boolean $save optional pass false to disable saving to file
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
protected function _setMetadatas($id, $metadatas, $save = true)
|
||||
{
|
||||
if (count($this->_metadatasArray) >= $this->_options['metadatas_array_max_size']) {
|
||||
$n = (int) ($this->_options['metadatas_array_max_size'] / 10);
|
||||
$this->_metadatasArray = array_slice($this->_metadatasArray, $n);
|
||||
}
|
||||
if ($save) {
|
||||
$result = $this->_saveMetadatas($id, $metadatas);
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$this->_metadatasArray[$id] = $metadatas;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop a metadata record
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
protected function _delMetadatas($id)
|
||||
{
|
||||
if (isset($this->_metadatasArray[$id])) {
|
||||
unset($this->_metadatasArray[$id]);
|
||||
}
|
||||
$file = $this->_metadatasFile($id);
|
||||
return $this->_remove($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the metadatas array
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _cleanMetadatas()
|
||||
{
|
||||
$this->_metadatasArray = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load metadatas from disk
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return array|false Metadatas associative array
|
||||
*/
|
||||
protected function _loadMetadatas($id)
|
||||
{
|
||||
$file = $this->_metadatasFile($id);
|
||||
$result = $this->_fileGetContents($file);
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
$tmp = @unserialize($result);
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save metadatas to disk
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param array $metadatas Associative array
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
protected function _saveMetadatas($id, $metadatas)
|
||||
{
|
||||
$file = $this->_metadatasFile($id);
|
||||
$result = $this->_filePutContents($file, serialize($metadatas));
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make and return a file name (with path) for metadatas
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return string Metadatas file name (with path)
|
||||
*/
|
||||
protected function _metadatasFile($id)
|
||||
{
|
||||
$path = $this->_path($id);
|
||||
$fileName = $this->_idToFileName('internal-metadatas---' . $id);
|
||||
return $path . $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given filename is a metadatas one
|
||||
*
|
||||
* @param string $fileName File name
|
||||
* @return boolean True if it's a metadatas one
|
||||
*/
|
||||
protected function _isMetadatasFile($fileName)
|
||||
{
|
||||
$id = $this->_fileNameToId($fileName);
|
||||
if (substr($id, 0, 21) == 'internal-metadatas---') {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a file
|
||||
*
|
||||
* If we can't remove the file (because of locks or any problem), we will touch
|
||||
* the file to invalidate it
|
||||
*
|
||||
* @param string $file Complete file path
|
||||
* @return boolean True if ok
|
||||
*/
|
||||
protected function _remove($file)
|
||||
{
|
||||
if (!is_file($file)) {
|
||||
return false;
|
||||
}
|
||||
if (!@unlink($file)) {
|
||||
# we can't remove the file (because of locks or any problem)
|
||||
$this->_log("Zend_Cache_Backend_File::_remove() : we can't remove $file");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records (protected method used for recursive stuff)
|
||||
*
|
||||
* Available modes are :
|
||||
* Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $dir Directory to clean
|
||||
* @param string $mode Clean mode
|
||||
* @param array $tags Array of tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
protected function _clean($dir, $mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
return false;
|
||||
}
|
||||
$result = true;
|
||||
$prefix = $this->_options['file_name_prefix'];
|
||||
$glob = @glob($dir . $prefix . '--*');
|
||||
if ($glob === false) {
|
||||
return true;
|
||||
}
|
||||
foreach ($glob as $file) {
|
||||
if (is_file($file)) {
|
||||
$fileName = basename($file);
|
||||
if ($this->_isMetadatasFile($fileName)) {
|
||||
// in CLEANING_MODE_ALL, we drop anything, even remainings old metadatas files
|
||||
if ($mode != Zend_Cache::CLEANING_MODE_ALL) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$id = $this->_fileNameToId($fileName);
|
||||
$metadatas = $this->_getMetadatas($id);
|
||||
if ($metadatas === FALSE) {
|
||||
$metadatas = array('expire' => 1, 'tags' => array());
|
||||
}
|
||||
switch ($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
$res = $this->remove($id);
|
||||
if (!$res) {
|
||||
// in this case only, we accept a problem with the metadatas file drop
|
||||
$res = $this->_remove($file);
|
||||
}
|
||||
$result = $result && $res;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_OLD:
|
||||
if (time() > $metadatas['expire']) {
|
||||
$result = $this->remove($id) && $result;
|
||||
}
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
|
||||
$matching = true;
|
||||
foreach ($tags as $tag) {
|
||||
if (!in_array($tag, $metadatas['tags'])) {
|
||||
$matching = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($matching) {
|
||||
$result = $this->remove($id) && $result;
|
||||
}
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
|
||||
$matching = false;
|
||||
foreach ($tags as $tag) {
|
||||
if (in_array($tag, $metadatas['tags'])) {
|
||||
$matching = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$matching) {
|
||||
$result = $this->remove($id) && $result;
|
||||
}
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
|
||||
$matching = false;
|
||||
foreach ($tags as $tag) {
|
||||
if (in_array($tag, $metadatas['tags'])) {
|
||||
$matching = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($matching) {
|
||||
$result = $this->remove($id) && $result;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Zend_Cache::throwException('Invalid mode for clean() method');
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ((is_dir($file)) and ($this->_options['hashed_directory_level']>0)) {
|
||||
// Recursive call
|
||||
$result = $this->_clean($file . DIRECTORY_SEPARATOR, $mode, $tags) && $result;
|
||||
if ($mode=='all') {
|
||||
// if mode=='all', we try to drop the structure too
|
||||
@rmdir($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function _get($dir, $mode, $tags = array())
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
return false;
|
||||
}
|
||||
$result = array();
|
||||
$prefix = $this->_options['file_name_prefix'];
|
||||
$glob = @glob($dir . $prefix . '--*');
|
||||
if ($glob === false) {
|
||||
return true;
|
||||
}
|
||||
foreach ($glob as $file) {
|
||||
if (is_file($file)) {
|
||||
$fileName = basename($file);
|
||||
$id = $this->_fileNameToId($fileName);
|
||||
$metadatas = $this->_getMetadatas($id);
|
||||
if ($metadatas === FALSE) {
|
||||
continue;
|
||||
}
|
||||
if (time() > $metadatas['expire']) {
|
||||
continue;
|
||||
}
|
||||
switch ($mode) {
|
||||
case 'ids':
|
||||
$result[] = $id;
|
||||
break;
|
||||
case 'tags':
|
||||
$result = array_unique(array_merge($result, $metadatas['tags']));
|
||||
break;
|
||||
case 'matching':
|
||||
$matching = true;
|
||||
foreach ($tags as $tag) {
|
||||
if (!in_array($tag, $metadatas['tags'])) {
|
||||
$matching = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($matching) {
|
||||
$result[] = $id;
|
||||
}
|
||||
break;
|
||||
case 'notMatching':
|
||||
$matching = false;
|
||||
foreach ($tags as $tag) {
|
||||
if (in_array($tag, $metadatas['tags'])) {
|
||||
$matching = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$matching) {
|
||||
$result[] = $id;
|
||||
}
|
||||
break;
|
||||
case 'matchingAny':
|
||||
$matching = false;
|
||||
foreach ($tags as $tag) {
|
||||
if (in_array($tag, $metadatas['tags'])) {
|
||||
$matching = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($matching) {
|
||||
$result[] = $id;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Zend_Cache::throwException('Invalid mode for _get() method');
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ((is_dir($file)) and ($this->_options['hashed_directory_level']>0)) {
|
||||
// Recursive call
|
||||
$result = array_unique(array_merge($result, $this->_get($file . DIRECTORY_SEPARATOR, $mode, $tags)));
|
||||
}
|
||||
}
|
||||
return array_unique($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute & return the expire time
|
||||
*
|
||||
* @return int expire time (unix timestamp)
|
||||
*/
|
||||
protected function _expireTime($lifetime)
|
||||
{
|
||||
if ($lifetime === null) {
|
||||
return 9999999999;
|
||||
}
|
||||
return time() + $lifetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a control key with the string containing datas
|
||||
*
|
||||
* @param string $data Data
|
||||
* @param string $controlType Type of control 'md5', 'crc32' or 'strlen'
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return string Control key
|
||||
*/
|
||||
protected function _hash($data, $controlType)
|
||||
{
|
||||
switch ($controlType) {
|
||||
case 'md5':
|
||||
return md5($data);
|
||||
case 'crc32':
|
||||
return crc32($data);
|
||||
case 'strlen':
|
||||
return strlen($data);
|
||||
case 'adler32':
|
||||
return hash('adler32', $data);
|
||||
default:
|
||||
Zend_Cache::throwException("Incorrect hash function : $controlType");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform a cache id into a file name and return it
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return string File name
|
||||
*/
|
||||
protected function _idToFileName($id)
|
||||
{
|
||||
$prefix = $this->_options['file_name_prefix'];
|
||||
$result = $prefix . '---' . $id;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make and return a file name (with path)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return string File name (with path)
|
||||
*/
|
||||
protected function _file($id)
|
||||
{
|
||||
$path = $this->_path($id);
|
||||
$fileName = $this->_idToFileName($id);
|
||||
return $path . $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the complete directory path of a filename (including hashedDirectoryStructure)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $parts if true, returns array of directory parts instead of single string
|
||||
* @return string Complete directory path
|
||||
*/
|
||||
protected function _path($id, $parts = false)
|
||||
{
|
||||
$partsArray = array();
|
||||
$root = $this->_options['cache_dir'];
|
||||
$prefix = $this->_options['file_name_prefix'];
|
||||
if ($this->_options['hashed_directory_level']>0) {
|
||||
$hash = hash('adler32', $id);
|
||||
for ($i=0 ; $i < $this->_options['hashed_directory_level'] ; $i++) {
|
||||
$root = $root . $prefix . '--' . substr($hash, 0, $i + 1) . DIRECTORY_SEPARATOR;
|
||||
$partsArray[] = $root;
|
||||
}
|
||||
}
|
||||
if ($parts) {
|
||||
return $partsArray;
|
||||
} else {
|
||||
return $root;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the directory strucuture for the given id
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return boolean true
|
||||
*/
|
||||
protected function _recursiveMkdirAndChmod($id)
|
||||
{
|
||||
if ($this->_options['hashed_directory_level'] <=0) {
|
||||
return true;
|
||||
}
|
||||
$partsArray = $this->_path($id, true);
|
||||
foreach ($partsArray as $part) {
|
||||
if (!is_dir($part)) {
|
||||
@mkdir($part, $this->_options['hashed_directory_umask']);
|
||||
@chmod($part, $this->_options['hashed_directory_umask']); // see #ZF-320 (this line is required in some configurations)
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the given cache id is available (and still valid as a cache record)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @return boolean|mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
protected function _test($id, $doNotTestCacheValidity)
|
||||
{
|
||||
$metadatas = $this->_getMetadatas($id);
|
||||
if (!$metadatas) {
|
||||
return false;
|
||||
}
|
||||
if ($doNotTestCacheValidity || (time() <= $metadatas['expire'])) {
|
||||
return $metadatas['mtime'];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file content of the given file
|
||||
*
|
||||
* @param string $file File complete path
|
||||
* @return string File content (or false if problem)
|
||||
*/
|
||||
protected function _fileGetContents($file)
|
||||
{
|
||||
$result = false;
|
||||
if (!is_file($file)) {
|
||||
return false;
|
||||
}
|
||||
$f = @fopen($file, 'rb');
|
||||
if ($f) {
|
||||
if ($this->_options['file_locking']) @flock($f, LOCK_SH);
|
||||
$result = stream_get_contents($f);
|
||||
if ($this->_options['file_locking']) @flock($f, LOCK_UN);
|
||||
@fclose($f);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the given string into the given file
|
||||
*
|
||||
* @param string $file File complete path
|
||||
* @param string $string String to put in file
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
protected function _filePutContents($file, $string)
|
||||
{
|
||||
$result = false;
|
||||
$f = @fopen($file, 'ab+');
|
||||
if ($f) {
|
||||
if ($this->_options['file_locking']) @flock($f, LOCK_EX);
|
||||
fseek($f, 0);
|
||||
ftruncate($f, 0);
|
||||
$tmp = @fwrite($f, $string);
|
||||
if (!($tmp === FALSE)) {
|
||||
$result = true;
|
||||
}
|
||||
@fclose($f);
|
||||
}
|
||||
@chmod($file, $this->_options['cache_file_umask']);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform a file name into cache id and return it
|
||||
*
|
||||
* @param string $fileName File name
|
||||
* @return string Cache id
|
||||
*/
|
||||
protected function _fileNameToId($fileName)
|
||||
{
|
||||
$prefix = $this->_options['file_name_prefix'];
|
||||
return preg_replace('~^' . $prefix . '---(.*)$~', '$1', $fileName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Cache_Backend_Interface
|
||||
{
|
||||
/**
|
||||
* Set the frontend directives
|
||||
*
|
||||
* @param array $directives assoc of directives
|
||||
*/
|
||||
public function setDirectives($directives);
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* Note : return value is always "string" (unserialization is done by the core not by the backend)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @return string|false cached datas
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false);
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id);
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data Datas to cache
|
||||
* @param string $id Cache id
|
||||
* @param array $tags Array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false);
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function remove($id);
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $mode Clean mode
|
||||
* @param array $tags Array of tags
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array());
|
||||
|
||||
}
|
||||
@@ -1,502 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/ExtendedInterface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
|
||||
{
|
||||
/**
|
||||
* Default Values
|
||||
*/
|
||||
const DEFAULT_HOST = '127.0.0.1';
|
||||
const DEFAULT_PORT = 11211;
|
||||
const DEFAULT_PERSISTENT = true;
|
||||
const DEFAULT_WEIGHT = 1;
|
||||
const DEFAULT_TIMEOUT = 1;
|
||||
const DEFAULT_RETRY_INTERVAL = 15;
|
||||
const DEFAULT_STATUS = true;
|
||||
const DEFAULT_FAILURE_CALLBACK = null;
|
||||
|
||||
/**
|
||||
* Log message
|
||||
*/
|
||||
const TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::clean() : tags are unsupported by the Memcached backend';
|
||||
const TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::save() : tags are unsupported by the Memcached backend';
|
||||
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* =====> (array) servers :
|
||||
* an array of memcached server ; each memcached server is described by an associative array :
|
||||
* 'host' => (string) : the name of the memcached server
|
||||
* 'port' => (int) : the port of the memcached server
|
||||
* 'persistent' => (bool) : use or not persistent connections to this memcached server
|
||||
* 'weight' => (int) : number of buckets to create for this server which in turn control its
|
||||
* probability of it being selected. The probability is relative to the total
|
||||
* weight of all servers.
|
||||
* 'timeout' => (int) : value in seconds which will be used for connecting to the daemon. Think twice
|
||||
* before changing the default value of 1 second - you can lose all the
|
||||
* advantages of caching if your connection is too slow.
|
||||
* 'retry_interval' => (int) : controls how often a failed server will be retried, the default value
|
||||
* is 15 seconds. Setting this parameter to -1 disables automatic retry.
|
||||
* 'status' => (bool) : controls if the server should be flagged as online.
|
||||
* 'failure_callback' => (callback) : Allows the user to specify a callback function to run upon
|
||||
* encountering an error. The callback is run before failover
|
||||
* is attempted. The function takes two parameters, the hostname
|
||||
* and port of the failed server.
|
||||
*
|
||||
* =====> (boolean) compression :
|
||||
* true if you want to use on-the-fly compression
|
||||
*
|
||||
* =====> (boolean) compatibility :
|
||||
* true if you use old memcache server or extension
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
'servers' => array(array(
|
||||
'host' => self::DEFAULT_HOST,
|
||||
'port' => self::DEFAULT_PORT,
|
||||
'persistent' => self::DEFAULT_PERSISTENT,
|
||||
'weight' => self::DEFAULT_WEIGHT,
|
||||
'timeout' => self::DEFAULT_TIMEOUT,
|
||||
'retry_interval' => self::DEFAULT_RETRY_INTERVAL,
|
||||
'status' => self::DEFAULT_STATUS,
|
||||
'failure_callback' => self::DEFAULT_FAILURE_CALLBACK
|
||||
)),
|
||||
'compression' => false,
|
||||
'compatibility' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* Memcache object
|
||||
*
|
||||
* @var mixed memcache object
|
||||
*/
|
||||
protected $_memcache = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
if (!extension_loaded('memcache')) {
|
||||
Zend_Cache::throwException('The memcache extension must be loaded for using this backend !');
|
||||
}
|
||||
parent::__construct($options);
|
||||
if (isset($this->_options['servers'])) {
|
||||
$value= $this->_options['servers'];
|
||||
if (isset($value['host'])) {
|
||||
// in this case, $value seems to be a simple associative array (one server only)
|
||||
$value = array(0 => $value); // let's transform it into a classical array of associative arrays
|
||||
}
|
||||
$this->setOption('servers', $value);
|
||||
}
|
||||
$this->_memcache = new Memcache;
|
||||
foreach ($this->_options['servers'] as $server) {
|
||||
if (!array_key_exists('port', $server)) {
|
||||
$server['port'] = self::DEFAULT_PORT;
|
||||
}
|
||||
if (!array_key_exists('persistent', $server)) {
|
||||
$server['persistent'] = self::DEFAULT_PERSISTENT;
|
||||
}
|
||||
if (!array_key_exists('weight', $server)) {
|
||||
$server['weight'] = self::DEFAULT_WEIGHT;
|
||||
}
|
||||
if (!array_key_exists('timeout', $server)) {
|
||||
$server['timeout'] = self::DEFAULT_TIMEOUT;
|
||||
}
|
||||
if (!array_key_exists('retry_interval', $server)) {
|
||||
$server['retry_interval'] = self::DEFAULT_RETRY_INTERVAL;
|
||||
}
|
||||
if (!array_key_exists('status', $server)) {
|
||||
$server['status'] = self::DEFAULT_STATUS;
|
||||
}
|
||||
if (!array_key_exists('failure_callback', $server)) {
|
||||
$server['failure_callback'] = self::DEFAULT_FAILURE_CALLBACK;
|
||||
}
|
||||
if ($this->_options['compatibility']) {
|
||||
// No status for compatibility mode (#ZF-5887)
|
||||
$this->_memcache->addServer($server['host'], $server['port'], $server['persistent'],
|
||||
$server['weight'], $server['timeout'],
|
||||
$server['retry_interval']);
|
||||
} else {
|
||||
$this->_memcache->addServer($server['host'], $server['port'], $server['persistent'],
|
||||
$server['weight'], $server['timeout'],
|
||||
$server['retry_interval'],
|
||||
$server['status'], $server['failure_callback']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @return string|false cached datas
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
$tmp = $this->_memcache->get($id);
|
||||
if (is_array($tmp)) {
|
||||
return $tmp[0];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
$tmp = $this->_memcache->get($id);
|
||||
if (is_array($tmp)) {
|
||||
return $tmp[1];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data Datas to cache
|
||||
* @param string $id Cache id
|
||||
* @param array $tags Array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false)
|
||||
{
|
||||
$lifetime = $this->getLifetime($specificLifetime);
|
||||
if ($this->_options['compression']) {
|
||||
$flag = MEMCACHE_COMPRESSED;
|
||||
} else {
|
||||
$flag = 0;
|
||||
}
|
||||
// #ZF-5702 : we try add() first becase set() seems to be slower
|
||||
if (!($result = $this->_memcache->add($id, array($data, time(), $lifetime), $flag, $lifetime))) {
|
||||
$result = $this->_memcache->set($id, array($data, time(), $lifetime), $flag, $lifetime);
|
||||
}
|
||||
if (count($tags) > 0) {
|
||||
$this->_log("Zend_Cache_Backend_Memcached::save() : tags are unsupported by the Memcached backend");
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
return $this->_memcache->delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* 'all' (default) => remove all cache entries ($tags is not used)
|
||||
* 'old' => unsupported
|
||||
* 'matchingTag' => unsupported
|
||||
* 'notMatchingTag' => unsupported
|
||||
* 'matchingAnyTag' => unsupported
|
||||
*
|
||||
* @param string $mode Clean mode
|
||||
* @param array $tags Array of tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
switch ($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
return $this->_memcache->flush();
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_OLD:
|
||||
$this->_log("Zend_Cache_Backend_Memcached::clean() : CLEANING_MODE_OLD is unsupported by the Memcached backend");
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
|
||||
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND);
|
||||
break;
|
||||
default:
|
||||
Zend_Cache::throwException('Invalid mode for clean() method');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the automatic cleaning is available for the backend
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAutomaticCleaningAvailable()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the frontend directives
|
||||
*
|
||||
* @param array $directives Assoc of directives
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function setDirectives($directives)
|
||||
{
|
||||
parent::setDirectives($directives);
|
||||
$lifetime = $this->getLifetime(false);
|
||||
if ($lifetime > 2592000) {
|
||||
// #ZF-3490 : For the memcached backend, there is a lifetime limit of 30 days (2592000 seconds)
|
||||
$this->_log('memcached backend has a limit of 30 days (2592000 seconds) for the lifetime');
|
||||
}
|
||||
if ($lifetime === null) {
|
||||
// #ZF-4614 : we tranform null to zero to get the maximal lifetime
|
||||
parent::setDirectives(array('lifetime' => 0));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds()
|
||||
{
|
||||
$this->_log("Zend_Cache_Backend_Memcached::save() : getting the list of cache ids is unsupported by the Memcache backend");
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
* @return array array of stored tags (string)
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingTags($tags = array())
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which don't match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical OR is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of not matching cache ids (string)
|
||||
*/
|
||||
public function getIdsNotMatchingTags($tags = array())
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match any given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of any matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingAnyTags($tags = array())
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the filling percentage of the backend storage
|
||||
*
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return int integer between 0 and 100
|
||||
*/
|
||||
public function getFillingPercentage()
|
||||
{
|
||||
$mems = $this->_memcache->getExtendedStats();
|
||||
|
||||
$memSize = 0;
|
||||
$memUsed = 0;
|
||||
foreach ($mems as $key => $mem) {
|
||||
if ($mem === false) {
|
||||
Zend_Cache::throwException('can\'t get stat from ' . $key);
|
||||
} else {
|
||||
$eachSize = $mem['limit_maxbytes'];
|
||||
if ($eachSize == 0) {
|
||||
Zend_Cache::throwException('can\'t get memory size from ' . $key);
|
||||
}
|
||||
|
||||
$eachUsed = $mem['bytes'];
|
||||
if ($eachUsed > $eachSize) {
|
||||
$eachUsed = $eachSize;
|
||||
}
|
||||
|
||||
$memSize += $eachSize;
|
||||
$memUsed += $eachUsed;
|
||||
}
|
||||
}
|
||||
|
||||
return ((int) (100. * ($memUsed / $memSize)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of metadatas for the given cache id
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - expire : the expire timestamp
|
||||
* - tags : a string array of tags
|
||||
* - mtime : timestamp of last modification time
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return array array of metadatas (false if the cache id is not found)
|
||||
*/
|
||||
public function getMetadatas($id)
|
||||
{
|
||||
$tmp = $this->_memcache->get($id);
|
||||
if (is_array($tmp)) {
|
||||
$data = $tmp[0];
|
||||
$mtime = $tmp[1];
|
||||
if (!isset($tmp[2])) {
|
||||
// because this record is only with 1.7 release
|
||||
// if old cache records are still there...
|
||||
return false;
|
||||
}
|
||||
$lifetime = $tmp[2];
|
||||
return array(
|
||||
'expire' => $mtime + $lifetime,
|
||||
'tags' => array(),
|
||||
'mtime' => $mtime
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param int $extraLifetime
|
||||
* @return boolean true if ok
|
||||
*/
|
||||
public function touch($id, $extraLifetime)
|
||||
{
|
||||
if ($this->_options['compression']) {
|
||||
$flag = MEMCACHE_COMPRESSED;
|
||||
} else {
|
||||
$flag = 0;
|
||||
}
|
||||
$tmp = $this->_memcache->get($id);
|
||||
if (is_array($tmp)) {
|
||||
$data = $tmp[0];
|
||||
$mtime = $tmp[1];
|
||||
if (!isset($tmp[2])) {
|
||||
// because this record is only with 1.7 release
|
||||
// if old cache records are still there...
|
||||
return false;
|
||||
}
|
||||
$lifetime = $tmp[2];
|
||||
$newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
|
||||
if ($newLifetime <=0) {
|
||||
return false;
|
||||
}
|
||||
// #ZF-5702 : we try replace() first becase set() seems to be slower
|
||||
if (!($result = $this->_memcache->replace($id, array($data, time(), $newLifetime), $flag, $newLifetime))) {
|
||||
$result = $this->_memcache->set($id, array($data, time(), $newLifetime), $flag, $newLifetime);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an associative array of capabilities (booleans) of the backend
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - automatic_cleaning (is automating cleaning necessary)
|
||||
* - tags (are tags supported)
|
||||
* - expired_read (is it possible to read expired cache records
|
||||
* (for doNotTestCacheValidity option for example))
|
||||
* - priority does the backend deal with priority when saving
|
||||
* - infinite_lifetime (is infinite lifetime can work with this backend)
|
||||
* - get_list (is it possible to get the list of cache ids and the complete list of tags)
|
||||
*
|
||||
* @return array associative of with capabilities
|
||||
*/
|
||||
public function getCapabilities()
|
||||
{
|
||||
return array(
|
||||
'automatic_cleaning' => false,
|
||||
'tags' => false,
|
||||
'expired_read' => false,
|
||||
'priority' => false,
|
||||
'infinite_lifetime' => false,
|
||||
'get_list' => false
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,679 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/ExtendedInterface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_Sqlite extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
|
||||
{
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* =====> (string) cache_db_complete_path :
|
||||
* - the complete path (filename included) of the SQLITE database
|
||||
*
|
||||
* ====> (int) automatic_vacuum_factor :
|
||||
* - Disable / Tune the automatic vacuum process
|
||||
* - The automatic vacuum process defragment the database file (and make it smaller)
|
||||
* when a clean() or delete() is called
|
||||
* 0 => no automatic vacuum
|
||||
* 1 => systematic vacuum (when delete() or clean() methods are called)
|
||||
* x (integer) > 1 => automatic vacuum randomly 1 times on x clean() or delete()
|
||||
*
|
||||
* @var array Available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
'cache_db_complete_path' => null,
|
||||
'automatic_vacuum_factor' => 10
|
||||
);
|
||||
|
||||
/**
|
||||
* DB ressource
|
||||
*
|
||||
* @var mixed $_db
|
||||
*/
|
||||
private $_db = null;
|
||||
|
||||
/**
|
||||
* Boolean to store if the structure has benn checked or not
|
||||
*
|
||||
* @var boolean $_structureChecked
|
||||
*/
|
||||
private $_structureChecked = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @throws Zend_cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
parent::__construct($options);
|
||||
if ($this->_options['cache_db_complete_path'] === null) {
|
||||
Zend_Cache::throwException('cache_db_complete_path option has to set');
|
||||
}
|
||||
if (!extension_loaded('sqlite')) {
|
||||
Zend_Cache::throwException("Cannot use SQLite storage because the 'sqlite' extension is not loaded in the current PHP environment");
|
||||
}
|
||||
$this->_getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
@sqlite_close($this->_getConnection());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @return string|false Cached datas
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
$this->_checkAndBuildStructure();
|
||||
$sql = "SELECT content FROM cache WHERE id='$id'";
|
||||
if (!$doNotTestCacheValidity) {
|
||||
$sql = $sql . " AND (expire=0 OR expire>" . time() . ')';
|
||||
}
|
||||
$result = $this->_query($sql);
|
||||
$row = @sqlite_fetch_array($result);
|
||||
if ($row) {
|
||||
return $row['content'];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
$this->_checkAndBuildStructure();
|
||||
$sql = "SELECT lastModified FROM cache WHERE id='$id' AND (expire=0 OR expire>" . time() . ')';
|
||||
$result = $this->_query($sql);
|
||||
$row = @sqlite_fetch_array($result);
|
||||
if ($row) {
|
||||
return ((int) $row['lastModified']);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data Datas to cache
|
||||
* @param string $id Cache id
|
||||
* @param array $tags Array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false)
|
||||
{
|
||||
$this->_checkAndBuildStructure();
|
||||
$lifetime = $this->getLifetime($specificLifetime);
|
||||
$data = @sqlite_escape_string($data);
|
||||
$mktime = time();
|
||||
if ($lifetime === null) {
|
||||
$expire = 0;
|
||||
} else {
|
||||
$expire = $mktime + $lifetime;
|
||||
}
|
||||
$this->_query("DELETE FROM cache WHERE id='$id'");
|
||||
$sql = "INSERT INTO cache (id, content, lastModified, expire) VALUES ('$id', '$data', $mktime, $expire)";
|
||||
$res = $this->_query($sql);
|
||||
if (!$res) {
|
||||
$this->_log("Zend_Cache_Backend_Sqlite::save() : impossible to store the cache id=$id");
|
||||
return false;
|
||||
}
|
||||
$res = true;
|
||||
foreach ($tags as $tag) {
|
||||
$res = $this->_registerTag($id, $tag) && $res;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
$this->_checkAndBuildStructure();
|
||||
$res = $this->_query("SELECT COUNT(*) AS nbr FROM cache WHERE id='$id'");
|
||||
$result1 = @sqlite_fetch_single($res);
|
||||
$result2 = $this->_query("DELETE FROM cache WHERE id='$id'");
|
||||
$result3 = $this->_query("DELETE FROM tag WHERE id='$id'");
|
||||
$this->_automaticVacuum();
|
||||
return ($result1 && $result2 && $result3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $mode Clean mode
|
||||
* @param array $tags Array of tags
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
$this->_checkAndBuildStructure();
|
||||
$return = $this->_clean($mode, $tags);
|
||||
$this->_automaticVacuum();
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds()
|
||||
{
|
||||
$this->_checkAndBuildStructure();
|
||||
$res = $this->_query("SELECT id FROM cache WHERE (expire=0 OR expire>" . time() . ")");
|
||||
$result = array();
|
||||
while ($id = @sqlite_fetch_single($res)) {
|
||||
$result[] = $id;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
* @return array array of stored tags (string)
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
$this->_checkAndBuildStructure();
|
||||
$res = $this->_query("SELECT DISTINCT(name) AS name FROM tag");
|
||||
$result = array();
|
||||
while ($id = @sqlite_fetch_single($res)) {
|
||||
$result[] = $id;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingTags($tags = array())
|
||||
{
|
||||
$first = true;
|
||||
$ids = array();
|
||||
foreach ($tags as $tag) {
|
||||
$res = $this->_query("SELECT DISTINCT(id) AS id FROM tag WHERE name='$tag'");
|
||||
if (!$res) {
|
||||
return array();
|
||||
}
|
||||
$rows = @sqlite_fetch_all($res, SQLITE_ASSOC);
|
||||
$ids2 = array();
|
||||
foreach ($rows as $row) {
|
||||
$ids2[] = $row['id'];
|
||||
}
|
||||
if ($first) {
|
||||
$ids = $ids2;
|
||||
$first = false;
|
||||
} else {
|
||||
$ids = array_intersect($ids, $ids2);
|
||||
}
|
||||
}
|
||||
$result = array();
|
||||
foreach ($ids as $id) {
|
||||
$result[] = $id;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which don't match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical OR is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of not matching cache ids (string)
|
||||
*/
|
||||
public function getIdsNotMatchingTags($tags = array())
|
||||
{
|
||||
$res = $this->_query("SELECT id FROM cache");
|
||||
$rows = @sqlite_fetch_all($res, SQLITE_ASSOC);
|
||||
$result = array();
|
||||
foreach ($rows as $row) {
|
||||
$id = $row['id'];
|
||||
$matching = false;
|
||||
foreach ($tags as $tag) {
|
||||
$res = $this->_query("SELECT COUNT(*) AS nbr FROM tag WHERE name='$tag' AND id='$id'");
|
||||
if (!$res) {
|
||||
return array();
|
||||
}
|
||||
$nbr = (int) @sqlite_fetch_single($res);
|
||||
if ($nbr > 0) {
|
||||
$matching = true;
|
||||
}
|
||||
}
|
||||
if (!$matching) {
|
||||
$result[] = $id;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match any given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of any matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingAnyTags($tags = array())
|
||||
{
|
||||
$first = true;
|
||||
$ids = array();
|
||||
foreach ($tags as $tag) {
|
||||
$res = $this->_query("SELECT DISTINCT(id) AS id FROM tag WHERE name='$tag'");
|
||||
if (!$res) {
|
||||
return array();
|
||||
}
|
||||
$rows = @sqlite_fetch_all($res, SQLITE_ASSOC);
|
||||
$ids2 = array();
|
||||
foreach ($rows as $row) {
|
||||
$ids2[] = $row['id'];
|
||||
}
|
||||
if ($first) {
|
||||
$ids = $ids2;
|
||||
$first = false;
|
||||
} else {
|
||||
$ids = array_merge($ids, $ids2);
|
||||
}
|
||||
}
|
||||
$result = array();
|
||||
foreach ($ids as $id) {
|
||||
$result[] = $id;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the filling percentage of the backend storage
|
||||
*
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return int integer between 0 and 100
|
||||
*/
|
||||
public function getFillingPercentage()
|
||||
{
|
||||
$dir = dirname($this->_options['cache_db_complete_path']);
|
||||
$free = disk_free_space($dir);
|
||||
$total = disk_total_space($dir);
|
||||
if ($total == 0) {
|
||||
Zend_Cache::throwException('can\'t get disk_total_space');
|
||||
} else {
|
||||
if ($free >= $total) {
|
||||
return 100;
|
||||
}
|
||||
return ((int) (100. * ($total - $free) / $total));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of metadatas for the given cache id
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - expire : the expire timestamp
|
||||
* - tags : a string array of tags
|
||||
* - mtime : timestamp of last modification time
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return array array of metadatas (false if the cache id is not found)
|
||||
*/
|
||||
public function getMetadatas($id)
|
||||
{
|
||||
$tags = array();
|
||||
$res = $this->_query("SELECT name FROM tag WHERE id='$id'");
|
||||
if ($res) {
|
||||
$rows = @sqlite_fetch_all($res, SQLITE_ASSOC);
|
||||
foreach ($rows as $row) {
|
||||
$tags[] = $row['name'];
|
||||
}
|
||||
}
|
||||
$this->_query('CREATE TABLE cache (id TEXT PRIMARY KEY, content BLOB, lastModified INTEGER, expire INTEGER)');
|
||||
$res = $this->_query("SELECT lastModified,expire FROM cache WHERE id='$id'");
|
||||
if (!$res) {
|
||||
return false;
|
||||
}
|
||||
$row = @sqlite_fetch_array($res, SQLITE_ASSOC);
|
||||
return array(
|
||||
'tags' => $tags,
|
||||
'mtime' => $row['lastModified'],
|
||||
'expire' => $row['expire']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param int $extraLifetime
|
||||
* @return boolean true if ok
|
||||
*/
|
||||
public function touch($id, $extraLifetime)
|
||||
{
|
||||
$sql = "SELECT expire FROM cache WHERE id='$id' AND (expire=0 OR expire>" . time() . ')';
|
||||
$res = $this->_query($sql);
|
||||
if (!$res) {
|
||||
return false;
|
||||
}
|
||||
$expire = @sqlite_fetch_single($res);
|
||||
$newExpire = $expire + $extraLifetime;
|
||||
$res = $this->_query("UPDATE cache SET lastModified=" . time() . ", expire=$newExpire WHERE id='$id'");
|
||||
if ($res) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an associative array of capabilities (booleans) of the backend
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - automatic_cleaning (is automating cleaning necessary)
|
||||
* - tags (are tags supported)
|
||||
* - expired_read (is it possible to read expired cache records
|
||||
* (for doNotTestCacheValidity option for example))
|
||||
* - priority does the backend deal with priority when saving
|
||||
* - infinite_lifetime (is infinite lifetime can work with this backend)
|
||||
* - get_list (is it possible to get the list of cache ids and the complete list of tags)
|
||||
*
|
||||
* @return array associative of with capabilities
|
||||
*/
|
||||
public function getCapabilities()
|
||||
{
|
||||
return array(
|
||||
'automatic_cleaning' => true,
|
||||
'tags' => true,
|
||||
'expired_read' => true,
|
||||
'priority' => false,
|
||||
'infinite_lifetime' => true,
|
||||
'get_list' => true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* PUBLIC METHOD FOR UNIT TESTING ONLY !
|
||||
*
|
||||
* Force a cache record to expire
|
||||
*
|
||||
* @param string $id Cache id
|
||||
*/
|
||||
public function ___expire($id)
|
||||
{
|
||||
$time = time() - 1;
|
||||
$this->_query("UPDATE cache SET lastModified=$time, expire=$time WHERE id='$id'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the connection resource
|
||||
*
|
||||
* If we are not connected, the connection is made
|
||||
*
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return resource Connection resource
|
||||
*/
|
||||
private function _getConnection()
|
||||
{
|
||||
if (is_resource($this->_db)) {
|
||||
return $this->_db;
|
||||
} else {
|
||||
$this->_db = @sqlite_open($this->_options['cache_db_complete_path']);
|
||||
if (!(is_resource($this->_db))) {
|
||||
Zend_Cache::throwException("Impossible to open " . $this->_options['cache_db_complete_path'] . " cache DB file");
|
||||
}
|
||||
return $this->_db;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an SQL query silently
|
||||
*
|
||||
* @param string $query SQL query
|
||||
* @return mixed|false query results
|
||||
*/
|
||||
private function _query($query)
|
||||
{
|
||||
$db = $this->_getConnection();
|
||||
if (is_resource($db)) {
|
||||
$res = @sqlite_query($db, $query);
|
||||
if ($res === false) {
|
||||
return false;
|
||||
} else {
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deal with the automatic vacuum process
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function _automaticVacuum()
|
||||
{
|
||||
if ($this->_options['automatic_vacuum_factor'] > 0) {
|
||||
$rand = rand(1, $this->_options['automatic_vacuum_factor']);
|
||||
if ($rand == 1) {
|
||||
$this->_query('VACUUM');
|
||||
@sqlite_close($this->_getConnection());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a cache id with the given tag
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param string $tag Tag
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
private function _registerTag($id, $tag) {
|
||||
$res = $this->_query("DELETE FROM TAG WHERE name='$tag' AND id='$id'");
|
||||
$res = $this->_query("INSERT INTO tag (name, id) VALUES ('$tag', '$id')");
|
||||
if (!$res) {
|
||||
$this->_log("Zend_Cache_Backend_Sqlite::_registerTag() : impossible to register tag=$tag on id=$id");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the database structure
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
private function _buildStructure()
|
||||
{
|
||||
$this->_query('DROP INDEX tag_id_index');
|
||||
$this->_query('DROP INDEX tag_name_index');
|
||||
$this->_query('DROP INDEX cache_id_expire_index');
|
||||
$this->_query('DROP TABLE version');
|
||||
$this->_query('DROP TABLE cache');
|
||||
$this->_query('DROP TABLE tag');
|
||||
$this->_query('CREATE TABLE version (num INTEGER PRIMARY KEY)');
|
||||
$this->_query('CREATE TABLE cache (id TEXT PRIMARY KEY, content BLOB, lastModified INTEGER, expire INTEGER)');
|
||||
$this->_query('CREATE TABLE tag (name TEXT, id TEXT)');
|
||||
$this->_query('CREATE INDEX tag_id_index ON tag(id)');
|
||||
$this->_query('CREATE INDEX tag_name_index ON tag(name)');
|
||||
$this->_query('CREATE INDEX cache_id_expire_index ON cache(id, expire)');
|
||||
$this->_query('INSERT INTO version (num) VALUES (1)');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the database structure is ok (with the good version)
|
||||
*
|
||||
* @return boolean True if ok
|
||||
*/
|
||||
private function _checkStructureVersion()
|
||||
{
|
||||
$result = $this->_query("SELECT num FROM version");
|
||||
if (!$result) return false;
|
||||
$row = @sqlite_fetch_array($result);
|
||||
if (!$row) {
|
||||
return false;
|
||||
}
|
||||
if (((int) $row['num']) != 1) {
|
||||
// old cache structure
|
||||
$this->_log('Zend_Cache_Backend_Sqlite::_checkStructureVersion() : old cache structure version detected => the cache is going to be dropped');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $mode Clean mode
|
||||
* @param array $tags Array of tags
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
private function _clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
switch ($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
$res1 = $this->_query('DELETE FROM cache');
|
||||
$res2 = $this->_query('DELETE FROM tag');
|
||||
return $res1 && $res2;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_OLD:
|
||||
$mktime = time();
|
||||
$res1 = $this->_query("DELETE FROM tag WHERE id IN (SELECT id FROM cache WHERE expire>0 AND expire<=$mktime)");
|
||||
$res2 = $this->_query("DELETE FROM cache WHERE expire>0 AND expire<=$mktime");
|
||||
return $res1 && $res2;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
|
||||
$ids = $this->getIdsMatchingTags($tags);
|
||||
$result = true;
|
||||
foreach ($ids as $id) {
|
||||
$result = $this->remove($id) && $result;
|
||||
}
|
||||
return $result;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
|
||||
$ids = $this->getIdsNotMatchingTags($tags);
|
||||
$result = true;
|
||||
foreach ($ids as $id) {
|
||||
$result = $this->remove($id) && $result;
|
||||
}
|
||||
return $result;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
|
||||
$ids = $this->getIdsMatchingAnyTags($tags);
|
||||
$result = true;
|
||||
foreach ($ids as $id) {
|
||||
$result = $this->remove($id) && $result;
|
||||
}
|
||||
return $result;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the database structure is ok (with the good version), if no : build it
|
||||
*
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean True if ok
|
||||
*/
|
||||
private function _checkAndBuildStructure()
|
||||
{
|
||||
if (!($this->_structureChecked)) {
|
||||
if (!$this->_checkStructureVersion()) {
|
||||
$this->_buildStructure();
|
||||
if (!$this->_checkStructureVersion()) {
|
||||
Zend_Cache::throwException("Impossible to build cache structure in " . $this->_options['cache_db_complete_path']);
|
||||
}
|
||||
}
|
||||
$this->_structureChecked = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,270 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/Interface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_Test extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
|
||||
{
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array();
|
||||
|
||||
/**
|
||||
* Frontend or Core directives
|
||||
*
|
||||
* @var array directives
|
||||
*/
|
||||
protected $_directives = array();
|
||||
|
||||
/**
|
||||
* Array to log actions
|
||||
*
|
||||
* @var array $_log
|
||||
*/
|
||||
private $_log = array();
|
||||
|
||||
/**
|
||||
* Current index for log array
|
||||
*
|
||||
* @var int $_index
|
||||
*/
|
||||
private $_index = 0;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options associative array of options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = array())
|
||||
{
|
||||
$this->_addLog('construct', array($options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the frontend directives
|
||||
*
|
||||
* @param array $directives assoc of directives
|
||||
* @return void
|
||||
*/
|
||||
public function setDirectives($directives)
|
||||
{
|
||||
$this->_addLog('setDirectives', array($directives));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* For this test backend only, if $id == 'false', then the method will return false
|
||||
* if $id == 'serialized', the method will return a serialized array
|
||||
* ('foo' else)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @return string Cached datas (or false)
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
$this->_addLog('get', array($id, $doNotTestCacheValidity));
|
||||
if ($id=='false') {
|
||||
return false;
|
||||
}
|
||||
if ($id=='serialized') {
|
||||
return serialize(array('foo'));
|
||||
}
|
||||
if ($id=='serialized2') {
|
||||
return serialize(array('headers' => array(), 'data' => 'foo'));
|
||||
}
|
||||
if (($id=='71769f39054f75894288e397df04e445') or ($id=='615d222619fb20b527168340cebd0578')) {
|
||||
return serialize(array('foo', 'bar'));
|
||||
}
|
||||
if (($id=='8a02d218a5165c467e7a5747cc6bd4b6') or ($id=='648aca1366211d17cbf48e65dc570bee')) {
|
||||
return serialize(array('foo', 'bar'));
|
||||
}
|
||||
return 'foo';
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* For this test backend only, if $id == 'false', then the method will return false
|
||||
* (123456 else)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
$this->_addLog('test', array($id));
|
||||
if ($id=='false') {
|
||||
return false;
|
||||
}
|
||||
if (($id=='d8523b3ee441006261eeffa5c3d3a0a7') or ($id=='3c439c922209e2cb0b54d6deffccd75a')) {
|
||||
return false;
|
||||
}
|
||||
if (($id=='40f649b94977c0a6e76902e2a0b43587') or ($id=='e83249ea22178277d5befc2c5e2e9ace')) {
|
||||
return false;
|
||||
}
|
||||
return 123456;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* For this test backend only, if $id == 'false', then the method will return false
|
||||
* (true else)
|
||||
*
|
||||
* @param string $data Datas to cache
|
||||
* @param string $id Cache id
|
||||
* @param array $tags Array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false)
|
||||
{
|
||||
$this->_addLog('save', array($data, $id, $tags));
|
||||
if ($id=='false') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* For this test backend only, if $id == 'false', then the method will return false
|
||||
* (true else)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
$this->_addLog('remove', array($id));
|
||||
if ($id=='false') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* For this test backend only, if $mode == 'false', then the method will return false
|
||||
* (true else)
|
||||
*
|
||||
* Available modes are :
|
||||
* Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $mode Clean mode
|
||||
* @param array $tags Array of tags
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
$this->_addLog('clean', array($mode, $tags));
|
||||
if ($mode=='false') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last log
|
||||
*
|
||||
* @return string The last log
|
||||
*/
|
||||
public function getLastLog()
|
||||
{
|
||||
return $this->_log[$this->_index - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the log index
|
||||
*
|
||||
* @return int Log index
|
||||
*/
|
||||
public function getLogIndex()
|
||||
{
|
||||
return $this->_index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the complete log array
|
||||
*
|
||||
* @return array Complete log array
|
||||
*/
|
||||
public function getAllLogs()
|
||||
{
|
||||
return $this->_log;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the automatic cleaning is available for the backend
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAutomaticCleaningAvailable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an event to the log array
|
||||
*
|
||||
* @param string $methodName MethodName
|
||||
* @param array $args Arguments
|
||||
* @return void
|
||||
*/
|
||||
private function _addLog($methodName, $args)
|
||||
{
|
||||
$this->_log[$this->_index] = array(
|
||||
'methodName' => $methodName,
|
||||
'args' => $args
|
||||
);
|
||||
$this->_index = $this->_index + 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,506 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_ExtendedInterface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/ExtendedInterface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
|
||||
{
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* =====> (string) slow_backend :
|
||||
* - Slow backend name
|
||||
* - Must implement the Zend_Cache_Backend_ExtendedInterface
|
||||
* - Should provide a big storage
|
||||
*
|
||||
* =====> (string) fast_backend :
|
||||
* - Flow backend name
|
||||
* - Must implement the Zend_Cache_Backend_ExtendedInterface
|
||||
* - Must be much faster than slow_backend
|
||||
*
|
||||
* =====> (array) slow_backend_options :
|
||||
* - Slow backend options (see corresponding backend)
|
||||
*
|
||||
* =====> (array) fast_backend_options :
|
||||
* - Fast backend options (see corresponding backend)
|
||||
*
|
||||
* =====> (int) stats_update_factor :
|
||||
* - Disable / Tune the computation of the fast backend filling percentage
|
||||
* - When saving a record into cache :
|
||||
* 1 => systematic computation of the fast backend filling percentage
|
||||
* x (integer) > 1 => computation of the fast backend filling percentage randomly 1 times on x cache write
|
||||
*
|
||||
* =====> (boolean) slow_backend_custom_naming :
|
||||
* =====> (boolean) fast_backend_custom_naming :
|
||||
* =====> (boolean) slow_backend_autoload :
|
||||
* =====> (boolean) fast_backend_autoload :
|
||||
* - See Zend_Cache::factory() method
|
||||
*
|
||||
* =====> (boolean) auto_refresh_fast_cache
|
||||
* - If true, auto refresh the fast cache when a cache record is hit
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
'slow_backend' => 'File',
|
||||
'fast_backend' => 'Apc',
|
||||
'slow_backend_options' => array(),
|
||||
'fast_backend_options' => array(),
|
||||
'stats_update_factor' => 10,
|
||||
'slow_backend_custom_naming' => false,
|
||||
'fast_backend_custom_naming' => false,
|
||||
'slow_backend_autoload' => false,
|
||||
'fast_backend_autoload' => false,
|
||||
'auto_refresh_fast_cache' => true
|
||||
);
|
||||
|
||||
/**
|
||||
* Slow Backend
|
||||
*
|
||||
* @var Zend_Cache_Backend
|
||||
*/
|
||||
private $_slowBackend;
|
||||
|
||||
/**
|
||||
* Fast Backend
|
||||
*
|
||||
* @var Zend_Cache_Backend
|
||||
*/
|
||||
private $_fastBackend;
|
||||
|
||||
/**
|
||||
* Cache for the fast backend filling percentage
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_fastBackendFillingPercentage = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
parent::__construct($options);
|
||||
if ($this->_options['slow_backend'] === null) {
|
||||
Zend_Cache::throwException('slow_backend option has to set');
|
||||
}
|
||||
if ($this->_options['fast_backend'] === null) {
|
||||
Zend_Cache::throwException('fast_backend option has to set');
|
||||
}
|
||||
$this->_slowBackend = Zend_Cache::_makeBackend($this->_options['slow_backend'], $this->_options['slow_backend_options'], $this->_options['slow_backend_custom_naming'], $this->_options['slow_backend_autoload']);
|
||||
$this->_fastBackend = Zend_Cache::_makeBackend($this->_options['fast_backend'], $this->_options['fast_backend_options'], $this->_options['fast_backend_custom_naming'], $this->_options['fast_backend_autoload']);
|
||||
if (!in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_slowBackend))) {
|
||||
Zend_Cache::throwException('slow_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
|
||||
}
|
||||
if (!in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_fastBackend))) {
|
||||
Zend_Cache::throwException('fast_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
|
||||
}
|
||||
$this->_slowBackend->setDirectives($this->_directives);
|
||||
$this->_fastBackend->setDirectives($this->_directives);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
$fastTest = $this->_fastBackend->test($id);
|
||||
if ($fastTest) {
|
||||
return $fastTest;
|
||||
} else {
|
||||
return $this->_slowBackend->test($id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data Datas to cache
|
||||
* @param string $id Cache id
|
||||
* @param array $tags Array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false, $priority = 8)
|
||||
{
|
||||
$usage = $this->_getFastFillingPercentage('saving');
|
||||
$boolFast = true;
|
||||
$lifetime = $this->getLifetime($specificLifetime);
|
||||
$preparedData = $this->_prepareData($data, $lifetime, $priority);
|
||||
if (($priority > 0) && (10 * $priority >= $usage)) {
|
||||
$fastLifetime = $this->_getFastLifetime($lifetime, $priority);
|
||||
$boolFast = $this->_fastBackend->save($preparedData, $id, array(), $fastLifetime);
|
||||
}
|
||||
$boolSlow = $this->_slowBackend->save($preparedData, $id, $tags, $lifetime);
|
||||
return ($boolFast && $boolSlow);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* Note : return value is always "string" (unserialization is done by the core not by the backend)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @return string|false cached datas
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
$res = $this->_fastBackend->load($id, $doNotTestCacheValidity);
|
||||
if ($res === false) {
|
||||
$res = $this->_slowBackend->load($id, $doNotTestCacheValidity);
|
||||
if ($res === false) {
|
||||
// there is no cache at all for this id
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$array = unserialize($res);
|
||||
// maybe, we have to refresh the fast cache ?
|
||||
if ($this->_options['auto_refresh_fast_cache']) {
|
||||
if ($array['priority'] == 10) {
|
||||
// no need to refresh the fast cache with priority = 10
|
||||
return $array['data'];
|
||||
}
|
||||
$newFastLifetime = $this->_getFastLifetime($array['lifetime'], $array['priority'], time() - $array['expire']);
|
||||
// we have the time to refresh the fast cache
|
||||
$usage = $this->_getFastFillingPercentage('loading');
|
||||
if (($array['priority'] > 0) && (10 * $array['priority'] >= $usage)) {
|
||||
// we can refresh the fast cache
|
||||
$preparedData = $this->_prepareData($array['data'], $array['lifetime'], $array['priority']);
|
||||
$this->_fastBackend->save($preparedData, $id, array(), $newFastLifetime);
|
||||
}
|
||||
}
|
||||
return $array['data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
$boolFast = $this->_fastBackend->remove($id);
|
||||
$boolSlow = $this->_slowBackend->remove($id);
|
||||
return $boolFast && $boolSlow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $mode Clean mode
|
||||
* @param array $tags Array of tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
switch($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
$boolFast = $this->_fastBackend->clean(Zend_Cache::CLEANING_MODE_ALL);
|
||||
$boolSlow = $this->_slowBackend->clean(Zend_Cache::CLEANING_MODE_ALL);
|
||||
return $boolFast && $boolSlow;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_OLD:
|
||||
return $this->_slowBackend->clean(Zend_Cache::CLEANING_MODE_OLD);
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
|
||||
$ids = $this->_slowBackend->getIdsMatchingTags($tags);
|
||||
$res = true;
|
||||
foreach ($ids as $id) {
|
||||
$bool = $this->remove($id);
|
||||
$res = $res && $bool;
|
||||
}
|
||||
return $res;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
|
||||
$ids = $this->_slowBackend->getIdsNotMatchingTags($tags);
|
||||
$res = true;
|
||||
foreach ($ids as $id) {
|
||||
$bool = $this->remove($id);
|
||||
$res = $res && $bool;
|
||||
}
|
||||
return $res;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
|
||||
$ids = $this->_slowBackend->getIdsMatchingAnyTags($tags);
|
||||
$res = true;
|
||||
foreach ($ids as $id) {
|
||||
$bool = $this->remove($id);
|
||||
$res = $res && $bool;
|
||||
}
|
||||
return $res;
|
||||
break;
|
||||
default:
|
||||
Zend_Cache::throwException('Invalid mode for clean() method');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds()
|
||||
{
|
||||
return $this->_slowBackend->getIds();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
* @return array array of stored tags (string)
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
return $this->_slowBackend->getTags();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingTags($tags = array())
|
||||
{
|
||||
return $this->_slowBackend->getIdsMatchingTags($tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which don't match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical OR is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of not matching cache ids (string)
|
||||
*/
|
||||
public function getIdsNotMatchingTags($tags = array())
|
||||
{
|
||||
return $this->_slowBackend->getIdsNotMatchingTags($tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match any given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of any matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingAnyTags($tags = array())
|
||||
{
|
||||
return $this->_slowBackend->getIdsMatchingAnyTags($tags);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the filling percentage of the backend storage
|
||||
*
|
||||
* @return int integer between 0 and 100
|
||||
*/
|
||||
public function getFillingPercentage()
|
||||
{
|
||||
return $this->_slowBackend->getFillingPercentage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of metadatas for the given cache id
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - expire : the expire timestamp
|
||||
* - tags : a string array of tags
|
||||
* - mtime : timestamp of last modification time
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return array array of metadatas (false if the cache id is not found)
|
||||
*/
|
||||
public function getMetadatas($id)
|
||||
{
|
||||
return $this->_slowBackend->getMetadatas($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param int $extraLifetime
|
||||
* @return boolean true if ok
|
||||
*/
|
||||
public function touch($id, $extraLifetime)
|
||||
{
|
||||
return $this->_slowBackend->touch($id, $extraLifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an associative array of capabilities (booleans) of the backend
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - automatic_cleaning (is automating cleaning necessary)
|
||||
* - tags (are tags supported)
|
||||
* - expired_read (is it possible to read expired cache records
|
||||
* (for doNotTestCacheValidity option for example))
|
||||
* - priority does the backend deal with priority when saving
|
||||
* - infinite_lifetime (is infinite lifetime can work with this backend)
|
||||
* - get_list (is it possible to get the list of cache ids and the complete list of tags)
|
||||
*
|
||||
* @return array associative of with capabilities
|
||||
*/
|
||||
public function getCapabilities()
|
||||
{
|
||||
$slowBackendCapabilities = $this->_slowBackend->getCapabilities();
|
||||
return array(
|
||||
'automatic_cleaning' => $slowBackendCapabilities['automatic_cleaning'],
|
||||
'tags' => $slowBackendCapabilities['tags'],
|
||||
'expired_read' => $slowBackendCapabilities['expired_read'],
|
||||
'priority' => $slowBackendCapabilities['priority'],
|
||||
'infinite_lifetime' => $slowBackendCapabilities['infinite_lifetime'],
|
||||
'get_list' => $slowBackendCapabilities['get_list']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a serialized array to store datas and metadatas informations
|
||||
*
|
||||
* @param string $data data to store
|
||||
* @param int $lifetime original lifetime
|
||||
* @param int $priority priority
|
||||
* @return string serialize array to store into cache
|
||||
*/
|
||||
private function _prepareData($data, $lifetime, $priority)
|
||||
{
|
||||
$lt = $lifetime;
|
||||
if ($lt === null) {
|
||||
$lt = 9999999999;
|
||||
}
|
||||
return serialize(array(
|
||||
'data' => $data,
|
||||
'lifetime' => $lifetime,
|
||||
'expire' => time() + $lt,
|
||||
'priority' => $priority
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute and return the lifetime for the fast backend
|
||||
*
|
||||
* @param int $lifetime original lifetime
|
||||
* @param int $priority priority
|
||||
* @param int $maxLifetime maximum lifetime
|
||||
* @return int lifetime for the fast backend
|
||||
*/
|
||||
private function _getFastLifetime($lifetime, $priority, $maxLifetime = null)
|
||||
{
|
||||
if ($lifetime === null) {
|
||||
// if lifetime is null, we have an infinite lifetime
|
||||
// we need to use arbitrary lifetimes
|
||||
$fastLifetime = (int) (2592000 / (11 - $priority));
|
||||
} else {
|
||||
$fastLifetime = (int) ($lifetime / (11 - $priority));
|
||||
}
|
||||
if (($maxLifetime !== null) && ($maxLifetime >= 0)) {
|
||||
if ($fastLifetime > $maxLifetime) {
|
||||
return $maxLifetime;
|
||||
}
|
||||
}
|
||||
return $fastLifetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* PUBLIC METHOD FOR UNIT TESTING ONLY !
|
||||
*
|
||||
* Force a cache record to expire
|
||||
*
|
||||
* @param string $id cache id
|
||||
*/
|
||||
public function ___expire($id)
|
||||
{
|
||||
$this->_fastBackend->remove($id);
|
||||
$this->_slowBackend->___expire($id);
|
||||
}
|
||||
|
||||
private function _getFastFillingPercentage($mode)
|
||||
{
|
||||
|
||||
if ($mode == 'saving') {
|
||||
// mode saving
|
||||
if ($this->_fastBackendFillingPercentage === null) {
|
||||
$this->_fastBackendFillingPercentage = $this->_fastBackend->getFillingPercentage();
|
||||
} else {
|
||||
$rand = rand(1, $this->_options['stats_update_factor']);
|
||||
if ($rand == 1) {
|
||||
// we force a refresh
|
||||
$this->_fastBackendFillingPercentage = $this->_fastBackend->getFillingPercentage();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// mode loading
|
||||
// we compute the percentage only if it's not available in cache
|
||||
if ($this->_fastBackendFillingPercentage === null) {
|
||||
$this->_fastBackendFillingPercentage = $this->_fastBackend->getFillingPercentage();
|
||||
}
|
||||
}
|
||||
return $this->_fastBackendFillingPercentage;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,216 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/Interface.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_Xcache extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* Log message
|
||||
*/
|
||||
const TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND = 'Zend_Cache_Backend_Xcache::clean() : tags are unsupported by the Xcache backend';
|
||||
const TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND = 'Zend_Cache_Backend_Xcache::save() : tags are unsupported by the Xcache backend';
|
||||
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* =====> (string) user :
|
||||
* xcache.admin.user (necessary for the clean() method)
|
||||
*
|
||||
* =====> (string) password :
|
||||
* xcache.admin.pass (clear, not MD5) (necessary for the clean() method)
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
'user' => null,
|
||||
'password' => null
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
if (!extension_loaded('xcache')) {
|
||||
Zend_Cache::throwException('The xcache extension must be loaded for using this backend !');
|
||||
}
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* WARNING $doNotTestCacheValidity=true is unsupported by the Xcache backend
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
|
||||
* @return string cached datas (or false)
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
if ($doNotTestCacheValidity) {
|
||||
$this->_log("Zend_Cache_Backend_Xcache::load() : \$doNotTestCacheValidity=true is unsupported by the Xcache backend");
|
||||
}
|
||||
$tmp = xcache_get($id);
|
||||
if (is_array($tmp)) {
|
||||
return $tmp[0];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
if (xcache_isset($id)) {
|
||||
$tmp = xcache_get($id);
|
||||
if (is_array($tmp)) {
|
||||
return $tmp[1];
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data datas to cache
|
||||
* @param string $id cache id
|
||||
* @param array $tags array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false)
|
||||
{
|
||||
$lifetime = $this->getLifetime($specificLifetime);
|
||||
$result = xcache_set($id, array($data, time()), $lifetime);
|
||||
if (count($tags) > 0) {
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
return xcache_unset($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* 'all' (default) => remove all cache entries ($tags is not used)
|
||||
* 'old' => unsupported
|
||||
* 'matchingTag' => unsupported
|
||||
* 'notMatchingTag' => unsupported
|
||||
* 'matchingAnyTag' => unsupported
|
||||
*
|
||||
* @param string $mode clean mode
|
||||
* @param array $tags array of tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
switch ($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
// Necessary because xcache_clear_cache() need basic authentification
|
||||
$backup = array();
|
||||
if (isset($_SERVER['PHP_AUTH_USER'])) {
|
||||
$backup['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER'];
|
||||
}
|
||||
if (isset($_SERVER['PHP_AUTH_PW'])) {
|
||||
$backup['PHP_AUTH_PW'] = $_SERVER['PHP_AUTH_PW'];
|
||||
}
|
||||
if ($this->_options['user']) {
|
||||
$_SERVER['PHP_AUTH_USER'] = $this->_options['user'];
|
||||
}
|
||||
if ($this->_options['password']) {
|
||||
$_SERVER['PHP_AUTH_PW'] = $this->_options['password'];
|
||||
}
|
||||
xcache_clear_cache(XC_TYPE_VAR, 0);
|
||||
if (isset($backup['PHP_AUTH_USER'])) {
|
||||
$_SERVER['PHP_AUTH_USER'] = $backup['PHP_AUTH_USER'];
|
||||
$_SERVER['PHP_AUTH_PW'] = $backup['PHP_AUTH_PW'];
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_OLD:
|
||||
$this->_log("Zend_Cache_Backend_Xcache::clean() : CLEANING_MODE_OLD is unsupported by the Xcache backend");
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
|
||||
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND);
|
||||
break;
|
||||
default:
|
||||
Zend_Cache::throwException('Invalid mode for clean() method');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the automatic cleaning is available for the backend
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAutomaticCleaningAvailable()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,317 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Backend_Interface
|
||||
*/
|
||||
require_once 'Zend/Cache/Backend/Interface.php';
|
||||
|
||||
|
||||
/**
|
||||
* Impementation of Zend Cache Backend using the Zend Platform (Output Content Caching)
|
||||
*
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_ZendPlatform extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
|
||||
{
|
||||
/**
|
||||
* internal ZP prefix
|
||||
*/
|
||||
const TAGS_PREFIX = "internal_ZPtag:";
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Validate that the Zend Platform is loaded and licensed
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
if (!function_exists('accelerator_license_info')) {
|
||||
Zend_Cache::throwException('The Zend Platform extension must be loaded for using this backend !');
|
||||
}
|
||||
if (!function_exists('accelerator_get_configuration')) {
|
||||
$licenseInfo = accelerator_license_info();
|
||||
Zend_Cache::throwException('The Zend Platform extension is not loaded correctly: '.$licenseInfo['failure_reason']);
|
||||
}
|
||||
$accConf = accelerator_get_configuration();
|
||||
if (@!$accConf['output_cache_licensed']) {
|
||||
Zend_Cache::throwException('The Zend Platform extension does not have the proper license to use content caching features');
|
||||
}
|
||||
if (@!$accConf['output_cache_enabled']) {
|
||||
Zend_Cache::throwException('The Zend Platform content caching feature must be enabled for using this backend, set the \'zend_accelerator.output_cache_enabled\' directive to On !');
|
||||
}
|
||||
if (!is_writable($accConf['output_cache_dir'])) {
|
||||
Zend_Cache::throwException('The cache copies directory \''. ini_get('zend_accelerator.output_cache_dir') .'\' must be writable !');
|
||||
}
|
||||
parent:: __construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @return string Cached data (or false)
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
// doNotTestCacheValidity implemented by giving zero lifetime to the cache
|
||||
if ($doNotTestCacheValidity) {
|
||||
$lifetime = 0;
|
||||
} else {
|
||||
$lifetime = $this->_directives['lifetime'];
|
||||
}
|
||||
$res = output_cache_get($id, $lifetime);
|
||||
if($res) {
|
||||
return $res[0];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
$result = output_cache_get($id, $this->_directives['lifetime']);
|
||||
if ($result) {
|
||||
return $result[1];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data Data to cache
|
||||
* @param string $id Cache id
|
||||
* @param array $tags Array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false)
|
||||
{
|
||||
if (!($specificLifetime === false)) {
|
||||
$this->_log("Zend_Cache_Backend_ZendPlatform::save() : non false specifc lifetime is unsuported for this backend");
|
||||
}
|
||||
|
||||
$lifetime = $this->_directives['lifetime'];
|
||||
$result1 = output_cache_put($id, array($data, time()));
|
||||
$result2 = (count($tags) == 0);
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
$tagid = self::TAGS_PREFIX.$tag;
|
||||
$old_tags = output_cache_get($tagid, $lifetime);
|
||||
if ($old_tags === false) {
|
||||
$old_tags = array();
|
||||
}
|
||||
$old_tags[$id] = $id;
|
||||
output_cache_remove_key($tagid);
|
||||
$result2 = output_cache_put($tagid, $old_tags);
|
||||
}
|
||||
|
||||
return $result1 && $result2;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
return output_cache_remove_key($id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
|
||||
* Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
|
||||
* This mode is not supported in this backend
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => unsupported
|
||||
* Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $mode Clean mode
|
||||
* @param array $tags Array of tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
switch ($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
case Zend_Cache::CLEANING_MODE_OLD:
|
||||
$cache_dir = ini_get('zend_accelerator.output_cache_dir');
|
||||
if (!$cache_dir) {
|
||||
return false;
|
||||
}
|
||||
$cache_dir .= '/.php_cache_api/';
|
||||
return $this->_clean($cache_dir, $mode);
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
|
||||
$idlist = null;
|
||||
foreach ($tags as $tag) {
|
||||
$next_idlist = output_cache_get(self::TAGS_PREFIX.$tag, $this->_directives['lifetime']);
|
||||
if ($idlist) {
|
||||
$idlist = array_intersect_assoc($idlist, $next_idlist);
|
||||
} else {
|
||||
$idlist = $next_idlist;
|
||||
}
|
||||
if (count($idlist) == 0) {
|
||||
// if ID list is already empty - we may skip checking other IDs
|
||||
$idlist = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($idlist) {
|
||||
foreach ($idlist as $id) {
|
||||
output_cache_remove_key($id);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
|
||||
$this->_log("Zend_Cache_Backend_ZendPlatform::clean() : CLEANING_MODE_NOT_MATCHING_TAG is not supported by the Zend Platform backend");
|
||||
return false;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
|
||||
$idlist = null;
|
||||
foreach ($tags as $tag) {
|
||||
$next_idlist = output_cache_get(self::TAGS_PREFIX.$tag, $this->_directives['lifetime']);
|
||||
if ($idlist) {
|
||||
$idlist = array_merge_recursive($idlist, $next_idlist);
|
||||
} else {
|
||||
$idlist = $next_idlist;
|
||||
}
|
||||
if (count($idlist) == 0) {
|
||||
// if ID list is already empty - we may skip checking other IDs
|
||||
$idlist = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($idlist) {
|
||||
foreach ($idlist as $id) {
|
||||
output_cache_remove_key($id);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
Zend_Cache::throwException('Invalid mode for clean() method');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean a directory and recursivly go over it's subdirectories
|
||||
*
|
||||
* Remove all the cached files that need to be cleaned (according to mode and files mtime)
|
||||
*
|
||||
* @param string $dir Path of directory ot clean
|
||||
* @param string $mode The same parameter as in Zend_Cache_Backend_ZendPlatform::clean()
|
||||
* @return boolean True if ok
|
||||
*/
|
||||
private function _clean($dir, $mode)
|
||||
{
|
||||
$d = @dir($dir);
|
||||
if (!$d) {
|
||||
return false;
|
||||
}
|
||||
$result = true;
|
||||
while (false !== ($file = $d->read())) {
|
||||
if ($file == '.' || $file == '..') {
|
||||
continue;
|
||||
}
|
||||
$file = $d->path . $file;
|
||||
if (is_dir($file)) {
|
||||
$result = ($this->_clean($file .'/', $mode)) && ($result);
|
||||
} else {
|
||||
if ($mode == Zend_Cache::CLEANING_MODE_ALL) {
|
||||
$result = ($this->_remove($file)) && ($result);
|
||||
} else if ($mode == Zend_Cache::CLEANING_MODE_OLD) {
|
||||
// Files older than lifetime get deleted from cache
|
||||
if ($this->_directives['lifetime'] !== null) {
|
||||
if ((time() - @filemtime($file)) > $this->_directives['lifetime']) {
|
||||
$result = ($this->_remove($file)) && ($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$d->close();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a file
|
||||
*
|
||||
* If we can't remove the file (because of locks or any problem), we will touch
|
||||
* the file to invalidate it
|
||||
*
|
||||
* @param string $file Complete file path
|
||||
* @return boolean True if ok
|
||||
*/
|
||||
private function _remove($file)
|
||||
{
|
||||
if (!@unlink($file)) {
|
||||
# If we can't remove the file (because of locks or any problem), we will touch
|
||||
# the file to invalidate it
|
||||
$this->_log("Zend_Cache_Backend_ZendPlatform::_remove() : we can't remove $file => we are going to try to invalidate it");
|
||||
if ($this->_directives['lifetime'] === null) {
|
||||
return false;
|
||||
}
|
||||
if (!file_exists($file)) {
|
||||
return false;
|
||||
}
|
||||
return @touch($file, time() - 2*abs($this->_directives['lifetime']));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,207 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/** @see Zend_Cache_Backend_Interface */
|
||||
require_once 'Zend/Cache/Backend/Interface.php';
|
||||
|
||||
/** @see Zend_Cache_Backend */
|
||||
require_once 'Zend/Cache/Backend.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Cache_Backend_ZendServer extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
|
||||
{
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* =====> (string) namespace :
|
||||
* Namespace to be used for chaching operations
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
'namespace' => 'zendframework'
|
||||
);
|
||||
|
||||
/**
|
||||
* Store data
|
||||
*
|
||||
* @param mixed $data Object to store
|
||||
* @param string $id Cache id
|
||||
* @param int $timeToLive Time to live in seconds
|
||||
* @throws Zend_Cache_Exception
|
||||
*/
|
||||
abstract protected function _store($data, $id, $timeToLive);
|
||||
|
||||
/**
|
||||
* Fetch data
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @throws Zend_Cache_Exception
|
||||
*/
|
||||
abstract protected function _fetch($id);
|
||||
|
||||
/**
|
||||
* Unset data
|
||||
*
|
||||
* @param string $id Cache id
|
||||
*/
|
||||
abstract protected function _unset($id);
|
||||
|
||||
/**
|
||||
* Clear cache
|
||||
*/
|
||||
abstract protected function _clear();
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
|
||||
* @return string cached datas (or false)
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false)
|
||||
{
|
||||
$tmp = $this->_fetch($id);
|
||||
if ($tmp !== null) {
|
||||
return $tmp;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
||||
* @throws Zend_Cache_Exception
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
$tmp = $this->_fetch('internal-metadatas---' . $id);
|
||||
if ($tmp !== false) {
|
||||
if (!is_array($tmp) || !isset($tmp['mtime'])) {
|
||||
Zend_Cache::throwException('Cache metadata for \'' . $id . '\' id is corrupted' );
|
||||
}
|
||||
return $tmp['mtime'];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute & return the expire time
|
||||
*
|
||||
* @return int expire time (unix timestamp)
|
||||
*/
|
||||
private function _expireTime($lifetime)
|
||||
{
|
||||
if ($lifetime === null) {
|
||||
return 9999999999;
|
||||
}
|
||||
return time() + $lifetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
* Note : $data is always "string" (serialization is done by the
|
||||
* core not by the backend)
|
||||
*
|
||||
* @param string $data datas to cache
|
||||
* @param string $id cache id
|
||||
* @param array $tags array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false)
|
||||
{
|
||||
$lifetime = $this->getLifetime($specificLifetime);
|
||||
$metadatas = array(
|
||||
'mtime' => time(),
|
||||
'expire' => $this->_expireTime($lifetime),
|
||||
);
|
||||
|
||||
if (count($tags) > 0) {
|
||||
$this->_log('Zend_Cache_Backend_ZendServer::save() : tags are unsupported by the ZendServer backends');
|
||||
}
|
||||
|
||||
return $this->_store($data, $id, $lifetime) &&
|
||||
$this->_store($metadatas, 'internal-metadatas---' . $id, $lifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
$result1 = $this->_unset($id);
|
||||
$result2 = $this->_unset('internal-metadatas---' . $id);
|
||||
|
||||
return $result1 && $result2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
* Available modes are :
|
||||
* 'all' (default) => remove all cache entries ($tags is not used)
|
||||
* 'old' => unsupported
|
||||
* 'matchingTag' => unsupported
|
||||
* 'notMatchingTag' => unsupported
|
||||
* 'matchingAnyTag' => unsupported
|
||||
*
|
||||
* @param string $mode clean mode
|
||||
* @param array $tags array of tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
switch ($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
$this->_clear();
|
||||
return true;
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_OLD:
|
||||
$this->_log("Zend_Cache_Backend_ZendServer::clean() : CLEANING_MODE_OLD is unsupported by the Zend Server backends.");
|
||||
break;
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
|
||||
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
|
||||
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
|
||||
$this->_clear();
|
||||
$this->_log('Zend_Cache_Backend_ZendServer::clean() : tags are unsupported by the Zend Server backends.');
|
||||
break;
|
||||
default:
|
||||
Zend_Cache::throwException('Invalid mode for clean() method');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/** @see Zend_Cache_Backend_Interface */
|
||||
require_once 'Zend/Cache/Backend/Interface.php';
|
||||
|
||||
/** @see Zend_Cache_Backend_ZendServer */
|
||||
require_once 'Zend/Cache/Backend/ZendServer.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_ZendServer_Disk extends Zend_Cache_Backend_ZendServer implements Zend_Cache_Backend_Interface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
if (!function_exists('zend_disk_cache_store')) {
|
||||
Zend_Cache::throwException('Zend_Cache_ZendServer_Disk backend has to be used within Zend Server environment.');
|
||||
}
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store data
|
||||
*
|
||||
* @param mixed $data Object to store
|
||||
* @param string $id Cache id
|
||||
* @param int $timeToLive Time to live in seconds
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
protected function _store($data, $id, $timeToLive)
|
||||
{
|
||||
if (zend_disk_cache_store($this->_options['namespace'] . '::' . $id,
|
||||
$data,
|
||||
$timeToLive) === false) {
|
||||
$this->_log('Store operation failed.');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data
|
||||
*
|
||||
* @param string $id Cache id
|
||||
*/
|
||||
protected function _fetch($id)
|
||||
{
|
||||
return zend_disk_cache_fetch($this->_options['namespace'] . '::' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset data
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
protected function _unset($id)
|
||||
{
|
||||
return zend_disk_cache_delete($this->_options['namespace'] . '::' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear cache
|
||||
*/
|
||||
protected function _clear()
|
||||
{
|
||||
zend_disk_cache_clear($this->_options['namespace']);
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/** @see Zend_Cache_Backend_Interface */
|
||||
require_once 'Zend/Cache/Backend/Interface.php';
|
||||
|
||||
/** @see Zend_Cache_Backend_ZendServer */
|
||||
require_once 'Zend/Cache/Backend/ZendServer.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Backend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Backend_ZendServer_ShMem extends Zend_Cache_Backend_ZendServer implements Zend_Cache_Backend_Interface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
if (!function_exists('zend_shm_cache_store')) {
|
||||
Zend_Cache::throwException('Zend_Cache_ZendServer_ShMem backend has to be used within Zend Server environment.');
|
||||
}
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store data
|
||||
*
|
||||
* @param mixed $data Object to store
|
||||
* @param string $id Cache id
|
||||
* @param int $timeToLive Time to live in seconds
|
||||
*
|
||||
*/
|
||||
protected function _store($data, $id, $timeToLive)
|
||||
{
|
||||
if (zend_shm_cache_store($this->_options['namespace'] . '::' . $id,
|
||||
$data,
|
||||
$timeToLive) === false) {
|
||||
$this->_log('Store operation failed.');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data
|
||||
*
|
||||
* @param string $id Cache id
|
||||
*/
|
||||
protected function _fetch($id)
|
||||
{
|
||||
return zend_shm_cache_fetch($this->_options['namespace'] . '::' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset data
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
protected function _unset($id)
|
||||
{
|
||||
return zend_shm_cache_delete($this->_options['namespace'] . '::' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear cache
|
||||
*/
|
||||
protected function _clear()
|
||||
{
|
||||
zend_shm_cache_clear($this->_options['namespace']);
|
||||
}
|
||||
}
|
||||
@@ -1,683 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Core
|
||||
{
|
||||
/**
|
||||
* Backend Object
|
||||
*
|
||||
* @var object $_backend
|
||||
*/
|
||||
protected $_backend = null;
|
||||
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* ====> (boolean) write_control :
|
||||
* - Enable / disable write control (the cache is read just after writing to detect corrupt entries)
|
||||
* - Enable write control will lightly slow the cache writing but not the cache reading
|
||||
* Write control can detect some corrupt cache files but maybe it's not a perfect control
|
||||
*
|
||||
* ====> (boolean) caching :
|
||||
* - Enable / disable caching
|
||||
* (can be very useful for the debug of cached scripts)
|
||||
*
|
||||
* =====> (string) cache_id_prefix :
|
||||
* - prefix for cache ids (namespace)
|
||||
*
|
||||
* ====> (boolean) automatic_serialization :
|
||||
* - Enable / disable automatic serialization
|
||||
* - It can be used to save directly datas which aren't strings (but it's slower)
|
||||
*
|
||||
* ====> (int) automatic_cleaning_factor :
|
||||
* - Disable / Tune the automatic cleaning process
|
||||
* - The automatic cleaning process destroy too old (for the given life time)
|
||||
* cache files when a new cache file is written :
|
||||
* 0 => no automatic cache cleaning
|
||||
* 1 => systematic cache cleaning
|
||||
* x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
|
||||
*
|
||||
* ====> (int) lifetime :
|
||||
* - Cache lifetime (in seconds)
|
||||
* - If null, the cache is valid forever.
|
||||
*
|
||||
* ====> (boolean) logging :
|
||||
* - If set to true, logging is activated (but the system is slower)
|
||||
*
|
||||
* ====> (boolean) ignore_user_abort
|
||||
* - If set to true, the core will set the ignore_user_abort PHP flag inside the
|
||||
* save() method to avoid cache corruptions in some cases (default false)
|
||||
*
|
||||
* @var array $_options available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
'write_control' => true,
|
||||
'caching' => true,
|
||||
'cache_id_prefix' => null,
|
||||
'automatic_serialization' => false,
|
||||
'automatic_cleaning_factor' => 10,
|
||||
'lifetime' => 3600,
|
||||
'logging' => false,
|
||||
'logger' => null,
|
||||
'ignore_user_abort' => false
|
||||
);
|
||||
|
||||
/**
|
||||
* Array of options which have to be transfered to backend
|
||||
*
|
||||
* @var array $_directivesList
|
||||
*/
|
||||
protected static $_directivesList = array('lifetime', 'logging', 'logger');
|
||||
|
||||
/**
|
||||
* Not used for the core, just a sort a hint to get a common setOption() method (for the core and for frontends)
|
||||
*
|
||||
* @var array $_specificOptions
|
||||
*/
|
||||
protected $_specificOptions = array();
|
||||
|
||||
/**
|
||||
* Last used cache id
|
||||
*
|
||||
* @var string $_lastId
|
||||
*/
|
||||
private $_lastId = null;
|
||||
|
||||
/**
|
||||
* True if the backend implements Zend_Cache_Backend_ExtendedInterface
|
||||
*
|
||||
* @var boolean $_extendedBackend
|
||||
*/
|
||||
protected $_extendedBackend = false;
|
||||
|
||||
/**
|
||||
* Array of capabilities of the backend (only if it implements Zend_Cache_Backend_ExtendedInterface)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_backendCapabilities = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array|Zend_Config $options Associative array of options or Zend_Config instance
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = array())
|
||||
{
|
||||
if ($options instanceof Zend_Config) {
|
||||
$options = $options->toArray();
|
||||
}
|
||||
if (!is_array($options)) {
|
||||
Zend_Cache::throwException("Options passed were not an array"
|
||||
. " or Zend_Config instance.");
|
||||
}
|
||||
while (list($name, $value) = each($options)) {
|
||||
$this->setOption($name, $value);
|
||||
}
|
||||
$this->_loggerSanity();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set options using an instance of type Zend_Config
|
||||
*
|
||||
* @param Zend_Config $config
|
||||
* @return Zend_Cache_Core
|
||||
*/
|
||||
public function setConfig(Zend_Config $config)
|
||||
{
|
||||
$options = $config->toArray();
|
||||
while (list($name, $value) = each($options)) {
|
||||
$this->setOption($name, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the backend
|
||||
*
|
||||
* @param object $backendObject
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function setBackend(Zend_Cache_Backend $backendObject)
|
||||
{
|
||||
$this->_backend= $backendObject;
|
||||
// some options (listed in $_directivesList) have to be given
|
||||
// to the backend too (even if they are not "backend specific")
|
||||
$directives = array();
|
||||
foreach (Zend_Cache_Core::$_directivesList as $directive) {
|
||||
$directives[$directive] = $this->_options[$directive];
|
||||
}
|
||||
$this->_backend->setDirectives($directives);
|
||||
if (in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_backend))) {
|
||||
$this->_extendedBackend = true;
|
||||
$this->_backendCapabilities = $this->_backend->getCapabilities();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the backend
|
||||
*
|
||||
* @return object backend object
|
||||
*/
|
||||
public function getBackend()
|
||||
{
|
||||
return $this->_backend;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public frontend to set an option
|
||||
*
|
||||
* There is an additional validation (relatively to the protected _setOption method)
|
||||
*
|
||||
* @param string $name Name of the option
|
||||
* @param mixed $value Value of the option
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function setOption($name, $value)
|
||||
{
|
||||
if (!is_string($name)) {
|
||||
Zend_Cache::throwException("Incorrect option name : $name");
|
||||
}
|
||||
$name = strtolower($name);
|
||||
if (array_key_exists($name, $this->_options)) {
|
||||
// This is a Core option
|
||||
$this->_setOption($name, $value);
|
||||
return;
|
||||
}
|
||||
if (array_key_exists($name, $this->_specificOptions)) {
|
||||
// This a specic option of this frontend
|
||||
$this->_specificOptions[$name] = $value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Public frontend to get an option value
|
||||
*
|
||||
* @param string $name Name of the option
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return mixed option value
|
||||
*/
|
||||
public function getOption($name)
|
||||
{
|
||||
if (is_string($name)) {
|
||||
$name = strtolower($name);
|
||||
if (array_key_exists($name, $this->_options)) {
|
||||
// This is a Core option
|
||||
return $this->_options[$name];
|
||||
}
|
||||
if (array_key_exists($name, $this->_specificOptions)) {
|
||||
// This a specic option of this frontend
|
||||
return $this->_specificOptions[$name];
|
||||
}
|
||||
}
|
||||
Zend_Cache::throwException("Incorrect option name : $name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an option
|
||||
*
|
||||
* @param string $name Name of the option
|
||||
* @param mixed $value Value of the option
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
private function _setOption($name, $value)
|
||||
{
|
||||
if (!is_string($name) || !array_key_exists($name, $this->_options)) {
|
||||
Zend_Cache::throwException("Incorrect option name : $name");
|
||||
}
|
||||
$this->_options[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Force a new lifetime
|
||||
*
|
||||
* The new value is set for the core/frontend but for the backend too (directive)
|
||||
*
|
||||
* @param int $newLifetime New lifetime (in seconds)
|
||||
* @return void
|
||||
*/
|
||||
public function setLifetime($newLifetime)
|
||||
{
|
||||
$this->_options['lifetime'] = $newLifetime;
|
||||
$this->_backend->setDirectives(array(
|
||||
'lifetime' => $newLifetime
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
|
||||
* @return mixed|false Cached datas
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
|
||||
{
|
||||
if (!$this->_options['caching']) {
|
||||
return false;
|
||||
}
|
||||
$id = $this->_id($id); // cache id may need prefix
|
||||
$this->_lastId = $id;
|
||||
self::_validateIdOrTag($id);
|
||||
$data = $this->_backend->load($id, $doNotTestCacheValidity);
|
||||
if ($data===false) {
|
||||
// no cache available
|
||||
return false;
|
||||
}
|
||||
if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) {
|
||||
// we need to unserialize before sending the result
|
||||
return unserialize($data);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True is a cache is available, false else
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
if (!$this->_options['caching']) {
|
||||
return false;
|
||||
}
|
||||
$id = $this->_id($id); // cache id may need prefix
|
||||
self::_validateIdOrTag($id);
|
||||
$this->_lastId = $id;
|
||||
return $this->_backend->test($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save some data in a cache
|
||||
*
|
||||
* @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on)
|
||||
* @param string $id Cache id (if not set, the last cache id will be used)
|
||||
* @param array $tags Cache tags
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean True if no problem
|
||||
*/
|
||||
public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8)
|
||||
{
|
||||
if (!$this->_options['caching']) {
|
||||
return true;
|
||||
}
|
||||
if ($id === null) {
|
||||
$id = $this->_lastId;
|
||||
} else {
|
||||
$id = $this->_id($id);
|
||||
}
|
||||
self::_validateIdOrTag($id);
|
||||
self::_validateTagsArray($tags);
|
||||
if ($this->_options['automatic_serialization']) {
|
||||
// we need to serialize datas before storing them
|
||||
$data = serialize($data);
|
||||
} else {
|
||||
if (!is_string($data)) {
|
||||
Zend_Cache::throwException("Datas must be string or set automatic_serialization = true");
|
||||
}
|
||||
}
|
||||
// automatic cleaning
|
||||
if ($this->_options['automatic_cleaning_factor'] > 0) {
|
||||
$rand = rand(1, $this->_options['automatic_cleaning_factor']);
|
||||
if ($rand==1) {
|
||||
if ($this->_extendedBackend) {
|
||||
// New way
|
||||
if ($this->_backendCapabilities['automatic_cleaning']) {
|
||||
$this->clean(Zend_Cache::CLEANING_MODE_OLD);
|
||||
} else {
|
||||
$this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
|
||||
}
|
||||
} else {
|
||||
// Deprecated way (will be removed in next major version)
|
||||
if (method_exists($this->_backend, 'isAutomaticCleaningAvailable') && ($this->_backend->isAutomaticCleaningAvailable())) {
|
||||
$this->clean(Zend_Cache::CLEANING_MODE_OLD);
|
||||
} else {
|
||||
$this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($this->_options['ignore_user_abort']) {
|
||||
$abort = ignore_user_abort(true);
|
||||
}
|
||||
if (($this->_extendedBackend) && ($this->_backendCapabilities['priority'])) {
|
||||
$result = $this->_backend->save($data, $id, $tags, $specificLifetime, $priority);
|
||||
} else {
|
||||
$result = $this->_backend->save($data, $id, $tags, $specificLifetime);
|
||||
}
|
||||
if ($this->_options['ignore_user_abort']) {
|
||||
ignore_user_abort($abort);
|
||||
}
|
||||
if (!$result) {
|
||||
// maybe the cache is corrupted, so we remove it !
|
||||
if ($this->_options['logging']) {
|
||||
$this->_log("Zend_Cache_Core::save() : impossible to save cache (id=$id)");
|
||||
}
|
||||
$this->remove($id);
|
||||
return false;
|
||||
}
|
||||
if ($this->_options['write_control']) {
|
||||
$data2 = $this->_backend->load($id, true);
|
||||
if ($data!=$data2) {
|
||||
$this->_log('Zend_Cache_Core::save() / write_control : written and read data do not match');
|
||||
$this->_backend->remove($id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a cache
|
||||
*
|
||||
* @param string $id Cache id to remove
|
||||
* @return boolean True if ok
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
if (!$this->_options['caching']) {
|
||||
return true;
|
||||
}
|
||||
$id = $this->_id($id); // cache id may need prefix
|
||||
self::_validateIdOrTag($id);
|
||||
return $this->_backend->remove($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean cache entries
|
||||
*
|
||||
* Available modes are :
|
||||
* 'all' (default) => remove all cache entries ($tags is not used)
|
||||
* 'old' => remove too old cache entries ($tags is not used)
|
||||
* 'matchingTag' => remove cache entries matching all given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* 'notMatchingTag' => remove cache entries not matching one of the given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
* 'matchingAnyTag' => remove cache entries matching any given tags
|
||||
* ($tags can be an array of strings or a single string)
|
||||
*
|
||||
* @param string $mode
|
||||
* @param array|string $tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean True if ok
|
||||
*/
|
||||
public function clean($mode = 'all', $tags = array())
|
||||
{
|
||||
if (!$this->_options['caching']) {
|
||||
return true;
|
||||
}
|
||||
if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL,
|
||||
Zend_Cache::CLEANING_MODE_OLD,
|
||||
Zend_Cache::CLEANING_MODE_MATCHING_TAG,
|
||||
Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
|
||||
Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) {
|
||||
Zend_Cache::throwException('Invalid cleaning mode');
|
||||
}
|
||||
self::_validateTagsArray($tags);
|
||||
return $this->_backend->clean($mode, $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of matching cache ids (string)
|
||||
*/
|
||||
public function getIdsMatchingTags($tags = array())
|
||||
{
|
||||
if (!$this->_extendedBackend) {
|
||||
Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
|
||||
}
|
||||
if (!($this->_backendCapabilities['tags'])) {
|
||||
Zend_Cache::throwException('tags are not supported by the current backend');
|
||||
}
|
||||
return $this->_backend->getIdsMatchingTags($tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which don't match given tags
|
||||
*
|
||||
* In case of multiple tags, a logical OR is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of not matching cache ids (string)
|
||||
*/
|
||||
public function getIdsNotMatchingTags($tags = array())
|
||||
{
|
||||
if (!$this->_extendedBackend) {
|
||||
Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
|
||||
}
|
||||
if (!($this->_backendCapabilities['tags'])) {
|
||||
Zend_Cache::throwException('tags are not supported by the current backend');
|
||||
}
|
||||
return $this->_backend->getIdsNotMatchingTags($tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds()
|
||||
{
|
||||
if (!$this->_extendedBackend) {
|
||||
Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
|
||||
}
|
||||
$array = $this->_backend->getIds();
|
||||
if ((!isset($this->_options['cache_id_prefix'])) || ($this->_options['cache_id_prefix'] == '')) return $array;
|
||||
// we need to remove cache_id_prefix from ids (see #ZF-6178)
|
||||
$res = array();
|
||||
while (list(,$id) = each($array)) {
|
||||
if (strpos($id, $this->_options['cache_id_prefix']) === 0) {
|
||||
$res[] = preg_replace("~^{$this->_options['cache_id_prefix']}~", '', $id);
|
||||
} else {
|
||||
$res[] = $id;
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
* @return array array of stored tags (string)
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
if (!$this->_extendedBackend) {
|
||||
Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
|
||||
}
|
||||
if (!($this->_backendCapabilities['tags'])) {
|
||||
Zend_Cache::throwException('tags are not supported by the current backend');
|
||||
}
|
||||
return $this->_backend->getTags();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the filling percentage of the backend storage
|
||||
*
|
||||
* @return int integer between 0 and 100
|
||||
*/
|
||||
public function getFillingPercentage()
|
||||
{
|
||||
if (!$this->_extendedBackend) {
|
||||
Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
|
||||
}
|
||||
return $this->_backend->getFillingPercentage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of metadatas for the given cache id
|
||||
*
|
||||
* The array will include these keys :
|
||||
* - expire : the expire timestamp
|
||||
* - tags : a string array of tags
|
||||
* - mtime : timestamp of last modification time
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return array array of metadatas (false if the cache id is not found)
|
||||
*/
|
||||
public function getMetadatas($id)
|
||||
{
|
||||
if (!$this->_extendedBackend) {
|
||||
Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
|
||||
}
|
||||
$id = $this->_id($id); // cache id may need prefix
|
||||
return $this->_backend->getMetadatas($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @param int $extraLifetime
|
||||
* @return boolean true if ok
|
||||
*/
|
||||
public function touch($id, $extraLifetime)
|
||||
{
|
||||
if (!$this->_extendedBackend) {
|
||||
Zend_Cache::throwException('Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available');
|
||||
}
|
||||
$id = $this->_id($id); // cache id may need prefix
|
||||
return $this->_backend->touch($id, $extraLifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
|
||||
*
|
||||
* Throw an exception if a problem is found
|
||||
*
|
||||
* @param string $string Cache id or tag
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected static function _validateIdOrTag($string)
|
||||
{
|
||||
if (!is_string($string)) {
|
||||
Zend_Cache::throwException('Invalid id or tag : must be a string');
|
||||
}
|
||||
if (substr($string, 0, 9) == 'internal-') {
|
||||
Zend_Cache::throwException('"internal-*" ids or tags are reserved');
|
||||
}
|
||||
if (!preg_match('~^[\w]+$~D', $string)) {
|
||||
Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_]");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a tags array (security, reliable filenames, reserved prefixes...)
|
||||
*
|
||||
* Throw an exception if a problem is found
|
||||
*
|
||||
* @param array $tags Array of tags
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected static function _validateTagsArray($tags)
|
||||
{
|
||||
if (!is_array($tags)) {
|
||||
Zend_Cache::throwException('Invalid tags array : must be an array');
|
||||
}
|
||||
foreach($tags as $tag) {
|
||||
self::_validateIdOrTag($tag);
|
||||
}
|
||||
reset($tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure if we enable logging that the Zend_Log class
|
||||
* is available.
|
||||
* Create a default log object if none is set.
|
||||
*
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected function _loggerSanity()
|
||||
{
|
||||
if (!isset($this->_options['logging']) || !$this->_options['logging']) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a default logger to the standard output stream
|
||||
require_once 'Zend/Log/Writer/Stream.php';
|
||||
$logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
|
||||
$this->_options['logger'] = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message at the WARN (4) priority.
|
||||
*
|
||||
* @param string $message
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected function _log($message, $priority = 4)
|
||||
{
|
||||
if (!$this->_options['logging']) {
|
||||
return;
|
||||
}
|
||||
if (!(isset($this->_options['logger']) || $this->_options['logger'] instanceof Zend_Log)) {
|
||||
Zend_Cache::throwException('Logging is enabled but logger is not set');
|
||||
}
|
||||
$logger = $this->_options['logger'];
|
||||
$logger->log($message, $priority);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make and return a cache id
|
||||
*
|
||||
* Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return string Cache id (with or without prefix)
|
||||
*/
|
||||
protected function _id($id)
|
||||
{
|
||||
if (($id !== null) && isset($this->_options['cache_id_prefix'])) {
|
||||
return $this->_options['cache_id_prefix'] . $id; // return with prefix
|
||||
}
|
||||
return $id; // no prefix, just return the $id passed
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Exception
|
||||
*/
|
||||
require_once 'Zend/Exception.php';
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Exception extends Zend_Exception {}
|
||||
@@ -1,243 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Core
|
||||
*/
|
||||
require_once 'Zend/Cache/Core.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Frontend_Class extends Zend_Cache_Core
|
||||
{
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* ====> (mixed) cached_entity :
|
||||
* - if set to a class name, we will cache an abstract class and will use only static calls
|
||||
* - if set to an object, we will cache this object methods
|
||||
*
|
||||
* ====> (boolean) cache_by_default :
|
||||
* - if true, method calls will be cached by default
|
||||
*
|
||||
* ====> (array) cached_methods :
|
||||
* - an array of method names which will be cached (even if cache_by_default = false)
|
||||
*
|
||||
* ====> (array) non_cached_methods :
|
||||
* - an array of method names which won't be cached (even if cache_by_default = true)
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_specificOptions = array(
|
||||
'cached_entity' => null,
|
||||
'cache_by_default' => true,
|
||||
'cached_methods' => array(),
|
||||
'non_cached_methods' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* Tags array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_tags = array();
|
||||
|
||||
/**
|
||||
* SpecificLifetime value
|
||||
*
|
||||
* false => no specific life time
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_specificLifetime = false;
|
||||
|
||||
/**
|
||||
* The cached object or the name of the cached abstract class
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
private $_cachedEntity = null;
|
||||
|
||||
/**
|
||||
* The class name of the cached object or cached abstract class
|
||||
*
|
||||
* Used to differentiate between different classes with the same method calls.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_cachedEntityLabel = '';
|
||||
|
||||
/**
|
||||
* Priority (used by some particular backends)
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_priority = 8;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
while (list($name, $value) = each($options)) {
|
||||
$this->setOption($name, $value);
|
||||
}
|
||||
if ($this->_specificOptions['cached_entity'] === null) {
|
||||
Zend_Cache::throwException('cached_entity must be set !');
|
||||
}
|
||||
$this->setCachedEntity($this->_specificOptions['cached_entity']);
|
||||
$this->setOption('automatic_serialization', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a specific life time
|
||||
*
|
||||
* @param int $specificLifetime
|
||||
* @return void
|
||||
*/
|
||||
public function setSpecificLifetime($specificLifetime = false)
|
||||
{
|
||||
$this->_specificLifetime = $specificLifetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the priority (used by some particular backends)
|
||||
*
|
||||
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority)
|
||||
*/
|
||||
public function setPriority($priority)
|
||||
{
|
||||
$this->_priority = $priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public frontend to set an option
|
||||
*
|
||||
* Just a wrapper to get a specific behaviour for cached_entity
|
||||
*
|
||||
* @param string $name Name of the option
|
||||
* @param mixed $value Value of the option
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function setOption($name, $value)
|
||||
{
|
||||
if ($name == 'cached_entity') {
|
||||
$this->setCachedEntity($value);
|
||||
} else {
|
||||
parent::setOption($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific method to set the cachedEntity
|
||||
*
|
||||
* if set to a class name, we will cache an abstract class and will use only static calls
|
||||
* if set to an object, we will cache this object methods
|
||||
*
|
||||
* @param mixed $cachedEntity
|
||||
*/
|
||||
public function setCachedEntity($cachedEntity)
|
||||
{
|
||||
if (!is_string($cachedEntity) && !is_object($cachedEntity)) {
|
||||
Zend_Cache::throwException('cached_entity must be an object or a class name');
|
||||
}
|
||||
$this->_cachedEntity = $cachedEntity;
|
||||
$this->_specificOptions['cached_entity'] = $cachedEntity;
|
||||
if (is_string($this->_cachedEntity)){
|
||||
$this->_cachedEntityLabel = $this->_cachedEntity;
|
||||
} else {
|
||||
$ro = new ReflectionObject($this->_cachedEntity);
|
||||
$this->_cachedEntityLabel = $ro->getName();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cache array
|
||||
*
|
||||
* @param array $tags
|
||||
* @return void
|
||||
*/
|
||||
public function setTagsArray($tags = array())
|
||||
{
|
||||
$this->_tags = $tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method : call the specified method or get the result from cache
|
||||
*
|
||||
* @param string $name Method name
|
||||
* @param array $parameters Method parameters
|
||||
* @return mixed Result
|
||||
*/
|
||||
public function __call($name, $parameters)
|
||||
{
|
||||
$cacheBool1 = $this->_specificOptions['cache_by_default'];
|
||||
$cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
|
||||
$cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
|
||||
$cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
|
||||
if (!$cache) {
|
||||
// We do not have not cache
|
||||
return call_user_func_array(array($this->_cachedEntity, $name), $parameters);
|
||||
}
|
||||
$id = $this->_makeId($name, $parameters);
|
||||
if ($this->test($id)) {
|
||||
// A cache is available
|
||||
$result = $this->load($id);
|
||||
$output = $result[0];
|
||||
$return = $result[1];
|
||||
} else {
|
||||
// A cache is not available
|
||||
ob_start();
|
||||
ob_implicit_flush(false);
|
||||
$return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
|
||||
$output = ob_get_contents();
|
||||
ob_end_clean();
|
||||
$data = array($output, $return);
|
||||
$this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority);
|
||||
}
|
||||
echo $output;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a cache id from the method name and parameters
|
||||
*
|
||||
* @param string $name Method name
|
||||
* @param array $parameters Method parameters
|
||||
* @return string Cache id
|
||||
*/
|
||||
private function _makeId($name, $parameters)
|
||||
{
|
||||
return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($parameters));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,209 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Core
|
||||
*/
|
||||
require_once 'Zend/Cache/Core.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Frontend_File extends Zend_Cache_Core
|
||||
{
|
||||
|
||||
/**
|
||||
* Consts for master_files_mode
|
||||
*/
|
||||
const MODE_AND = 'AND';
|
||||
const MODE_OR = 'OR';
|
||||
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
* ====> (string) master_file :
|
||||
* - a complete path of the master file
|
||||
* - deprecated (see master_files)
|
||||
*
|
||||
* ====> (array) master_files :
|
||||
* - an array of complete path of master files
|
||||
* - this option has to be set !
|
||||
*
|
||||
* ====> (string) master_files_mode :
|
||||
* - Zend_Cache_Frontend_File::MODE_AND or Zend_Cache_Frontend_File::MODE_OR
|
||||
* - if MODE_AND, then all master files have to be touched to get a cache invalidation
|
||||
* - if MODE_OR (default), then a single touched master file is enough to get a cache invalidation
|
||||
*
|
||||
* ====> (boolean) ignore_missing_master_files
|
||||
* - if set to true, missing master files are ignored silently
|
||||
* - if set to false (default), an exception is thrown if there is a missing master file
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_specificOptions = array(
|
||||
'master_file' => null,
|
||||
'master_files' => null,
|
||||
'master_files_mode' => 'OR',
|
||||
'ignore_missing_master_files' => false
|
||||
);
|
||||
|
||||
/**
|
||||
* Master file mtimes
|
||||
*
|
||||
* Array of int
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_masterFile_mtimes = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
while (list($name, $value) = each($options)) {
|
||||
$this->setOption($name, $value);
|
||||
}
|
||||
if (!isset($this->_specificOptions['master_files'])) {
|
||||
Zend_Cache::throwException('master_files option must be set');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the master_file option
|
||||
*
|
||||
* @param string $masterFile the complete path and name of the master file
|
||||
*/
|
||||
public function setMasterFiles($masterFiles)
|
||||
{
|
||||
clearstatcache();
|
||||
$this->_specificOptions['master_file'] = $masterFiles[0]; // to keep a compatibility
|
||||
$this->_specificOptions['master_files'] = $masterFiles;
|
||||
$this->_masterFile_mtimes = array();
|
||||
$i = 0;
|
||||
foreach ($masterFiles as $masterFile) {
|
||||
$this->_masterFile_mtimes[$i] = @filemtime($masterFile);
|
||||
if ((!($this->_specificOptions['ignore_missing_master_files'])) && (!($this->_masterFile_mtimes[$i]))) {
|
||||
Zend_Cache::throwException('Unable to read master_file : '.$masterFile);
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the master_file option
|
||||
*
|
||||
* To keep the compatibility
|
||||
*
|
||||
* @deprecated
|
||||
* @param string $masterFile the complete path and name of the master file
|
||||
*/
|
||||
public function setMasterFile($masterFile)
|
||||
{
|
||||
$this->setMasterFiles(array(0 => $masterFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* Public frontend to set an option
|
||||
*
|
||||
* Just a wrapper to get a specific behaviour for master_file
|
||||
*
|
||||
* @param string $name Name of the option
|
||||
* @param mixed $value Value of the option
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function setOption($name, $value)
|
||||
{
|
||||
if ($name == 'master_file') {
|
||||
$this->setMasterFile($value);
|
||||
} else if ($name == 'master_files') {
|
||||
$this->setMasterFiles($value);
|
||||
} else {
|
||||
parent::setOption($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
|
||||
* @return mixed|false Cached datas
|
||||
*/
|
||||
public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
|
||||
{
|
||||
if (!$doNotTestCacheValidity) {
|
||||
if ($this->test($id)) {
|
||||
return parent::load($id, true, $doNotUnserialize);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return parent::load($id, true, $doNotUnserialize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @return boolean True is a cache is available, false else
|
||||
*/
|
||||
public function test($id)
|
||||
{
|
||||
$lastModified = parent::test($id);
|
||||
if ($lastModified) {
|
||||
if ($this->_specificOptions['master_files_mode'] == self::MODE_AND) {
|
||||
// MODE_AND
|
||||
foreach($this->_masterFile_mtimes as $masterFileMTime) {
|
||||
if ($masterFileMTime) {
|
||||
if ($lastModified > $masterFileMTime) {
|
||||
return $lastModified;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// MODE_OR
|
||||
$res = true;
|
||||
foreach($this->_masterFile_mtimes as $masterFileMTime) {
|
||||
if ($masterFileMTime) {
|
||||
if ($lastModified <= $masterFileMTime) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $lastModified;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Core
|
||||
*/
|
||||
require_once 'Zend/Cache/Core.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Frontend_Function extends Zend_Cache_Core
|
||||
{
|
||||
/**
|
||||
* This frontend specific options
|
||||
*
|
||||
* ====> (boolean) cache_by_default :
|
||||
* - if true, function calls will be cached by default
|
||||
*
|
||||
* ====> (array) cached_functions :
|
||||
* - an array of function names which will be cached (even if cache_by_default = false)
|
||||
*
|
||||
* ====> (array) non_cached_functions :
|
||||
* - an array of function names which won't be cached (even if cache_by_default = true)
|
||||
*
|
||||
* @var array options
|
||||
*/
|
||||
protected $_specificOptions = array(
|
||||
'cache_by_default' => true,
|
||||
'cached_functions' => array(),
|
||||
'non_cached_functions' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
while (list($name, $value) = each($options)) {
|
||||
$this->setOption($name, $value);
|
||||
}
|
||||
$this->setOption('automatic_serialization', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method : call the specified function or get the result from cache
|
||||
*
|
||||
* @param string $name Function name
|
||||
* @param array $parameters Function parameters
|
||||
* @param array $tags Cache tags
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
|
||||
* @return mixed Result
|
||||
*/
|
||||
public function call($name, $parameters = array(), $tags = array(), $specificLifetime = false, $priority = 8)
|
||||
{
|
||||
$cacheBool1 = $this->_specificOptions['cache_by_default'];
|
||||
$cacheBool2 = in_array($name, $this->_specificOptions['cached_functions']);
|
||||
$cacheBool3 = in_array($name, $this->_specificOptions['non_cached_functions']);
|
||||
$cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
|
||||
if (!$cache) {
|
||||
// We do not have not cache
|
||||
return call_user_func_array($name, $parameters);
|
||||
}
|
||||
$id = $this->_makeId($name, $parameters);
|
||||
if ($this->test($id)) {
|
||||
// A cache is available
|
||||
$result = $this->load($id);
|
||||
$output = $result[0];
|
||||
$return = $result[1];
|
||||
} else {
|
||||
// A cache is not available
|
||||
ob_start();
|
||||
ob_implicit_flush(false);
|
||||
$return = call_user_func_array($name, $parameters);
|
||||
$output = ob_get_contents();
|
||||
ob_end_clean();
|
||||
$data = array($output, $return);
|
||||
$this->save($data, $id, $tags, $specificLifetime, $priority);
|
||||
}
|
||||
echo $output;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a cache id from the function name and parameters
|
||||
*
|
||||
* @param string $name Function name
|
||||
* @param array $parameters Function parameters
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return string Cache id
|
||||
*/
|
||||
private function _makeId($name, $parameters)
|
||||
{
|
||||
if (!is_string($name)) {
|
||||
Zend_Cache::throwException('Incorrect function name');
|
||||
}
|
||||
if (!is_array($parameters)) {
|
||||
Zend_Cache::throwException('parameters argument must be an array');
|
||||
}
|
||||
return md5($name . serialize($parameters));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Core
|
||||
*/
|
||||
require_once 'Zend/Cache/Core.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Frontend_Output extends Zend_Cache_Core
|
||||
{
|
||||
|
||||
private $_idStack = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
parent::__construct($options);
|
||||
$this->_idStack = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the cache
|
||||
*
|
||||
* @param string $id Cache id
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @param boolean $echoData If set to true, datas are sent to the browser if the cache is hit (simpy returned else)
|
||||
* @return mixed True if the cache is hit (false else) with $echoData=true (default) ; string else (datas)
|
||||
*/
|
||||
public function start($id, $doNotTestCacheValidity = false, $echoData = true)
|
||||
{
|
||||
$data = $this->load($id, $doNotTestCacheValidity);
|
||||
if ($data !== false) {
|
||||
if ( $echoData ) {
|
||||
echo($data);
|
||||
return true;
|
||||
} else {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
ob_start();
|
||||
ob_implicit_flush(false);
|
||||
$this->_idStack[] = $id;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the cache
|
||||
*
|
||||
* @param array $tags Tags array
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @param string $forcedDatas If not null, force written datas with this
|
||||
* @param boolean $echoData If set to true, datas are sent to the browser
|
||||
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
|
||||
* @return void
|
||||
*/
|
||||
public function end($tags = array(), $specificLifetime = false, $forcedDatas = null, $echoData = true, $priority = 8)
|
||||
{
|
||||
if ($forcedDatas === null) {
|
||||
$data = ob_get_contents();
|
||||
ob_end_clean();
|
||||
} else {
|
||||
$data =& $forcedDatas;
|
||||
}
|
||||
$id = array_pop($this->_idStack);
|
||||
if ($id === null) {
|
||||
Zend_Cache::throwException('use of end() without a start()');
|
||||
}
|
||||
$this->save($data, $id, $tags, $specificLifetime, $priority);
|
||||
if ($echoData) {
|
||||
echo($data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,402 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @see Zend_Cache_Core
|
||||
*/
|
||||
require_once 'Zend/Cache/Core.php';
|
||||
|
||||
|
||||
/**
|
||||
* @package Zend_Cache
|
||||
* @subpackage Zend_Cache_Frontend
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cache_Frontend_Page extends Zend_Cache_Core
|
||||
{
|
||||
/**
|
||||
* This frontend specific options
|
||||
*
|
||||
* ====> (boolean) http_conditional :
|
||||
* - if true, http conditional mode is on
|
||||
* WARNING : http_conditional OPTION IS NOT IMPLEMENTED FOR THE MOMENT (TODO)
|
||||
*
|
||||
* ====> (boolean) debug_header :
|
||||
* - if true, a debug text is added before each cached pages
|
||||
*
|
||||
* ====> (boolean) content_type_memorization :
|
||||
* - deprecated => use memorize_headers instead
|
||||
* - if the Content-Type header is sent after the cache was started, the
|
||||
* corresponding value can be memorized and replayed when the cache is hit
|
||||
* (if false (default), the frontend doesn't take care of Content-Type header)
|
||||
*
|
||||
* ====> (array) memorize_headers :
|
||||
* - an array of strings corresponding to some HTTP headers name. Listed headers
|
||||
* will be stored with cache datas and "replayed" when the cache is hit
|
||||
*
|
||||
* ====> (array) default_options :
|
||||
* - an associative array of default options :
|
||||
* - (boolean) cache : cache is on by default if true
|
||||
* - (boolean) cacheWithXXXVariables (XXXX = 'Get', 'Post', 'Session', 'Files' or 'Cookie') :
|
||||
* if true, cache is still on even if there are some variables in this superglobal array
|
||||
* if false, cache is off if there are some variables in this superglobal array
|
||||
* - (boolean) makeIdWithXXXVariables (XXXX = 'Get', 'Post', 'Session', 'Files' or 'Cookie') :
|
||||
* if true, we have to use the content of this superglobal array to make a cache id
|
||||
* if false, the cache id won't be dependent of the content of this superglobal array
|
||||
* - (int) specific_lifetime : cache specific lifetime
|
||||
* (false => global lifetime is used, null => infinite lifetime,
|
||||
* integer => this lifetime is used), this "lifetime" is probably only
|
||||
* usefull when used with "regexps" array
|
||||
* - (array) tags : array of tags (strings)
|
||||
* - (int) priority : integer between 0 (very low priority) and 10 (maximum priority) used by
|
||||
* some particular backends
|
||||
*
|
||||
* ====> (array) regexps :
|
||||
* - an associative array to set options only for some REQUEST_URI
|
||||
* - keys are (pcre) regexps
|
||||
* - values are associative array with specific options to set if the regexp matchs on $_SERVER['REQUEST_URI']
|
||||
* (see default_options for the list of available options)
|
||||
* - if several regexps match the $_SERVER['REQUEST_URI'], only the last one will be used
|
||||
*
|
||||
* @var array options
|
||||
*/
|
||||
protected $_specificOptions = array(
|
||||
'http_conditional' => false,
|
||||
'debug_header' => false,
|
||||
'content_type_memorization' => false,
|
||||
'memorize_headers' => array(),
|
||||
'default_options' => array(
|
||||
'cache_with_get_variables' => false,
|
||||
'cache_with_post_variables' => false,
|
||||
'cache_with_session_variables' => false,
|
||||
'cache_with_files_variables' => false,
|
||||
'cache_with_cookie_variables' => false,
|
||||
'make_id_with_get_variables' => true,
|
||||
'make_id_with_post_variables' => true,
|
||||
'make_id_with_session_variables' => true,
|
||||
'make_id_with_files_variables' => true,
|
||||
'make_id_with_cookie_variables' => true,
|
||||
'cache' => true,
|
||||
'specific_lifetime' => false,
|
||||
'tags' => array(),
|
||||
'priority' => null
|
||||
),
|
||||
'regexps' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* Internal array to store some options
|
||||
*
|
||||
* @var array associative array of options
|
||||
*/
|
||||
protected $_activeOptions = array();
|
||||
|
||||
/**
|
||||
* If true, the page won't be cached
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_cancel = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $options Associative array of options
|
||||
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
while (list($name, $value) = each($options)) {
|
||||
$name = strtolower($name);
|
||||
switch ($name) {
|
||||
case 'regexps':
|
||||
$this->_setRegexps($value);
|
||||
break;
|
||||
case 'default_options':
|
||||
$this->_setDefaultOptions($value);
|
||||
break;
|
||||
case 'content_type_memorization':
|
||||
$this->_setContentTypeMemorization($value);
|
||||
break;
|
||||
default:
|
||||
$this->setOption($name, $value);
|
||||
}
|
||||
}
|
||||
if (isset($this->_specificOptions['http_conditional'])) {
|
||||
if ($this->_specificOptions['http_conditional']) {
|
||||
Zend_Cache::throwException('http_conditional is not implemented for the moment !');
|
||||
}
|
||||
}
|
||||
$this->setOption('automatic_serialization', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific setter for the 'default_options' option (with some additional tests)
|
||||
*
|
||||
* @param array $options Associative array
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected function _setDefaultOptions($options)
|
||||
{
|
||||
if (!is_array($options)) {
|
||||
Zend_Cache::throwException('default_options must be an array !');
|
||||
}
|
||||
foreach ($options as $key=>$value) {
|
||||
if (!is_string($key)) {
|
||||
Zend_Cache::throwException("invalid option [$key] !");
|
||||
}
|
||||
$key = strtolower($key);
|
||||
if (isset($this->_specificOptions['default_options'][$key])) {
|
||||
$this->_specificOptions['default_options'][$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the deprecated contentTypeMemorization option
|
||||
*
|
||||
* @param boolean $value value
|
||||
* @return void
|
||||
* @deprecated
|
||||
*/
|
||||
protected function _setContentTypeMemorization($value)
|
||||
{
|
||||
$found = null;
|
||||
foreach ($this->_specificOptions['memorize_headers'] as $key => $value) {
|
||||
if (strtolower($value) == 'content-type') {
|
||||
$found = $key;
|
||||
}
|
||||
}
|
||||
if ($value) {
|
||||
if (!$found) {
|
||||
$this->_specificOptions['memorize_headers'][] = 'Content-Type';
|
||||
}
|
||||
} else {
|
||||
if ($found) {
|
||||
unset($this->_specificOptions['memorize_headers'][$found]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific setter for the 'regexps' option (with some additional tests)
|
||||
*
|
||||
* @param array $options Associative array
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected function _setRegexps($regexps)
|
||||
{
|
||||
if (!is_array($regexps)) {
|
||||
Zend_Cache::throwException('regexps option must be an array !');
|
||||
}
|
||||
foreach ($regexps as $regexp=>$conf) {
|
||||
if (!is_array($conf)) {
|
||||
Zend_Cache::throwException('regexps option must be an array of arrays !');
|
||||
}
|
||||
$validKeys = array_keys($this->_specificOptions['default_options']);
|
||||
foreach ($conf as $key=>$value) {
|
||||
if (!is_string($key)) {
|
||||
Zend_Cache::throwException("unknown option [$key] !");
|
||||
}
|
||||
$key = strtolower($key);
|
||||
if (!in_array($key, $validKeys)) {
|
||||
unset($regexps[$regexp][$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->setOption('regexps', $regexps);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the cache
|
||||
*
|
||||
* @param string $id (optional) A cache id (if you set a value here, maybe you have to use Output frontend instead)
|
||||
* @param boolean $doNotDie For unit testing only !
|
||||
* @return boolean True if the cache is hit (false else)
|
||||
*/
|
||||
public function start($id = false, $doNotDie = false)
|
||||
{
|
||||
$this->_cancel = false;
|
||||
$lastMatchingRegexp = null;
|
||||
foreach ($this->_specificOptions['regexps'] as $regexp => $conf) {
|
||||
if (preg_match("`$regexp`", $_SERVER['REQUEST_URI'])) {
|
||||
$lastMatchingRegexp = $regexp;
|
||||
}
|
||||
}
|
||||
$this->_activeOptions = $this->_specificOptions['default_options'];
|
||||
if ($lastMatchingRegexp !== null) {
|
||||
$conf = $this->_specificOptions['regexps'][$lastMatchingRegexp];
|
||||
foreach ($conf as $key=>$value) {
|
||||
$this->_activeOptions[$key] = $value;
|
||||
}
|
||||
}
|
||||
if (!($this->_activeOptions['cache'])) {
|
||||
return false;
|
||||
}
|
||||
if (!$id) {
|
||||
$id = $this->_makeId();
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$array = $this->load($id);
|
||||
if ($array !== false) {
|
||||
$data = $array['data'];
|
||||
$headers = $array['headers'];
|
||||
if (!headers_sent()) {
|
||||
foreach ($headers as $key=>$headerCouple) {
|
||||
$name = $headerCouple[0];
|
||||
$value = $headerCouple[1];
|
||||
header("$name: $value");
|
||||
}
|
||||
}
|
||||
if ($this->_specificOptions['debug_header']) {
|
||||
echo 'DEBUG HEADER : This is a cached page !';
|
||||
}
|
||||
echo $data;
|
||||
if ($doNotDie) {
|
||||
return true;
|
||||
}
|
||||
die();
|
||||
}
|
||||
ob_start(array($this, '_flush'));
|
||||
ob_implicit_flush(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the current caching process
|
||||
*/
|
||||
public function cancel()
|
||||
{
|
||||
$this->_cancel = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* callback for output buffering
|
||||
* (shouldn't really be called manually)
|
||||
*
|
||||
* @param string $data Buffered output
|
||||
* @return string Data to send to browser
|
||||
*/
|
||||
public function _flush($data)
|
||||
{
|
||||
if ($this->_cancel) {
|
||||
return $data;
|
||||
}
|
||||
$contentType = null;
|
||||
$storedHeaders = array();
|
||||
$headersList = headers_list();
|
||||
foreach($this->_specificOptions['memorize_headers'] as $key=>$headerName) {
|
||||
foreach ($headersList as $headerSent) {
|
||||
$tmp = explode(':', $headerSent);
|
||||
$headerSentName = trim(array_shift($tmp));
|
||||
if (strtolower($headerName) == strtolower($headerSentName)) {
|
||||
$headerSentValue = trim(implode(':', $tmp));
|
||||
$storedHeaders[] = array($headerSentName, $headerSentValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
$array = array(
|
||||
'data' => $data,
|
||||
'headers' => $storedHeaders
|
||||
);
|
||||
$this->save($array, null, $this->_activeOptions['tags'], $this->_activeOptions['specific_lifetime'], $this->_activeOptions['priority']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an id depending on REQUEST_URI and superglobal arrays (depending on options)
|
||||
*
|
||||
* @return mixed|false a cache id (string), false if the cache should have not to be used
|
||||
*/
|
||||
protected function _makeId()
|
||||
{
|
||||
$tmp = $_SERVER['REQUEST_URI'];
|
||||
$array = explode('?', $tmp, 2);
|
||||
$tmp = $array[0];
|
||||
foreach (array('Get', 'Post', 'Session', 'Files', 'Cookie') as $arrayName) {
|
||||
$tmp2 = $this->_makePartialId($arrayName, $this->_activeOptions['cache_with_' . strtolower($arrayName) . '_variables'], $this->_activeOptions['make_id_with_' . strtolower($arrayName) . '_variables']);
|
||||
if ($tmp2===false) {
|
||||
return false;
|
||||
}
|
||||
$tmp = $tmp . $tmp2;
|
||||
}
|
||||
return md5($tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a partial id depending on options
|
||||
*
|
||||
* @param string $arrayName Superglobal array name
|
||||
* @param bool $bool1 If true, cache is still on even if there are some variables in the superglobal array
|
||||
* @param bool $bool2 If true, we have to use the content of the superglobal array to make a partial id
|
||||
* @return mixed|false Partial id (string) or false if the cache should have not to be used
|
||||
*/
|
||||
protected function _makePartialId($arrayName, $bool1, $bool2)
|
||||
{
|
||||
switch ($arrayName) {
|
||||
case 'Get':
|
||||
$var = $_GET;
|
||||
break;
|
||||
case 'Post':
|
||||
$var = $_POST;
|
||||
break;
|
||||
case 'Session':
|
||||
if (isset($_SESSION)) {
|
||||
$var = $_SESSION;
|
||||
} else {
|
||||
$var = null;
|
||||
}
|
||||
break;
|
||||
case 'Cookie':
|
||||
if (isset($_COOKIE)) {
|
||||
$var = $_COOKIE;
|
||||
} else {
|
||||
$var = null;
|
||||
}
|
||||
break;
|
||||
case 'Files':
|
||||
$var = $_FILES;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if ($bool1) {
|
||||
if ($bool2) {
|
||||
return serialize($var);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
if (count($var) > 0) {
|
||||
return false;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Validate_Interface */
|
||||
require_once 'Zend/Validate/Interface.php';
|
||||
|
||||
/**
|
||||
* Generic Captcha adapter interface
|
||||
*
|
||||
* Each specific captcha implementation should implement this interface
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
interface Zend_Captcha_Adapter extends Zend_Validate_Interface
|
||||
{
|
||||
/**
|
||||
* Generate a new captcha
|
||||
*
|
||||
* @return string new captcha ID
|
||||
*/
|
||||
public function generate();
|
||||
|
||||
/**
|
||||
* Display the captcha
|
||||
*
|
||||
* @param Zend_View_Interface $view
|
||||
* @param mixed $element
|
||||
* @return string
|
||||
*/
|
||||
public function render(Zend_View_Interface $view = null, $element = null);
|
||||
|
||||
/**
|
||||
* Set captcha name
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_Captcha_Adapter
|
||||
*/
|
||||
public function setName($name);
|
||||
|
||||
/**
|
||||
* Get captcha name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Get optional private decorator for this captcha type
|
||||
*
|
||||
* @return Zend_Form_Decorator_Interface|string
|
||||
*/
|
||||
public function getDecorator();
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Captcha_Adapter */
|
||||
require_once 'Zend/Captcha/Adapter.php';
|
||||
|
||||
/** Zend_Validate_Abstract */
|
||||
require_once 'Zend/Validate/Abstract.php';
|
||||
|
||||
/**
|
||||
* Base class for Captcha adapters
|
||||
*
|
||||
* Provides some utility functionality to build on
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
abstract class Zend_Captcha_Base extends Zend_Validate_Abstract implements Zend_Captcha_Adapter
|
||||
{
|
||||
/**
|
||||
* Element name
|
||||
*
|
||||
* Useful to generate/check form fields
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_name;
|
||||
|
||||
/**
|
||||
* Captcha options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_options = array();
|
||||
|
||||
/**
|
||||
* Options to skip when processing options
|
||||
* @var array
|
||||
*/
|
||||
protected $_skipOptions = array(
|
||||
'options',
|
||||
'config',
|
||||
);
|
||||
|
||||
/**
|
||||
* Get name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set name
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->_name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array|Zend_Config $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = null)
|
||||
{
|
||||
// Set options
|
||||
if (is_array($options)) {
|
||||
$this->setOptions($options);
|
||||
} else if ($options instanceof Zend_Config) {
|
||||
$this->setConfig($options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set single option for the object
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return Zend_Form_Element
|
||||
*/
|
||||
public function setOption($key, $value)
|
||||
{
|
||||
if (in_array(strtolower($key), $this->_skipOptions)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$method = 'set' . ucfirst ($key);
|
||||
if (method_exists ($this, $method)) {
|
||||
// Setter exists; use it
|
||||
$this->$method ($value);
|
||||
$this->_options[$key] = $value;
|
||||
} elseif (property_exists($this, $key)) {
|
||||
// Assume it's metadata
|
||||
$this->$key = $value;
|
||||
$this->_options[$key] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set object state from options array
|
||||
*
|
||||
* @param array $options
|
||||
* @return Zend_Form_Element
|
||||
*/
|
||||
public function setOptions($options = null)
|
||||
{
|
||||
foreach ($options as $key => $value) {
|
||||
$this->setOption($key, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve options representing object state
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set object state from config object
|
||||
*
|
||||
* @param Zend_Config $config
|
||||
* @return Zend_Captcha_Base
|
||||
*/
|
||||
public function setConfig(Zend_Config $config)
|
||||
{
|
||||
return $this->setOptions($config->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get optional decorator
|
||||
*
|
||||
* By default, return null, indicating no extra decorator needed.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function getDecorator()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Captcha_Word */
|
||||
require_once 'Zend/Captcha/Word.php';
|
||||
|
||||
/**
|
||||
* Example dumb word-based captcha
|
||||
*
|
||||
* Note that only rendering is necessary for word-based captcha
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
class Zend_Captcha_Dumb extends Zend_Captcha_Word
|
||||
{
|
||||
/**
|
||||
* Render the captcha
|
||||
*
|
||||
* @param Zend_View_Interface $view
|
||||
* @param mixed $element
|
||||
* @return string
|
||||
*/
|
||||
public function render(Zend_View_Interface $view = null, $element = null)
|
||||
{
|
||||
return 'Please type this word backwards: <b>'
|
||||
. strrev($this->getWord())
|
||||
. '</b>';
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id$
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once ('Zend/Exception.php');
|
||||
|
||||
/**
|
||||
* Exception for Zend_Form component.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Captcha_Exception extends Zend_Exception
|
||||
{
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Captcha_Word */
|
||||
require_once 'Zend/Captcha/Word.php';
|
||||
|
||||
/** Zend_Text_Figlet */
|
||||
require_once 'Zend/Text/Figlet.php';
|
||||
|
||||
/**
|
||||
* Captcha based on figlet text rendering service
|
||||
*
|
||||
* Note that this engine seems not to like numbers
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
class Zend_Captcha_Figlet extends Zend_Captcha_Word
|
||||
{
|
||||
/**
|
||||
* Figlet text renderer
|
||||
*
|
||||
* @var Zend_Text_Figlet
|
||||
*/
|
||||
protected $_figlet;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param null|string|array|Zend_Config $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = null)
|
||||
{
|
||||
parent::__construct($options);
|
||||
$this->_figlet = new Zend_Text_Figlet($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate new captcha
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$this->_useNumbers = false;
|
||||
return parent::generate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the captcha
|
||||
*
|
||||
* @param Zend_View_Interface $view
|
||||
* @param mixed $element
|
||||
* @return string
|
||||
*/
|
||||
public function render(Zend_View_Interface $view = null, $element = null)
|
||||
{
|
||||
return '<pre>'
|
||||
. $this->_figlet->render($this->getWord())
|
||||
. "</pre>\n";
|
||||
}
|
||||
}
|
||||
@@ -1,600 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/** Zend_Captcha_Word */
|
||||
require_once 'Zend/Captcha/Word.php';
|
||||
|
||||
/**
|
||||
* Image-based captcha element
|
||||
*
|
||||
* Generates image displaying random word
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Captcha_Image extends Zend_Captcha_Word
|
||||
{
|
||||
/**
|
||||
* Directory for generated images
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_imgDir = "./images/captcha/";
|
||||
|
||||
/**
|
||||
* URL for accessing images
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_imgUrl = "/images/captcha/";
|
||||
|
||||
/**
|
||||
* Image's alt tag content
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_imgAlt = "";
|
||||
|
||||
/**
|
||||
* Image suffix (including dot)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_suffix = ".png";
|
||||
|
||||
/**
|
||||
* Image width
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_width = 200;
|
||||
|
||||
/**
|
||||
* Image height
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_height = 50;
|
||||
|
||||
/**
|
||||
* Font size
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_fsize = 24;
|
||||
|
||||
/**
|
||||
* Image font file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_font;
|
||||
|
||||
/**
|
||||
* Image to use as starting point
|
||||
* Default is blank image. If ptovided, should be PNG image.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_startImage;
|
||||
/**
|
||||
* How frequently to execute garbage collection
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_gcFreq = 10;
|
||||
|
||||
/**
|
||||
* How long to keep generated images
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_expiration = 600;
|
||||
|
||||
/**
|
||||
* Number of noise dots on image
|
||||
* Used twice - before and after transform
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_dotNoiseLevel = 100;
|
||||
/**
|
||||
* Number of noise lines on image
|
||||
* Used twice - before and after transform
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_lineNoiseLevel = 5;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getImgAlt ()
|
||||
{
|
||||
return $this->_imgAlt;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getStartImage ()
|
||||
{
|
||||
return $this->_startImage;
|
||||
}
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getDotNoiseLevel ()
|
||||
{
|
||||
return $this->_dotNoiseLevel;
|
||||
}
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getLineNoiseLevel ()
|
||||
{
|
||||
return $this->_lineNoiseLevel;
|
||||
}
|
||||
/**
|
||||
* Get captcha expiration
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getExpiration()
|
||||
{
|
||||
return $this->_expiration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get garbage collection frequency
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getGcFreq()
|
||||
{
|
||||
return $this->_gcFreq;
|
||||
}
|
||||
/**
|
||||
* Get font to use when generating captcha
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFont()
|
||||
{
|
||||
return $this->_font;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font size
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getFontSize()
|
||||
{
|
||||
return $this->_fsize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get captcha image height
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getHeight()
|
||||
{
|
||||
return $this->_height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get captcha image directory
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getImgDir()
|
||||
{
|
||||
return $this->_imgDir;
|
||||
}
|
||||
/**
|
||||
* Get captcha image base URL
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getImgUrl()
|
||||
{
|
||||
return $this->_imgUrl;
|
||||
}
|
||||
/**
|
||||
* Get captcha image file suffix
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSuffix()
|
||||
{
|
||||
return $this->_suffix;
|
||||
}
|
||||
/**
|
||||
* Get captcha image width
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getWidth()
|
||||
{
|
||||
return $this->_width;
|
||||
}
|
||||
/**
|
||||
* @param string $startImage
|
||||
*/
|
||||
public function setStartImage ($startImage)
|
||||
{
|
||||
$this->_startImage = $startImage;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param int $dotNoiseLevel
|
||||
*/
|
||||
public function setDotNoiseLevel ($dotNoiseLevel)
|
||||
{
|
||||
$this->_dotNoiseLevel = $dotNoiseLevel;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param int $lineNoiseLevel
|
||||
*/
|
||||
public function setLineNoiseLevel ($lineNoiseLevel)
|
||||
{
|
||||
$this->_lineNoiseLevel = $lineNoiseLevel;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha expiration
|
||||
*
|
||||
* @param int $expiration
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setExpiration($expiration)
|
||||
{
|
||||
$this->_expiration = $expiration;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set garbage collection frequency
|
||||
*
|
||||
* @param int $gcFreq
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setGcFreq($gcFreq)
|
||||
{
|
||||
$this->_gcFreq = $gcFreq;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha font
|
||||
*
|
||||
* @param string $font
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setFont($font)
|
||||
{
|
||||
$this->_font = $font;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha font size
|
||||
*
|
||||
* @param int $fsize
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setFontSize($fsize)
|
||||
{
|
||||
$this->_fsize = $fsize;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha image height
|
||||
*
|
||||
* @param int $height
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setHeight($height)
|
||||
{
|
||||
$this->_height = $height;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha image storage directory
|
||||
*
|
||||
* @param string $imgDir
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setImgDir($imgDir)
|
||||
{
|
||||
$this->_imgDir = rtrim($imgDir, "/\\") . '/';
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha image base URL
|
||||
*
|
||||
* @param string $imgUrl
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setImgUrl($imgUrl)
|
||||
{
|
||||
$this->_imgUrl = rtrim($imgUrl, "/\\") . '/';
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param string $imgAlt
|
||||
*/
|
||||
public function setImgAlt ($imgAlt)
|
||||
{
|
||||
$this->_imgAlt = $imgAlt;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captch image filename suffix
|
||||
*
|
||||
* @param string $suffix
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setSuffix($suffix)
|
||||
{
|
||||
$this->_suffix = $suffix;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha image width
|
||||
*
|
||||
* @param int $width
|
||||
* @return Zend_Captcha_Image
|
||||
*/
|
||||
public function setWidth($width)
|
||||
{
|
||||
$this->_width = $width;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate random frequency
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
protected function _randomFreq()
|
||||
{
|
||||
return mt_rand(700000, 1000000) / 15000000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate random phase
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
protected function _randomPhase()
|
||||
{
|
||||
// random phase from 0 to pi
|
||||
return mt_rand(0, 3141592) / 1000000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate random character size
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function _randomSize()
|
||||
{
|
||||
return mt_rand(300, 700) / 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate captcha
|
||||
*
|
||||
* @return string captcha ID
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$id = parent::generate();
|
||||
$tries = 5;
|
||||
// If there's already such file, try creating a new ID
|
||||
while($tries-- && file_exists($this->getImgDir() . $id . $this->getSuffix())) {
|
||||
$id = $this->_generateRandomId();
|
||||
$this->_setId($id);
|
||||
}
|
||||
$this->_generateImage($id, $this->getWord());
|
||||
|
||||
if (mt_rand(1, $this->getGcFreq()) == 1) {
|
||||
$this->_gc();
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate image captcha
|
||||
*
|
||||
* Override this function if you want different image generator
|
||||
* Wave transform from http://www.captcha.ru/captchas/multiwave/
|
||||
*
|
||||
* @param string $id Captcha ID
|
||||
* @param string $word Captcha word
|
||||
*/
|
||||
protected function _generateImage($id, $word)
|
||||
{
|
||||
if (!extension_loaded("gd")) {
|
||||
require_once 'Zend/Captcha/Exception.php';
|
||||
throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension");
|
||||
}
|
||||
|
||||
if (!function_exists("imagepng")) {
|
||||
require_once 'Zend/Captcha/Exception.php';
|
||||
throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support");
|
||||
}
|
||||
|
||||
if (!function_exists("imageftbbox")) {
|
||||
require_once 'Zend/Captcha/Exception.php';
|
||||
throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support");
|
||||
}
|
||||
|
||||
$font = $this->getFont();
|
||||
|
||||
if (empty($font)) {
|
||||
require_once 'Zend/Captcha/Exception.php';
|
||||
throw new Zend_Captcha_Exception("Image CAPTCHA requires font");
|
||||
}
|
||||
|
||||
$w = $this->getWidth();
|
||||
$h = $this->getHeight();
|
||||
$fsize = $this->getFontSize();
|
||||
|
||||
$img_file = $this->getImgDir() . $id . $this->getSuffix();
|
||||
if(empty($this->_startImage)) {
|
||||
$img = imagecreatetruecolor($w, $h);
|
||||
} else {
|
||||
$img = imagecreatefrompng($this->_startImage);
|
||||
if(!$img) {
|
||||
require_once 'Zend/Captcha/Exception.php';
|
||||
throw new Zend_Captcha_Exception("Can not load start image");
|
||||
}
|
||||
$w = imagesx($img);
|
||||
$h = imagesy($img);
|
||||
}
|
||||
$text_color = imagecolorallocate($img, 0, 0, 0);
|
||||
$bg_color = imagecolorallocate($img, 255, 255, 255);
|
||||
imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);
|
||||
$textbox = imageftbbox($fsize, 0, $font, $word);
|
||||
$x = ($w - ($textbox[2] - $textbox[0])) / 2;
|
||||
$y = ($h - ($textbox[7] - $textbox[1])) / 2;
|
||||
imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word);
|
||||
|
||||
// generate noise
|
||||
for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
|
||||
imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
|
||||
}
|
||||
for($i=0; $i<$this->_lineNoiseLevel; $i++) {
|
||||
imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
|
||||
}
|
||||
|
||||
// transformed image
|
||||
$img2 = imagecreatetruecolor($w, $h);
|
||||
$bg_color = imagecolorallocate($img2, 255, 255, 255);
|
||||
imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
|
||||
// apply wave transforms
|
||||
$freq1 = $this->_randomFreq();
|
||||
$freq2 = $this->_randomFreq();
|
||||
$freq3 = $this->_randomFreq();
|
||||
$freq4 = $this->_randomFreq();
|
||||
|
||||
$ph1 = $this->_randomPhase();
|
||||
$ph2 = $this->_randomPhase();
|
||||
$ph3 = $this->_randomPhase();
|
||||
$ph4 = $this->_randomPhase();
|
||||
|
||||
$szx = $this->_randomSize();
|
||||
$szy = $this->_randomSize();
|
||||
|
||||
for ($x = 0; $x < $w; $x++) {
|
||||
for ($y = 0; $y < $h; $y++) {
|
||||
$sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx;
|
||||
$sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy;
|
||||
|
||||
if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
|
||||
continue;
|
||||
} else {
|
||||
$color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF;
|
||||
$color_x = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF;
|
||||
$color_y = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF;
|
||||
$color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;
|
||||
}
|
||||
if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {
|
||||
// ignore background
|
||||
continue;
|
||||
} elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {
|
||||
// transfer inside of the image as-is
|
||||
$newcolor = 0;
|
||||
} else {
|
||||
// do antialiasing for border items
|
||||
$frac_x = $sx-floor($sx);
|
||||
$frac_y = $sy-floor($sy);
|
||||
$frac_x1 = 1-$frac_x;
|
||||
$frac_y1 = 1-$frac_y;
|
||||
|
||||
$newcolor = $color * $frac_x1 * $frac_y1
|
||||
+ $color_x * $frac_x * $frac_y1
|
||||
+ $color_y * $frac_x1 * $frac_y
|
||||
+ $color_xy * $frac_x * $frac_y;
|
||||
}
|
||||
imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));
|
||||
}
|
||||
}
|
||||
|
||||
// generate noise
|
||||
for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
|
||||
imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
|
||||
}
|
||||
for ($i=0; $i<$this->_lineNoiseLevel; $i++) {
|
||||
imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
|
||||
}
|
||||
|
||||
imagepng($img2, $img_file);
|
||||
imagedestroy($img);
|
||||
imagedestroy($img2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove old files from image directory
|
||||
*
|
||||
*/
|
||||
protected function _gc()
|
||||
{
|
||||
$expire = time() - $this->getExpiration();
|
||||
$imgdir = $this->getImgDir();
|
||||
if(!$imgdir || strlen($imgdir) < 2) {
|
||||
// safety guard
|
||||
return;
|
||||
}
|
||||
foreach (new DirectoryIterator($imgdir) as $file) {
|
||||
if (!$file->isDot() && !$file->isDir()) {
|
||||
if ($file->getMTime() < $expire) {
|
||||
unlink($file->getPathname());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the captcha
|
||||
*
|
||||
* @param Zend_View_Interface $view
|
||||
* @param mixed $element
|
||||
* @return string
|
||||
*/
|
||||
public function render(Zend_View_Interface $view = null, $element = null)
|
||||
{
|
||||
return '<img width="'.$this->getWidth().'" height="'.$this->getHeight().'" alt="'.$this->getImgAlt().'" src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '"/><br/>';
|
||||
}
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Captcha_Base */
|
||||
require_once 'Zend/Captcha/Base.php';
|
||||
|
||||
/** Zend_Service_ReCaptcha */
|
||||
require_once 'Zend/Service/ReCaptcha.php';
|
||||
|
||||
/**
|
||||
* ReCaptcha adapter
|
||||
*
|
||||
* Allows to insert captchas driven by ReCaptcha service
|
||||
*
|
||||
* @see http://recaptcha.net/apidocs/captcha/
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
|
||||
{
|
||||
/**@+
|
||||
* ReCaptcha Field names
|
||||
* @var string
|
||||
*/
|
||||
protected $_CHALLENGE = 'recaptcha_challenge_field';
|
||||
protected $_RESPONSE = 'recaptcha_response_field';
|
||||
/**@-*/
|
||||
|
||||
/**
|
||||
* Recaptcha service object
|
||||
*
|
||||
* @var Zend_Service_Recaptcha
|
||||
*/
|
||||
protected $_service;
|
||||
|
||||
/**
|
||||
* Parameters defined by the service
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_serviceParams = array();
|
||||
|
||||
/**
|
||||
* Options defined by the service
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_serviceOptions = array();
|
||||
|
||||
/**#@+
|
||||
* Error codes
|
||||
* @const string
|
||||
*/
|
||||
const MISSING_VALUE = 'missingValue';
|
||||
const ERR_CAPTCHA = 'errCaptcha';
|
||||
const BAD_CAPTCHA = 'badCaptcha';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* Error messages
|
||||
* @var array
|
||||
*/
|
||||
protected $_messageTemplates = array(
|
||||
self::MISSING_VALUE => 'Missing captcha fields',
|
||||
self::ERR_CAPTCHA => 'Failed to validate captcha',
|
||||
self::BAD_CAPTCHA => 'Captcha value is wrong: %value%',
|
||||
);
|
||||
|
||||
/**
|
||||
* Retrieve ReCaptcha Private key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPrivkey()
|
||||
{
|
||||
return $this->getService()->getPrivateKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve ReCaptcha Public key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPubkey()
|
||||
{
|
||||
return $this->getService()->getPublicKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ReCaptcha Private key
|
||||
*
|
||||
* @param string $privkey
|
||||
* @return Zend_Captcha_ReCaptcha
|
||||
*/
|
||||
public function setPrivkey($privkey)
|
||||
{
|
||||
$this->getService()->setPrivateKey($privkey);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ReCaptcha public key
|
||||
*
|
||||
* @param string $pubkey
|
||||
* @return Zend_Captcha_ReCaptcha
|
||||
*/
|
||||
public function setPubkey($pubkey)
|
||||
{
|
||||
$this->getService()->setPublicKey($pubkey);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array|Zend_Config $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = null)
|
||||
{
|
||||
$this->setService(new Zend_Service_ReCaptcha());
|
||||
$this->_serviceParams = $this->getService()->getParams();
|
||||
$this->_serviceOptions = $this->getService()->getOptions();
|
||||
|
||||
parent::__construct($options);
|
||||
|
||||
if ($options instanceof Zend_Config) {
|
||||
$options = $options->toArray();
|
||||
}
|
||||
if (!empty($options)) {
|
||||
$this->setOptions($options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set service object
|
||||
*
|
||||
* @param Zend_Service_ReCaptcha $service
|
||||
* @return Zend_Captcha_ReCaptcha
|
||||
*/
|
||||
public function setService(Zend_Service_ReCaptcha $service)
|
||||
{
|
||||
$this->_service = $service;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve ReCaptcha service object
|
||||
*
|
||||
* @return Zend_Service_ReCaptcha
|
||||
*/
|
||||
public function getService()
|
||||
{
|
||||
return $this->_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set option
|
||||
*
|
||||
* If option is a service parameter, proxies to the service. The same
|
||||
* goes for any service options (distinct from service params)
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return Zend_Captcha_ReCaptcha
|
||||
*/
|
||||
public function setOption($key, $value)
|
||||
{
|
||||
$service = $this->getService();
|
||||
if (isset($this->_serviceParams[$key])) {
|
||||
$service->setParam($key, $value);
|
||||
return $this;
|
||||
}
|
||||
if (isset($this->_serviceOptions[$key])) {
|
||||
$service->setOption($key, $value);
|
||||
return $this;
|
||||
}
|
||||
return parent::setOption($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate captcha
|
||||
*
|
||||
* @see Zend_Form_Captcha_Adapter::generate()
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate captcha
|
||||
*
|
||||
* @see Zend_Validate_Interface::isValid()
|
||||
* @param mixed $value
|
||||
* @return boolean
|
||||
*/
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
if (!is_array($value) && !is_array($context)) {
|
||||
$this->_error(self::MISSING_VALUE);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_array($value) && is_array($context)) {
|
||||
$value = $context;
|
||||
}
|
||||
|
||||
if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) {
|
||||
$this->_error(self::MISSING_VALUE);
|
||||
return false;
|
||||
}
|
||||
|
||||
$service = $this->getService();
|
||||
|
||||
$res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]);
|
||||
|
||||
if (!$res) {
|
||||
$this->_error(self::ERR_CAPTCHA);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$res->isValid()) {
|
||||
$this->_error(self::BAD_CAPTCHA, $res->getErrorCode());
|
||||
$service->setParam('error', $res->getErrorCode());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render captcha
|
||||
*
|
||||
* @param Zend_View_Interface $view
|
||||
* @param mixed $element
|
||||
* @return string
|
||||
*/
|
||||
public function render(Zend_View_Interface $view = null, $element = null)
|
||||
{
|
||||
return $this->getService()->getHTML();
|
||||
}
|
||||
}
|
||||
@@ -1,373 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Captcha_Base */
|
||||
require_once 'Zend/Captcha/Base.php';
|
||||
|
||||
/**
|
||||
* Word-based captcha adapter
|
||||
*
|
||||
* Generates random word which user should recognise
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Captcha
|
||||
* @subpackage Adapter
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
abstract class Zend_Captcha_Word extends Zend_Captcha_Base
|
||||
{
|
||||
/**#@+
|
||||
* @var array Character sets
|
||||
*/
|
||||
static $V = array("a", "e", "i", "o", "u", "y");
|
||||
static $VN = array("a", "e", "i", "o", "u", "y","2","3","4","5","6","7","8","9");
|
||||
static $C = array("b","c","d","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","z");
|
||||
static $CN = array("b","c","d","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","z","2","3","4","5","6","7","8","9");
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* Random session ID
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_id;
|
||||
|
||||
/**
|
||||
* Generated word
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_word;
|
||||
|
||||
/**
|
||||
* Session
|
||||
*
|
||||
* @var Zend_Session_Namespace
|
||||
*/
|
||||
protected $_session;
|
||||
|
||||
/**
|
||||
* Class name for sessions
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_sessionClass = 'Zend_Session_Namespace';
|
||||
|
||||
/**
|
||||
* Should the numbers be used or only letters
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_useNumbers = true;
|
||||
|
||||
/**
|
||||
* Should both cases be used or only lowercase
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
// protected $_useCase = false;
|
||||
|
||||
/**
|
||||
* Session lifetime for the captcha data
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $_timeout = 300;
|
||||
|
||||
/**#@+
|
||||
* Error codes
|
||||
* @const string
|
||||
*/
|
||||
const MISSING_VALUE = 'missingValue';
|
||||
const MISSING_ID = 'missingID';
|
||||
const BAD_CAPTCHA = 'badCaptcha';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* Error messages
|
||||
* @var array
|
||||
*/
|
||||
protected $_messageTemplates = array(
|
||||
self::MISSING_VALUE => 'Empty captcha value',
|
||||
self::MISSING_ID => 'Captcha ID field is missing',
|
||||
self::BAD_CAPTCHA => 'Captcha value is wrong',
|
||||
);
|
||||
|
||||
/**
|
||||
* Length of the word to generate
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $_wordlen = 8;
|
||||
|
||||
/**
|
||||
* Retrieve session class to utilize
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSessionClass()
|
||||
{
|
||||
return $this->_sessionClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set session class for persistence
|
||||
*
|
||||
* @param string $_sessionClass
|
||||
* @return Zend_Captcha_Word
|
||||
*/
|
||||
public function setSessionClass($_sessionClass)
|
||||
{
|
||||
$this->_sessionClass = $_sessionClass;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve word length to use when genrating captcha
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getWordlen()
|
||||
{
|
||||
return $this->_wordlen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set word length of captcha
|
||||
*
|
||||
* @param integer $wordlen
|
||||
* @return Zend_Captcha_Word
|
||||
*/
|
||||
public function setWordlen($wordlen)
|
||||
{
|
||||
$this->_wordlen = $wordlen;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve captcha ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId ()
|
||||
{
|
||||
if (null === $this->_id) {
|
||||
$this->_setId($this->_generateRandomId());
|
||||
}
|
||||
return $this->_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha identifier
|
||||
*
|
||||
* @param string $id
|
||||
* return Zend_Captcha_Word
|
||||
*/
|
||||
protected function _setId ($id)
|
||||
{
|
||||
$this->_id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set timeout for session token
|
||||
*
|
||||
* @param int $ttl
|
||||
* @return Zend_Captcha_Word
|
||||
*/
|
||||
public function setTimeout($ttl)
|
||||
{
|
||||
$this->_timeout = (int) $ttl;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get session token timeout
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeout()
|
||||
{
|
||||
return $this->_timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get session object
|
||||
*
|
||||
* @return Zend_Session_Namespace
|
||||
*/
|
||||
public function getSession()
|
||||
{
|
||||
if (!isset($this->_session) || (null === $this->_session)) {
|
||||
$id = $this->getId();
|
||||
if (!class_exists($this->_sessionClass)) {
|
||||
require_once 'Zend/Loader.php';
|
||||
Zend_Loader::loadClass($this->_sessionClass);
|
||||
}
|
||||
$this->_session = new $this->_sessionClass('Zend_Form_Captcha_' . $id);
|
||||
$this->_session->setExpirationHops(1, null, true);
|
||||
$this->_session->setExpirationSeconds($this->getTimeout());
|
||||
}
|
||||
return $this->_session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set session namespace object
|
||||
*
|
||||
* @param Zend_Session_Namespace $session
|
||||
* @return Zend_Captcha_Word
|
||||
*/
|
||||
public function setSession(Zend_Session_Namespace $session)
|
||||
{
|
||||
$this->_session = $session;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get captcha word
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getWord()
|
||||
{
|
||||
if (empty($this->_word)) {
|
||||
$session = $this->getSession();
|
||||
$this->_word = $session->word;
|
||||
}
|
||||
return $this->_word;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set captcha word
|
||||
*
|
||||
* @param string $word
|
||||
* @return Zend_Captcha_Word
|
||||
*/
|
||||
protected function _setWord($word)
|
||||
{
|
||||
$session = $this->getSession();
|
||||
$session->word = $word;
|
||||
$this->_word = $word;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate new random word
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _generateWord()
|
||||
{
|
||||
$word = '';
|
||||
$wordLen = $this->getWordLen();
|
||||
$vowels = $this->_useNumbers ? self::$VN : self::$V;
|
||||
$consonants = $this->_useNumbers ? self::$CN : self::$C;
|
||||
|
||||
for ($i=0; $i < $wordLen; $i = $i + 2) {
|
||||
// generate word with mix of vowels and consonants
|
||||
$consonant = $consonants[array_rand($consonants)];
|
||||
$vowel = $vowels[array_rand($vowels)];
|
||||
$word .= $consonant . $vowel;
|
||||
}
|
||||
|
||||
if (strlen($word) > $wordLen) {
|
||||
$word = substr($word, 0, $wordLen);
|
||||
}
|
||||
|
||||
return $word;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate new session ID and new word
|
||||
*
|
||||
* @return string session ID
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$this->_session = null;
|
||||
$id = $this->_generateRandomId();
|
||||
$this->_setId($id);
|
||||
$word = $this->_generateWord();
|
||||
$this->_setWord($word);
|
||||
return $id;
|
||||
}
|
||||
|
||||
protected function _generateRandomId()
|
||||
{
|
||||
return md5(mt_rand(0, 1000) . microtime(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the word
|
||||
*
|
||||
* @see Zend_Validate_Interface::isValid()
|
||||
* @param mixed $value
|
||||
* @return boolean
|
||||
*/
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
if (!is_array($value) && !is_array($context)) {
|
||||
$this->_error(self::MISSING_VALUE);
|
||||
return false;
|
||||
}
|
||||
if (!is_array($value) && is_array($context)) {
|
||||
$value = $context;
|
||||
}
|
||||
|
||||
$name = $this->getName();
|
||||
|
||||
if (isset($value[$name])) {
|
||||
$value = $value[$name];
|
||||
}
|
||||
|
||||
if (!isset($value['input'])) {
|
||||
$this->_error(self::MISSING_VALUE);
|
||||
return false;
|
||||
}
|
||||
$input = strtolower($value['input']);
|
||||
$this->_setValue($input);
|
||||
|
||||
if (!isset($value['id'])) {
|
||||
$this->_error(self::MISSING_ID);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->_id = $value['id'];
|
||||
if ($input !== $this->getWord()) {
|
||||
$this->_error(self::BAD_CAPTCHA);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get captcha decorator
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDecorator()
|
||||
{
|
||||
return "Captcha_Word";
|
||||
}
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_CodeGenerator_Abstract
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_sourceContent = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isSourceDirty = true;
|
||||
|
||||
/**
|
||||
* __construct()
|
||||
*
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct($options = array())
|
||||
{
|
||||
$this->_init();
|
||||
if ($options != null) {
|
||||
// use Zend_Config objects if provided
|
||||
if ($options instanceof Zend_Config) {
|
||||
$options = $options->toArray();
|
||||
}
|
||||
// pass arrays to setOptions
|
||||
if (is_array($options)) {
|
||||
$this->setOptions($options);
|
||||
}
|
||||
}
|
||||
$this->_prepare();
|
||||
}
|
||||
|
||||
/**
|
||||
* setConfig()
|
||||
*
|
||||
* @param Zend_Config $config
|
||||
* @return Zend_CodeGenerator_Abstract
|
||||
*/
|
||||
public function setConfig(Zend_Config $config)
|
||||
{
|
||||
$this->setOptions($config->toArray());
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setOptions()
|
||||
*
|
||||
* @param array $options
|
||||
* @return Zend_CodeGenerator_Abstract
|
||||
*/
|
||||
public function setOptions(Array $options)
|
||||
{
|
||||
foreach ($options as $optionName => $optionValue) {
|
||||
$methodName = 'set' . $optionName;
|
||||
if (method_exists($this, $methodName)) {
|
||||
$this->{$methodName}($optionValue);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setSourceContent()
|
||||
*
|
||||
* @param string $sourceContent
|
||||
*/
|
||||
public function setSourceContent($sourceContent)
|
||||
{
|
||||
$this->_sourceContent = $sourceContent;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* getSourceContent()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSourceContent()
|
||||
{
|
||||
return $this->_sourceContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* _init() - this is called before the constuctor
|
||||
*
|
||||
*/
|
||||
protected function _init()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* _prepare() - this is called at construction completion
|
||||
*
|
||||
*/
|
||||
protected function _prepare()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* generate() - must be implemented by the child
|
||||
*
|
||||
*/
|
||||
abstract public function generate();
|
||||
|
||||
/**
|
||||
* __toString() - casting to a string will in turn call generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
final public function __toString()
|
||||
{
|
||||
return $this->generate();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Exception
|
||||
*/
|
||||
require_once 'Zend/Exception.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Exception extends Zend_Exception
|
||||
{}
|
||||
@@ -1,97 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Abstract.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_CodeGenerator_Php_Abstract extends Zend_CodeGenerator_Abstract
|
||||
{
|
||||
|
||||
/**
|
||||
* Line feed to use in place of EOL
|
||||
*
|
||||
*/
|
||||
const LINE_FEED = "\n";
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isSourceDirty = true;
|
||||
|
||||
/**
|
||||
* @var int|string
|
||||
*/
|
||||
protected $_indentation = ' ';
|
||||
|
||||
/**
|
||||
* setSourceDirty()
|
||||
*
|
||||
* @param bool $isSourceDirty
|
||||
* @return Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
public function setSourceDirty($isSourceDirty = true)
|
||||
{
|
||||
$this->_isSourceDirty = ($isSourceDirty) ? true : false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* isSourceDirty()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSourceDirty()
|
||||
{
|
||||
return $this->_isSourceDirty;
|
||||
}
|
||||
|
||||
/**
|
||||
* setIndentation()
|
||||
*
|
||||
* @param string|int $indentation
|
||||
* @return Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
public function setIndentation($indentation)
|
||||
{
|
||||
$this->_indentation = $indentation;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getIndentation()
|
||||
*
|
||||
* @return string|int
|
||||
*/
|
||||
public function getIndentation()
|
||||
{
|
||||
return $this->_indentation;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Abstract.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Body extends Zend_CodeGenerator_Abstract
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_content = null;
|
||||
|
||||
/**
|
||||
* setContent()
|
||||
*
|
||||
* @param string $content
|
||||
* @return Zend_CodeGenerator_Php_Body
|
||||
*/
|
||||
public function setContent($content)
|
||||
{
|
||||
$this->_content = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getContent()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return (string) $this->_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
return $this->getContent();
|
||||
}
|
||||
}
|
||||
@@ -1,513 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Member_Container
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Member/Container.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Method
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Method.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Property
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Property.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
protected $_docblock = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_name = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isAbstract = false;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_extendedClass = null;
|
||||
|
||||
/**
|
||||
* @var array Array of string names
|
||||
*/
|
||||
protected $_implementedInterfaces = array();
|
||||
|
||||
/**
|
||||
* @var array Array of properties
|
||||
*/
|
||||
protected $_properties = null;
|
||||
|
||||
/**
|
||||
* @var array Array of methods
|
||||
*/
|
||||
protected $_methods = null;
|
||||
|
||||
/**
|
||||
* fromReflection() - build a Code Generation PHP Object from a Class Reflection
|
||||
*
|
||||
* @param Zend_Reflection_Class $reflectionClass
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Class $reflectionClass)
|
||||
{
|
||||
$class = new self();
|
||||
|
||||
$class->setSourceContent($class->getSourceContent());
|
||||
$class->setSourceDirty(false);
|
||||
|
||||
if ($reflectionClass->getDocComment() != '') {
|
||||
$class->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionClass->getDocblock()));
|
||||
}
|
||||
|
||||
$class->setAbstract($reflectionClass->isAbstract());
|
||||
$class->setName($reflectionClass->getName());
|
||||
|
||||
if ($parentClass = $reflectionClass->getParentClass()) {
|
||||
$class->setExtendedClass($parentClass->getName());
|
||||
$interfaces = array_diff($reflectionClass->getInterfaces(), $parentClass->getInterfaces());
|
||||
} else {
|
||||
$interfaces = $reflectionClass->getInterfaces();
|
||||
}
|
||||
|
||||
$interfaceNames = array();
|
||||
foreach($interfaces AS $interface) {
|
||||
$interfaceNames[] = $interface->getName();
|
||||
}
|
||||
|
||||
$class->setImplementedInterfaces($interfaceNames);
|
||||
|
||||
$properties = array();
|
||||
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
|
||||
if ($reflectionProperty->getDeclaringClass()->getName() == $class->getName()) {
|
||||
$properties[] = Zend_CodeGenerator_Php_Property::fromReflection($reflectionProperty);
|
||||
}
|
||||
}
|
||||
$class->setProperties($properties);
|
||||
|
||||
$methods = array();
|
||||
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
|
||||
if ($reflectionMethod->getDeclaringClass()->getName() == $class->getName()) {
|
||||
$methods[] = Zend_CodeGenerator_Php_Method::fromReflection($reflectionMethod);
|
||||
}
|
||||
}
|
||||
$class->setMethods($methods);
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* setDocblock() Set the docblock
|
||||
*
|
||||
* @param Zend_CodeGenerator_Php_Docblock|array|string $docblock
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public function setDocblock($docblock)
|
||||
{
|
||||
if (is_string($docblock)) {
|
||||
$docblock = array('shortDescription' => $docblock);
|
||||
}
|
||||
|
||||
if (is_array($docblock)) {
|
||||
$docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
|
||||
} elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
|
||||
}
|
||||
|
||||
$this->_docblock = $docblock;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDocblock()
|
||||
*
|
||||
* @return Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
public function getDocblock()
|
||||
{
|
||||
return $this->_docblock;
|
||||
}
|
||||
|
||||
/**
|
||||
* setName()
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->_name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* setAbstract()
|
||||
*
|
||||
* @param bool $isAbstract
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function setAbstract($isAbstract)
|
||||
{
|
||||
$this->_isAbstract = ($isAbstract) ? true : false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* isAbstract()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAbstract()
|
||||
{
|
||||
return $this->_isAbstract;
|
||||
}
|
||||
|
||||
/**
|
||||
* setExtendedClass()
|
||||
*
|
||||
* @param string $extendedClass
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function setExtendedClass($extendedClass)
|
||||
{
|
||||
$this->_extendedClass = $extendedClass;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getExtendedClass()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getExtendedClass()
|
||||
{
|
||||
return $this->_extendedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* setImplementedInterfaces()
|
||||
*
|
||||
* @param array $implementedInterfaces
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function setImplementedInterfaces(Array $implementedInterfaces)
|
||||
{
|
||||
$this->_implementedInterfaces = $implementedInterfaces;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getImplementedInterfaces
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getImplementedInterfaces()
|
||||
{
|
||||
return $this->_implementedInterfaces;
|
||||
}
|
||||
|
||||
/**
|
||||
* setProperties()
|
||||
*
|
||||
* @param array $properties
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function setProperties(Array $properties)
|
||||
{
|
||||
foreach ($properties as $property) {
|
||||
$this->setProperty($property);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setProperty()
|
||||
*
|
||||
* @param array|Zend_CodeGenerator_Php_Property $property
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function setProperty($property)
|
||||
{
|
||||
if (is_array($property)) {
|
||||
$property = new Zend_CodeGenerator_Php_Property($property);
|
||||
$propertyName = $property->getName();
|
||||
} elseif ($property instanceof Zend_CodeGenerator_Php_Property) {
|
||||
$propertyName = $property->getName();
|
||||
} else {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
|
||||
}
|
||||
|
||||
if (isset($this->_properties[$propertyName])) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.');
|
||||
}
|
||||
|
||||
$this->_properties[$propertyName] = $property;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getProperties()
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties()
|
||||
{
|
||||
return $this->_properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* getProperty()
|
||||
*
|
||||
* @param string $propertyName
|
||||
* @return Zend_CodeGenerator_Php_Property
|
||||
*/
|
||||
public function getProperty($propertyName)
|
||||
{
|
||||
foreach ($this->_properties as $property) {
|
||||
if ($property->getName() == $propertyName) {
|
||||
return $property;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* hasProperty()
|
||||
*
|
||||
* @param string $propertyName
|
||||
* @return bool
|
||||
*/
|
||||
public function hasProperty($propertyName)
|
||||
{
|
||||
return isset($this->_properties[$propertyName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* setMethods()
|
||||
*
|
||||
* @param array $methods
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function setMethods(Array $methods)
|
||||
{
|
||||
foreach ($methods as $method) {
|
||||
$this->setMethod($method);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setMethod()
|
||||
*
|
||||
* @param array|Zend_CodeGenerator_Php_Method $method
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function setMethod($method)
|
||||
{
|
||||
if (is_array($method)) {
|
||||
$method = new Zend_CodeGenerator_Php_Method($method);
|
||||
$methodName = $method->getName();
|
||||
} elseif ($method instanceof Zend_CodeGenerator_Php_Method) {
|
||||
$methodName = $method->getName();
|
||||
} else {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method');
|
||||
}
|
||||
|
||||
if (isset($this->_methods[$methodName])) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('A method by name ' . $methodName . ' already exists in this class.');
|
||||
}
|
||||
|
||||
$this->_methods[$methodName] = $method;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getMethods()
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getMethods()
|
||||
{
|
||||
return $this->_methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* getMethod()
|
||||
*
|
||||
* @param string $methodName
|
||||
* @return Zend_CodeGenerator_Php_Method
|
||||
*/
|
||||
public function getMethod($methodName)
|
||||
{
|
||||
foreach ($this->_methods as $method) {
|
||||
if ($method->getName() == $methodName) {
|
||||
return $method;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* hasMethod()
|
||||
*
|
||||
* @param string $methodName
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMethod($methodName)
|
||||
{
|
||||
return isset($this->_methods[$methodName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* isSourceDirty()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSourceDirty()
|
||||
{
|
||||
if (($docblock = $this->getDocblock()) && $docblock->isSourceDirty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($this->_properties as $property) {
|
||||
if ($property->isSourceDirty()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->_methods as $method) {
|
||||
if ($method->isSourceDirty()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::isSourceDirty();
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
if (!$this->isSourceDirty()) {
|
||||
return $this->getSourceContent();
|
||||
}
|
||||
|
||||
$output = '';
|
||||
|
||||
if (null !== ($docblock = $this->getDocblock())) {
|
||||
$docblock->setIndentation('');
|
||||
$output .= $docblock->generate();
|
||||
}
|
||||
|
||||
if ($this->isAbstract()) {
|
||||
$output .= 'abstract ';
|
||||
}
|
||||
|
||||
$output .= 'class ' . $this->getName();
|
||||
|
||||
if (null !== ($ec = $this->_extendedClass)) {
|
||||
$output .= ' extends ' . $ec;
|
||||
}
|
||||
|
||||
$implemented = $this->getImplementedInterfaces();
|
||||
if (!empty($implemented)) {
|
||||
$output .= ' implements ' . implode(', ', $implemented);
|
||||
}
|
||||
|
||||
$output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
|
||||
|
||||
$properties = $this->getProperties();
|
||||
if (!empty($properties)) {
|
||||
foreach ($properties as $property) {
|
||||
$output .= $property->generate() . self::LINE_FEED . self::LINE_FEED;
|
||||
}
|
||||
}
|
||||
|
||||
$methods = $this->getMethods();
|
||||
if (!empty($methods)) {
|
||||
foreach ($methods as $method) {
|
||||
$output .= $method->generate() . self::LINE_FEED;
|
||||
}
|
||||
}
|
||||
|
||||
$output .= self::LINE_FEED . '}' . self::LINE_FEED;
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* _init() - is called at construction time
|
||||
*
|
||||
*/
|
||||
protected function _init()
|
||||
{
|
||||
$this->_properties = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
|
||||
$this->_methods = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_METHOD);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Docblock_Tag
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_shortDescription = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_longDescription = null;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_tags = array();
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_indentation = '';
|
||||
|
||||
/**
|
||||
* fromReflection() - Build a docblock generator object from a reflection object
|
||||
*
|
||||
* @param Zend_Reflection_Docblock $reflectionDocblock
|
||||
* @return Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Docblock $reflectionDocblock)
|
||||
{
|
||||
$docblock = new self();
|
||||
|
||||
$docblock->setSourceContent($reflectionDocblock->getContents());
|
||||
$docblock->setSourceDirty(false);
|
||||
|
||||
$docblock->setShortDescription($reflectionDocblock->getShortDescription());
|
||||
$docblock->setLongDescription($reflectionDocblock->getLongDescription());
|
||||
|
||||
foreach ($reflectionDocblock->getTags() as $tag) {
|
||||
$docblock->setTag(Zend_CodeGenerator_Php_Docblock_Tag::fromReflection($tag));
|
||||
}
|
||||
|
||||
return $docblock;
|
||||
}
|
||||
|
||||
/**
|
||||
* setShortDescription()
|
||||
*
|
||||
* @param string $shortDescription
|
||||
* @return Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
public function setShortDescription($shortDescription)
|
||||
{
|
||||
$this->_shortDescription = $shortDescription;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getShortDescription()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getShortDescription()
|
||||
{
|
||||
return $this->_shortDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* setLongDescription()
|
||||
*
|
||||
* @param string $longDescription
|
||||
* @return Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
public function setLongDescription($longDescription)
|
||||
{
|
||||
$this->_longDescription = $longDescription;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getLongDescription()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLongDescription()
|
||||
{
|
||||
return $this->_longDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* setTags()
|
||||
*
|
||||
* @param array $tags
|
||||
* @return Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
public function setTags(Array $tags)
|
||||
{
|
||||
foreach ($tags as $tag) {
|
||||
$this->setTag($tag);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setTag()
|
||||
*
|
||||
* @param array|Zend_CodeGenerator_Php_Docblock_Tag $tag
|
||||
* @return Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
public function setTag($tag)
|
||||
{
|
||||
if (is_array($tag)) {
|
||||
$tag = new Zend_CodeGenerator_Php_Docblock_Tag($tag);
|
||||
} elseif (!$tag instanceof Zend_CodeGenerator_Php_Docblock_Tag) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception(
|
||||
'setTag() expects either an array of method options or an '
|
||||
. 'instance of Zend_CodeGenerator_Php_Docblock_Tag'
|
||||
);
|
||||
}
|
||||
|
||||
$this->_tags[] = $tag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getTags
|
||||
*
|
||||
* @return array Array of Zend_CodeGenerator_Php_Docblock_Tag
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
return $this->_tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
if (!$this->isSourceDirty()) {
|
||||
return $this->_docCommentize($this->getSourceContent());
|
||||
}
|
||||
|
||||
$output = '';
|
||||
if (null !== ($sd = $this->getShortDescription())) {
|
||||
$output .= $sd . self::LINE_FEED . self::LINE_FEED;
|
||||
}
|
||||
if (null !== ($ld = $this->getLongDescription())) {
|
||||
$output .= $ld . self::LINE_FEED . self::LINE_FEED;
|
||||
}
|
||||
|
||||
foreach ($this->getTags() as $tag) {
|
||||
$output .= $tag->generate() . self::LINE_FEED;
|
||||
}
|
||||
|
||||
return $this->_docCommentize(trim($output));
|
||||
}
|
||||
|
||||
/**
|
||||
* _docCommentize()
|
||||
*
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
protected function _docCommentize($content)
|
||||
{
|
||||
$indent = $this->getIndentation();
|
||||
$output = $indent . '/**' . self::LINE_FEED;
|
||||
$content = wordwrap($content, 80, self::LINE_FEED);
|
||||
$lines = explode(self::LINE_FEED, $content);
|
||||
foreach ($lines as $line) {
|
||||
$output .= $indent . ' * ' . $line . self::LINE_FEED;
|
||||
}
|
||||
$output .= $indent . ' */' . self::LINE_FEED;
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,184 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Docblock_Tag_Param
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock/Tag/Param.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Docblock_Tag_Return
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock/Tag/Return.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Docblock_Tag extends Zend_CodeGenerator_Php_Abstract
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Zend_Loader_PluginLoader
|
||||
*/
|
||||
protected static $_pluginLoader = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_name = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_description = null;
|
||||
|
||||
/**
|
||||
* fromReflection()
|
||||
*
|
||||
* @param Zend_Reflection_Docblock_Tag $reflectionTag
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Docblock_Tag $reflectionTag)
|
||||
{
|
||||
$tagName = $reflectionTag->getName();
|
||||
|
||||
$codeGenDocblockTag = self::factory($tagName);
|
||||
|
||||
// transport any properties via accessors and mutators from reflection to codegen object
|
||||
$reflectionClass = new ReflectionClass($reflectionTag);
|
||||
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
|
||||
if (substr($method->getName(), 0, 3) == 'get') {
|
||||
$propertyName = substr($method->getName(), 3);
|
||||
if (method_exists($codeGenDocblockTag, 'set' . $propertyName)) {
|
||||
$codeGenDocblockTag->{'set' . $propertyName}($reflectionTag->{'get' . $propertyName}());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $codeGenDocblockTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* setPluginLoader()
|
||||
*
|
||||
* @param Zend_Loader_PluginLoader $pluginLoader
|
||||
*/
|
||||
public static function setPluginLoader(Zend_Loader_PluginLoader $pluginLoader)
|
||||
{
|
||||
self::$_pluginLoader = $pluginLoader;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* getPluginLoader()
|
||||
*
|
||||
* @return Zend_Loader_PluginLoader
|
||||
*/
|
||||
public static function getPluginLoader()
|
||||
{
|
||||
if (self::$_pluginLoader == null) {
|
||||
require_once 'Zend/Loader/PluginLoader.php';
|
||||
self::setPluginLoader(new Zend_Loader_PluginLoader(array(
|
||||
'Zend_CodeGenerator_Php_Docblock_Tag' => dirname(__FILE__) . '/Tag/'))
|
||||
);
|
||||
}
|
||||
|
||||
return self::$_pluginLoader;
|
||||
}
|
||||
|
||||
public static function factory($tagName)
|
||||
{
|
||||
$pluginLoader = self::getPluginLoader();
|
||||
|
||||
try {
|
||||
$tagClass = $pluginLoader->load($tagName);
|
||||
} catch (Zend_Loader_Exception $exception) {
|
||||
$tagClass = 'Zend_CodeGenerator_Php_Docblock_Tag';
|
||||
}
|
||||
|
||||
$tag = new $tagClass(array('name' => $tagName));
|
||||
return $tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* setName()
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->_name = ltrim($name, '@');
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* setDescription()
|
||||
*
|
||||
* @param string $description
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag
|
||||
*/
|
||||
public function setDescription($description)
|
||||
{
|
||||
$this->_description = $description;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDescription()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->_description;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
return '@' . $this->_name . ' ' . $this->_description;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Docblock_Tag
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Docblock_Tag_License extends Zend_CodeGenerator_Php_Docblock_Tag
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_url = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_description = null;
|
||||
|
||||
/**
|
||||
* fromReflection()
|
||||
*
|
||||
* @param Zend_Reflection_Docblock_Tag $reflectionTagReturn
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag_License
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Docblock_Tag $reflectionTagLicense)
|
||||
{
|
||||
$returnTag = new self();
|
||||
|
||||
$returnTag->setName('license');
|
||||
$returnTag->setUrl($reflectionTagLicense->getUrl());
|
||||
$returnTag->setDescription($reflectionTagLicense->getDescription());
|
||||
|
||||
return $returnTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* setUrl()
|
||||
*
|
||||
* @param string $url
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag_License
|
||||
*/
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->_url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getUrl()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->_url;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$output = '@license ' . $this->_url . ' ' . $this->_description . self::LINE_FEED;
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Docblock_Tag
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Docblock_Tag_Param extends Zend_CodeGenerator_Php_Docblock_Tag
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_datatype = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_paramName = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_description = null;
|
||||
|
||||
/**
|
||||
* fromReflection()
|
||||
*
|
||||
* @param Zend_Reflection_Docblock_Tag $reflectionTagParam
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag_Param
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Docblock_Tag $reflectionTagParam)
|
||||
{
|
||||
$paramTag = new self();
|
||||
|
||||
$paramTag->setName('param');
|
||||
$paramTag->setDatatype($reflectionTagParam->getType()); // @todo rename
|
||||
$paramTag->setParamName($reflectionTagParam->getVariableName());
|
||||
$paramTag->setDescription($reflectionTagParam->getDescription());
|
||||
|
||||
return $paramTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* setDatatype()
|
||||
*
|
||||
* @param string $datatype
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag_Param
|
||||
*/
|
||||
public function setDatatype($datatype)
|
||||
{
|
||||
$this->_datatype = $datatype;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDatatype
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDatatype()
|
||||
{
|
||||
return $this->_datatype;
|
||||
}
|
||||
|
||||
/**
|
||||
* setParamName()
|
||||
*
|
||||
* @param string $paramName
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag_Param
|
||||
*/
|
||||
public function setParamName($paramName)
|
||||
{
|
||||
$this->_paramName = $paramName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getParamName()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getParamName()
|
||||
{
|
||||
return $this->_paramName;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$output = '@param '
|
||||
. (($this->_datatype != null) ? $this->_datatype : 'unknown')
|
||||
. (($this->_paramName != null) ? ' $' . $this->_paramName : '')
|
||||
. (($this->_description != null) ? ' ' . $this->_description : '');
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Docblock_Tag
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Docblock_Tag_Return extends Zend_CodeGenerator_Php_Docblock_Tag
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_datatype = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_description = null;
|
||||
|
||||
/**
|
||||
* fromReflection()
|
||||
*
|
||||
* @param Zend_Reflection_Docblock_Tag $reflectionTagReturn
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag_Return
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Docblock_Tag $reflectionTagReturn)
|
||||
{
|
||||
$returnTag = new self();
|
||||
|
||||
$returnTag->setName('return');
|
||||
$returnTag->setDatatype($reflectionTagReturn->getType()); // @todo rename
|
||||
$returnTag->setDescription($reflectionTagReturn->getDescription());
|
||||
|
||||
return $returnTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* setDatatype()
|
||||
*
|
||||
* @param string $datatype
|
||||
* @return Zend_CodeGenerator_Php_Docblock_Tag_Return
|
||||
*/
|
||||
public function setDatatype($datatype)
|
||||
{
|
||||
$this->_datatype = $datatype;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDatatype()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDatatype()
|
||||
{
|
||||
return $this->_datatype;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$output = '@return ' . $this->_datatype . ' ' . $this->_description;
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Exception
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Exception.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Exception extends Zend_CodeGenerator_Exception
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,465 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Class.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_File extends Zend_CodeGenerator_Php_Abstract
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array Array of Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
protected static $_fileCodeGenerators = array();
|
||||
|
||||
/**#@+
|
||||
* @var string
|
||||
*/
|
||||
protected static $_markerDocblock = '/* Zend_CodeGenerator_Php_File-DocblockMarker */';
|
||||
protected static $_markerRequire = '/* Zend_CodeGenerator_Php_File-RequireMarker: {?} */';
|
||||
protected static $_markerClass = '/* Zend_CodeGenerator_Php_File-ClassMarker: {?} */';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_filename = null;
|
||||
|
||||
/**
|
||||
* @var Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
protected $_docblock = null;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_requiredFiles = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_classes = array();
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_body = null;
|
||||
|
||||
public static function registerFileCodeGenerator(Zend_CodeGenerator_Php_File $fileCodeGenerator, $fileName = null)
|
||||
{
|
||||
if ($fileName == null) {
|
||||
$fileName = $fileCodeGenerator->getFilename();
|
||||
}
|
||||
|
||||
if ($fileName == '') {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('FileName does not exist.');
|
||||
}
|
||||
|
||||
// cannot use realpath since the file might not exist, but we do need to have the index
|
||||
// in the same DIRECTORY_SEPARATOR that realpath would use:
|
||||
$fileName = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $fileName);
|
||||
|
||||
self::$_fileCodeGenerators[$fileName] = $fileCodeGenerator;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* fromReflectedFilePath() - use this if you intend on generating code generation objects based on the same file.
|
||||
* This will keep previous changes to the file in tact during the same PHP process
|
||||
*
|
||||
* @param string $filePath
|
||||
* @param bool $usePreviousCodeGeneratorIfItExists
|
||||
* @param bool $includeIfNotAlreadyIncluded
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public static function fromReflectedFileName($filePath, $usePreviousCodeGeneratorIfItExists = true, $includeIfNotAlreadyIncluded = true)
|
||||
{
|
||||
$realpath = realpath($filePath);
|
||||
|
||||
if ($realpath === false) {
|
||||
if ( ($realpath = Zend_Reflection_file::findRealpathInIncludePath($filePath)) === false) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('No file for ' . $realpath . ' was found.');
|
||||
}
|
||||
}
|
||||
|
||||
if ($usePreviousCodeGeneratorIfItExists && isset(self::$_fileCodeGenerators[$realpath])) {
|
||||
return self::$_fileCodeGenerators[$realpath];
|
||||
}
|
||||
|
||||
if ($includeIfNotAlreadyIncluded && !in_array($realpath, get_included_files())) {
|
||||
include $realpath;
|
||||
}
|
||||
|
||||
$codeGenerator = self::fromReflection(($fileReflector = new Zend_Reflection_File($realpath)));
|
||||
|
||||
if (!isset(self::$_fileCodeGenerators[$fileReflector->getFileName()])) {
|
||||
self::$_fileCodeGenerators[$fileReflector->getFileName()] = $codeGenerator;
|
||||
}
|
||||
|
||||
return $codeGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* fromReflection()
|
||||
*
|
||||
* @param Zend_Reflection_File $reflectionFile
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_File $reflectionFile)
|
||||
{
|
||||
$file = new self();
|
||||
|
||||
$file->setSourceContent($reflectionFile->getContents());
|
||||
$file->setSourceDirty(false);
|
||||
|
||||
$body = $reflectionFile->getContents();
|
||||
|
||||
// @todo this whole area needs to be reworked with respect to how body lines are processed
|
||||
foreach ($reflectionFile->getClasses() as $class) {
|
||||
$file->setClass(Zend_CodeGenerator_Php_Class::fromReflection($class));
|
||||
$classStartLine = $class->getStartLine(true);
|
||||
$classEndLine = $class->getEndLine();
|
||||
|
||||
$bodyLines = explode("\n", $body);
|
||||
$bodyReturn = array();
|
||||
for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) {
|
||||
if ($lineNum == $classStartLine) {
|
||||
$bodyReturn[] = str_replace('?', $class->getName(), self::$_markerClass); //'/* Zend_CodeGenerator_Php_File-ClassMarker: {' . $class->getName() . '} */';
|
||||
$lineNum = $classEndLine;
|
||||
} else {
|
||||
$bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion
|
||||
}
|
||||
}
|
||||
$body = implode("\n", $bodyReturn);
|
||||
unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine);
|
||||
}
|
||||
|
||||
if (($reflectionFile->getDocComment() != '')) {
|
||||
$docblock = $reflectionFile->getDocblock();
|
||||
$file->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($docblock));
|
||||
|
||||
$bodyLines = explode("\n", $body);
|
||||
$bodyReturn = array();
|
||||
for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) {
|
||||
if ($lineNum == $docblock->getStartLine()) {
|
||||
$bodyReturn[] = str_replace('?', $class->getName(), self::$_markerDocblock); //'/* Zend_CodeGenerator_Php_File-ClassMarker: {' . $class->getName() . '} */';
|
||||
$lineNum = $docblock->getEndLine();
|
||||
} else {
|
||||
$bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion
|
||||
}
|
||||
}
|
||||
$body = implode("\n", $bodyReturn);
|
||||
unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine);
|
||||
}
|
||||
|
||||
$file->setBody($body);
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* setDocblock() Set the docblock
|
||||
*
|
||||
* @param Zend_CodeGenerator_Php_Docblock|array|string $docblock
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public function setDocblock($docblock)
|
||||
{
|
||||
if (is_string($docblock)) {
|
||||
$docblock = array('shortDescription' => $docblock);
|
||||
}
|
||||
|
||||
if (is_array($docblock)) {
|
||||
$docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
|
||||
} elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
|
||||
}
|
||||
|
||||
$this->_docblock = $docblock;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get docblock
|
||||
*
|
||||
* @return Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
public function getDocblock()
|
||||
{
|
||||
return $this->_docblock;
|
||||
}
|
||||
|
||||
/**
|
||||
* setRequiredFiles
|
||||
*
|
||||
* @param array $requiredFiles
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public function setRequiredFiles($requiredFiles)
|
||||
{
|
||||
$this->_requiredFiles = $requiredFiles;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getRequiredFiles()
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRequiredFiles()
|
||||
{
|
||||
return $this->_requiredFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* setClasses()
|
||||
*
|
||||
* @param array $classes
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public function setClasses(Array $classes)
|
||||
{
|
||||
foreach ($classes as $class) {
|
||||
$this->setClass($class);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getClass()
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function getClass($name = null)
|
||||
{
|
||||
if ($name == null) {
|
||||
reset($this->_classes);
|
||||
return current($this->_classes);
|
||||
}
|
||||
|
||||
return $this->_classes[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* setClass()
|
||||
*
|
||||
* @param Zend_CodeGenerator_Php_Class|array $class
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public function setClass($class)
|
||||
{
|
||||
if (is_array($class)) {
|
||||
$class = new Zend_CodeGenerator_Php_Class($class);
|
||||
$className = $class->getName();
|
||||
} elseif ($class instanceof Zend_CodeGenerator_Php_Class) {
|
||||
$className = $class->getName();
|
||||
} else {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('Expecting either an array or an instance of Zend_CodeGenerator_Php_Class');
|
||||
}
|
||||
|
||||
// @todo check for dup here
|
||||
|
||||
$this->_classes[$className] = $class;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setFilename()
|
||||
*
|
||||
* @param string $filename
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public function setFilename($filename)
|
||||
{
|
||||
$this->_filename = $filename;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFilename()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFilename()
|
||||
{
|
||||
return $this->_filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* getClasses()
|
||||
*
|
||||
* @return array Array of Zend_CodeGenerator_Php_Class
|
||||
*/
|
||||
public function getClasses()
|
||||
{
|
||||
return $this->_classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* setBody()
|
||||
*
|
||||
* @param string $body
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public function setBody($body)
|
||||
{
|
||||
$this->_body = $body;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getBody()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return $this->_body;
|
||||
}
|
||||
|
||||
/**
|
||||
* isSourceDirty()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSourceDirty()
|
||||
{
|
||||
if (($docblock = $this->getDocblock()) && $docblock->isSourceDirty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($this->_classes as $class) {
|
||||
if ($class->isSourceDirty()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::isSourceDirty();
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
if ($this->isSourceDirty() === false) {
|
||||
return $this->_sourceContent;
|
||||
}
|
||||
|
||||
$output = '';
|
||||
|
||||
// start with the body (if there), or open tag
|
||||
if (preg_match('#(?:\s*)<\?php#', $this->getBody()) == false) {
|
||||
$output = '<?php' . self::LINE_FEED;
|
||||
}
|
||||
|
||||
// if there are markers, put the body into the output
|
||||
$body = $this->getBody();
|
||||
if (preg_match('#/\* Zend_CodeGenerator_Php_File-(.*?)Marker:#', $body)) {
|
||||
$output .= $body;
|
||||
$body = '';
|
||||
}
|
||||
|
||||
// Add file docblock, if any
|
||||
if (null !== ($docblock = $this->getDocblock())) {
|
||||
$docblock->setIndentation('');
|
||||
$regex = preg_quote(self::$_markerDocblock, '#');
|
||||
if (preg_match('#'.$regex.'#', $output)) {
|
||||
$output = preg_replace('#'.$regex.'#', $docblock->generate(), $output, 1);
|
||||
} else {
|
||||
$output .= $docblock->generate() . self::LINE_FEED;
|
||||
}
|
||||
}
|
||||
|
||||
// newline
|
||||
$output .= self::LINE_FEED;
|
||||
|
||||
// process required files
|
||||
// @todo marker replacement for required files
|
||||
$requiredFiles = $this->getRequiredFiles();
|
||||
if (!empty($requiredFiles)) {
|
||||
foreach ($requiredFiles as $requiredFile) {
|
||||
$output .= 'require_once \'' . $requiredFile . '\';' . self::LINE_FEED;
|
||||
}
|
||||
|
||||
$output .= self::LINE_FEED;
|
||||
}
|
||||
|
||||
// process classes
|
||||
$classes = $this->getClasses();
|
||||
if (!empty($classes)) {
|
||||
foreach ($classes as $class) {
|
||||
$regex = str_replace('?', $class->getName(), self::$_markerClass);
|
||||
$regex = preg_quote($regex, '#');
|
||||
if (preg_match('#'.$regex.'#', $output)) {
|
||||
$output = preg_replace('#'.$regex.'#', $class->generate(), $output, 1);
|
||||
} else {
|
||||
$output .= $class->generate() . self::LINE_FEED;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!empty($body)) {
|
||||
|
||||
// add an extra space betwee clsses and
|
||||
if (!empty($classes)) {
|
||||
$output .= self::LINE_FEED;
|
||||
}
|
||||
|
||||
$output .= $body;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public function write()
|
||||
{
|
||||
if ($this->_filename == '' || !is_writable(dirname($this->_filename))) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('This code generator object is not writable.');
|
||||
}
|
||||
file_put_contents($this->_filename, $this->generate());
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,222 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_CodeGenerator_Php_Member_Abstract extends Zend_CodeGenerator_Php_Abstract
|
||||
{
|
||||
|
||||
/**#@+
|
||||
* @param const string
|
||||
*/
|
||||
const VISIBILITY_PUBLIC = 'public';
|
||||
const VISIBILITY_PROTECTED = 'protected';
|
||||
const VISIBILITY_PRIVATE = 'private';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* @var Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
protected $_docblock = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isAbstract = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isFinal = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isStatic = false;
|
||||
|
||||
/**
|
||||
* @var const
|
||||
*/
|
||||
protected $_visibility = self::VISIBILITY_PUBLIC;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_name = null;
|
||||
|
||||
/**
|
||||
* setDocblock() Set the docblock
|
||||
*
|
||||
* @param Zend_CodeGenerator_Php_Docblock|array|string $docblock
|
||||
* @return Zend_CodeGenerator_Php_File
|
||||
*/
|
||||
public function setDocblock($docblock)
|
||||
{
|
||||
if (is_string($docblock)) {
|
||||
$docblock = array('shortDescription' => $docblock);
|
||||
}
|
||||
|
||||
if (is_array($docblock)) {
|
||||
$docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
|
||||
} elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
|
||||
}
|
||||
|
||||
$this->_docblock = $docblock;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDocblock()
|
||||
*
|
||||
* @return Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
public function getDocblock()
|
||||
{
|
||||
return $this->_docblock;
|
||||
}
|
||||
|
||||
/**
|
||||
* setAbstract()
|
||||
*
|
||||
* @param bool $isAbstract
|
||||
* @return Zend_CodeGenerator_Php_Member_Abstract
|
||||
*/
|
||||
public function setAbstract($isAbstract)
|
||||
{
|
||||
$this->_isAbstract = ($isAbstract) ? true : false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* isAbstract()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAbstract()
|
||||
{
|
||||
return $this->_isAbstract;
|
||||
}
|
||||
|
||||
/**
|
||||
* setFinal()
|
||||
*
|
||||
* @param bool $isFinal
|
||||
* @return Zend_CodeGenerator_Php_Member_Abstract
|
||||
*/
|
||||
public function setFinal($isFinal)
|
||||
{
|
||||
$this->_isFinal = ($isFinal) ? true : false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* isFinal()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isFinal()
|
||||
{
|
||||
return $this->_isFinal;
|
||||
}
|
||||
|
||||
/**
|
||||
* setStatic()
|
||||
*
|
||||
* @param bool $isStatic
|
||||
* @return Zend_CodeGenerator_Php_Member_Abstract
|
||||
*/
|
||||
public function setStatic($isStatic)
|
||||
{
|
||||
$this->_isStatic = ($isStatic) ? true : false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* isStatic()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isStatic()
|
||||
{
|
||||
return $this->_isStatic;
|
||||
}
|
||||
|
||||
/**
|
||||
* setVisitibility()
|
||||
*
|
||||
* @param const $visibility
|
||||
* @return Zend_CodeGenerator_Php_Member_Abstract
|
||||
*/
|
||||
public function setVisibility($visibility)
|
||||
{
|
||||
$this->_visibility = $visibility;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getVisibility()
|
||||
*
|
||||
* @return const
|
||||
*/
|
||||
public function getVisibility()
|
||||
{
|
||||
return $this->_visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* setName()
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_CodeGenerator_Php_Member_Abstract
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->_name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->_name;
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Member_Container extends ArrayObject
|
||||
{
|
||||
|
||||
/**#@+
|
||||
* @param const string
|
||||
*/
|
||||
const TYPE_PROPERTY = 'property';
|
||||
const TYPE_METHOD = 'method';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* @var const|string
|
||||
*/
|
||||
protected $_type = self::TYPE_PROPERTY;
|
||||
|
||||
/**
|
||||
* __construct()
|
||||
*
|
||||
* @param const|string $type
|
||||
*/
|
||||
public function __construct($type = self::TYPE_PROPERTY)
|
||||
{
|
||||
$this->_type = $type;
|
||||
parent::__construct(array(), self::ARRAY_AS_PROPS);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,234 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Member_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Docblock.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Parameter
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Parameter.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstract
|
||||
{
|
||||
/**
|
||||
* @var Zend_CodeGenerator_Php_Docblock
|
||||
*/
|
||||
protected $_docblock = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isFinal = false;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_parameters = array();
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_body = null;
|
||||
|
||||
/**
|
||||
* fromReflection()
|
||||
*
|
||||
* @param Zend_Reflection_Method $reflectionMethod
|
||||
* @return Zend_CodeGenerator_Php_Method
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Method $reflectionMethod)
|
||||
{
|
||||
$method = new self();
|
||||
|
||||
$method->setSourceContent($reflectionMethod->getContents(false));
|
||||
$method->setSourceDirty(false);
|
||||
|
||||
if ($reflectionMethod->getDocComment() != '') {
|
||||
$method->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionMethod->getDocblock()));
|
||||
}
|
||||
|
||||
$method->setFinal($reflectionMethod->isFinal());
|
||||
|
||||
if ($reflectionMethod->isPrivate()) {
|
||||
$method->setVisibility(self::VISIBILITY_PRIVATE);
|
||||
} elseif ($reflectionMethod->isProtected()) {
|
||||
$method->setVisibility(self::VISIBILITY_PROTECTED);
|
||||
} else {
|
||||
$method->setVisibility(self::VISIBILITY_PUBLIC);
|
||||
}
|
||||
|
||||
$method->setStatic($reflectionMethod->isStatic());
|
||||
|
||||
$method->setName($reflectionMethod->getName());
|
||||
|
||||
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
|
||||
$method->setParameter(Zend_CodeGenerator_Php_Parameter::fromReflection($reflectionParameter));
|
||||
}
|
||||
|
||||
$method->setBody($reflectionMethod->getBody());
|
||||
|
||||
return $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* setFinal()
|
||||
*
|
||||
* @param bool $isFinal
|
||||
*/
|
||||
public function setFinal($isFinal)
|
||||
{
|
||||
$this->_isFinal = ($isFinal) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* setParameters()
|
||||
*
|
||||
* @param array $parameters
|
||||
* @return Zend_CodeGenerator_Php_Method
|
||||
*/
|
||||
public function setParameters(Array $parameters)
|
||||
{
|
||||
foreach ($parameters as $parameter) {
|
||||
$this->setParameter($parameter);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* setParameter()
|
||||
*
|
||||
* @param Zend_CodeGenerator_Php_Parameter|array $parameter
|
||||
* @return Zend_CodeGenerator_Php_Method
|
||||
*/
|
||||
public function setParameter($parameter)
|
||||
{
|
||||
if (is_array($parameter)) {
|
||||
$parameter = new Zend_CodeGenerator_Php_Parameter($parameter);
|
||||
$parameterName = $parameter->getName();
|
||||
} elseif ($parameter instanceof Zend_CodeGenerator_Php_Parameter) {
|
||||
$parameterName = $parameter->getName();
|
||||
} else {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('setParameter() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Parameter');
|
||||
}
|
||||
|
||||
$this->_parameters[$parameterName] = $parameter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getParameters()
|
||||
*
|
||||
* @return array Array of Zend_CodeGenerator_Php_Parameter
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->_parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* setBody()
|
||||
*
|
||||
* @param string $body
|
||||
* @return Zend_CodeGenerator_Php_Method
|
||||
*/
|
||||
public function setBody($body)
|
||||
{
|
||||
$this->_body = $body;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getBody()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return $this->_body;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$output = '';
|
||||
|
||||
$indent = $this->getIndentation();
|
||||
|
||||
if (($docblock = $this->getDocblock()) !== null) {
|
||||
$docblock->setIndentation($indent);
|
||||
$output .= $docblock->generate();
|
||||
}
|
||||
|
||||
$output .= $indent;
|
||||
|
||||
if ($this->isAbstract()) {
|
||||
$output .= 'abstract ';
|
||||
} else {
|
||||
$output .= (($this->isFinal()) ? 'final ' : '');
|
||||
}
|
||||
|
||||
$output .= $this->getVisibility()
|
||||
. (($this->isStatic()) ? ' static' : '')
|
||||
. ' function ' . $this->getName() . '(';
|
||||
|
||||
$parameters = $this->getParameters();
|
||||
if (!empty($parameters)) {
|
||||
foreach ($parameters as $parameter) {
|
||||
$parameterOuput[] = $parameter->generate();
|
||||
}
|
||||
|
||||
$output .= implode(', ', $parameterOuput);
|
||||
}
|
||||
|
||||
$output .= ')' . self::LINE_FEED . $indent . '{' . self::LINE_FEED;
|
||||
|
||||
if ($this->_body) {
|
||||
$output .= ' '
|
||||
. str_replace(self::LINE_FEED, self::LINE_FEED . $indent . $indent, trim($this->_body))
|
||||
. self::LINE_FEED;
|
||||
}
|
||||
|
||||
$output .= $indent . '}' . self::LINE_FEED;
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,250 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_ParameterDefaultValue
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Parameter/DefaultValue.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_type = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_name = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_defaultValue = null;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $_position = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_passedByReference = false;
|
||||
|
||||
/**
|
||||
* fromReflection()
|
||||
*
|
||||
* @param Zend_Reflection_Parameter $reflectionParameter
|
||||
* @return Zend_CodeGenerator_Php_Parameter
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Parameter $reflectionParameter)
|
||||
{
|
||||
$param = new Zend_CodeGenerator_Php_Parameter();
|
||||
$param->setName($reflectionParameter->getName());
|
||||
|
||||
if($reflectionParameter->isArray()) {
|
||||
$param->setType('array');
|
||||
} else {
|
||||
$typeClass = $reflectionParameter->getClass();
|
||||
if($typeClass !== null) {
|
||||
$param->setType($typeClass->getName());
|
||||
}
|
||||
}
|
||||
|
||||
$param->setPosition($reflectionParameter->getPosition());
|
||||
|
||||
if($reflectionParameter->isOptional()) {
|
||||
$param->setDefaultValue($reflectionParameter->getDefaultValue());
|
||||
}
|
||||
$param->setPassedByReference($reflectionParameter->isPassedByReference());
|
||||
|
||||
return $param;
|
||||
}
|
||||
|
||||
/**
|
||||
* setType()
|
||||
*
|
||||
* @param string $type
|
||||
* @return Zend_CodeGenerator_Php_Parameter
|
||||
*/
|
||||
public function setType($type)
|
||||
{
|
||||
$this->_type = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getType()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* setName()
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_CodeGenerator_Php_Parameter
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->_name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default value of the parameter.
|
||||
*
|
||||
* Certain variables are difficult to expres
|
||||
*
|
||||
* @param null|bool|string|int|float|Zend_CodeGenerator_Php_Parameter_DefaultValue $defaultValue
|
||||
* @return Zend_CodeGenerator_Php_Parameter
|
||||
*/
|
||||
public function setDefaultValue($defaultValue)
|
||||
{
|
||||
if($defaultValue === null) {
|
||||
$this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("null");
|
||||
} else if(is_array($defaultValue)) {
|
||||
$defaultValue = str_replace(array("\r", "\n"), "", var_export($defaultValue, true));
|
||||
$this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue($defaultValue);
|
||||
} else if(is_bool($defaultValue)) {
|
||||
if($defaultValue == true) {
|
||||
$this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("true");
|
||||
} else {
|
||||
$this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("false");
|
||||
}
|
||||
} else {
|
||||
$this->_defaultValue = $defaultValue;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDefaultValue()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultValue()
|
||||
{
|
||||
return $this->_defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* setPosition()
|
||||
*
|
||||
* @param int $position
|
||||
* @return Zend_CodeGenerator_Php_Parameter
|
||||
*/
|
||||
public function setPosition($position)
|
||||
{
|
||||
$this->_position = $position;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getPosition()
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPosition()
|
||||
{
|
||||
return $this->_position;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getPassedByReference()
|
||||
{
|
||||
return $this->_passedByReference;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $passedByReference
|
||||
* @return Zend_CodeGenerator_Php_Parameter
|
||||
*/
|
||||
public function setPassedByReference($passedByReference)
|
||||
{
|
||||
$this->_passedByReference = $passedByReference;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$output = '';
|
||||
|
||||
if ($this->_type) {
|
||||
$output .= $this->_type . ' ';
|
||||
}
|
||||
|
||||
if($this->_passedByReference === true) {
|
||||
$output .= '&';
|
||||
}
|
||||
|
||||
$output .= '$' . $this->_name;
|
||||
|
||||
if ($this->_defaultValue !== null) {
|
||||
$output .= ' = ';
|
||||
if (is_string($this->_defaultValue)) {
|
||||
$output .= '\'' . $this->_defaultValue . '\'';
|
||||
} else if($this->_defaultValue instanceof Zend_CodeGenerator_Php_ParameterDefaultValue) {
|
||||
$output .= (string)$this->_defaultValue;
|
||||
} else {
|
||||
$output .= $this->_defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage Php
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* A value-holder object for non-expressable parameter default values, such as null, booleans and empty array()
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage Php
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Parameter_DefaultValue
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_defaultValue = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $defaultValue
|
||||
* @throws Zend_CodeGenerator_Php_Exception
|
||||
*/
|
||||
public function __construct($defaultValue)
|
||||
{
|
||||
if(!is_string($defaultValue)) {
|
||||
require_once "Zend/CodeGenerator/Php/Exception.php";
|
||||
throw new Zend_CodeGenerator_Php_Exception(
|
||||
"Can only set a string as default value representation, ".
|
||||
"but ".gettype($defaultValue)." was given."
|
||||
);
|
||||
}
|
||||
$this->_defaultValue = $defaultValue;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->_defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -1,179 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Member_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Property_DefaultValue
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Property/DefaultValue.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Property extends Zend_CodeGenerator_Php_Member_Abstract
|
||||
{
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isConst = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_defaultValue = null;
|
||||
|
||||
/**
|
||||
* fromReflection()
|
||||
*
|
||||
* @param Zend_Reflection_Property $reflectionProperty
|
||||
* @return Zend_CodeGenerator_Php_Property
|
||||
*/
|
||||
public static function fromReflection(Zend_Reflection_Property $reflectionProperty)
|
||||
{
|
||||
$property = new self();
|
||||
|
||||
$property->setName($reflectionProperty->getName());
|
||||
|
||||
$allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
|
||||
|
||||
$property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]);
|
||||
|
||||
if ($reflectionProperty->getDocComment() != '') {
|
||||
$property->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionProperty->getDocComment()));
|
||||
}
|
||||
|
||||
if ($reflectionProperty->isStatic()) {
|
||||
$property->setStatic(true);
|
||||
}
|
||||
|
||||
if ($reflectionProperty->isPrivate()) {
|
||||
$property->setVisibility(self::VISIBILITY_PRIVATE);
|
||||
} elseif ($reflectionProperty->isProtected()) {
|
||||
$property->setVisibility(self::VISIBILITY_PROTECTED);
|
||||
} else {
|
||||
$property->setVisibility(self::VISIBILITY_PUBLIC);
|
||||
}
|
||||
|
||||
$property->setSourceDirty(false);
|
||||
|
||||
return $property;
|
||||
}
|
||||
|
||||
/**
|
||||
* setConst()
|
||||
*
|
||||
* @param bool $const
|
||||
* @return Zend_CodeGenerator_Php_Property
|
||||
*/
|
||||
public function setConst($const)
|
||||
{
|
||||
$this->_isConst = $const;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* isConst()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConst()
|
||||
{
|
||||
return ($this->_isConst) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* setDefaultValue()
|
||||
*
|
||||
* @param Zend_CodeGenerator_Php_Property_DefaultValue|string|array $defaultValue
|
||||
* @return Zend_CodeGenerator_Php_Property
|
||||
*/
|
||||
public function setDefaultValue($defaultValue)
|
||||
{
|
||||
// if it looks like
|
||||
if (is_array($defaultValue)
|
||||
&& array_key_exists('value', $defaultValue)
|
||||
&& array_key_exists('type', $defaultValue)) {
|
||||
$defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue($defaultValue);
|
||||
}
|
||||
|
||||
if (!($defaultValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue)) {
|
||||
$defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue(array('value' => $defaultValue));
|
||||
}
|
||||
|
||||
$this->_defaultValue = $defaultValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDefaultValue()
|
||||
*
|
||||
* @return Zend_CodeGenerator_Php_Property_DefaultValue
|
||||
*/
|
||||
public function getDefaultValue()
|
||||
{
|
||||
return $this->_defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$name = $this->getName();
|
||||
$defaultValue = $this->getDefaultValue();
|
||||
|
||||
$output = '';
|
||||
|
||||
if (($docblock = $this->getDocblock()) !== null) {
|
||||
$docblock->setIndentation(' ');
|
||||
$output .= $docblock->generate();
|
||||
}
|
||||
|
||||
if ($this->isConst()) {
|
||||
if ($defaultValue != null && !$defaultValue->isValidConstantType()) {
|
||||
require_once 'Zend/CodeGenerator/Php/Exception.php';
|
||||
throw new Zend_CodeGenerator_Php_Exception('The property ' . $this->_name . ' is said to be '
|
||||
. 'constant but does not have a valid constant value.');
|
||||
}
|
||||
$output .= $this->_indentation . 'const ' . $name . ' = '
|
||||
. (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
|
||||
} else {
|
||||
$output .= $this->_indentation
|
||||
. $this->getVisibility()
|
||||
. (($this->isStatic()) ? ' static' : '')
|
||||
. ' $' . $name . ' = '
|
||||
. (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,323 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @subpackage PHP
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_CodeGenerator_Php_Abstract
|
||||
*/
|
||||
require_once 'Zend/CodeGenerator/Php/Abstract.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_CodeGenerator
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Php_Abstract
|
||||
{
|
||||
/**#@+
|
||||
* Constant values
|
||||
*/
|
||||
const TYPE_AUTO = 'auto';
|
||||
const TYPE_BOOLEAN = 'boolean';
|
||||
const TYPE_BOOL = 'bool';
|
||||
const TYPE_NUMBER = 'number';
|
||||
const TYPE_INTEGER = 'integer';
|
||||
const TYPE_INT = 'int';
|
||||
const TYPE_FLOAT = 'float';
|
||||
const TYPE_DOUBLE = 'double';
|
||||
const TYPE_STRING = 'string';
|
||||
const TYPE_ARRAY = 'array';
|
||||
const TYPE_CONSTANT = 'constant';
|
||||
const TYPE_NULL = 'null';
|
||||
const TYPE_OTHER = 'other';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* @var array of reflected constants
|
||||
*/
|
||||
protected static $_constants = array();
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_value = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_type = self::TYPE_AUTO;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $_arrayDepth = 1;
|
||||
|
||||
/**
|
||||
* _init()
|
||||
*
|
||||
* This method will prepare the constant array for this class
|
||||
*/
|
||||
protected function _init()
|
||||
{
|
||||
if(count(self::$_constants) == 0) {
|
||||
$reflect = new ReflectionClass(get_class($this));
|
||||
self::$_constants = $reflect->getConstants();
|
||||
unset($reflect);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* isValidConstantType()
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValidConstantType()
|
||||
{
|
||||
if ($this->_type == self::TYPE_AUTO) {
|
||||
$type = $this->_getAutoDeterminedType($this->_value);
|
||||
}
|
||||
|
||||
// valid types for constants
|
||||
$scalarTypes = array(
|
||||
self::TYPE_BOOLEAN,
|
||||
self::TYPE_BOOL,
|
||||
self::TYPE_NUMBER,
|
||||
self::TYPE_INTEGER,
|
||||
self::TYPE_INT,
|
||||
self::TYPE_FLOAT,
|
||||
self::TYPE_DOUBLE,
|
||||
self::TYPE_STRING,
|
||||
self::TYPE_CONSTANT,
|
||||
self::TYPE_NULL
|
||||
);
|
||||
|
||||
return in_array($type, $scalarTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* setValue()
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return Zend_CodeGenerator_Php_Property_DefaultValue
|
||||
*/
|
||||
public function setValue($value)
|
||||
{
|
||||
$this->_value = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getValue()
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* setType()
|
||||
*
|
||||
* @param string $type
|
||||
* @return Zend_CodeGenerator_Php_Property_DefaultValue
|
||||
*/
|
||||
public function setType($type)
|
||||
{
|
||||
$this->_type = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getType()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* setArrayDepth()
|
||||
*
|
||||
* @param int $arrayDepth
|
||||
* @return Zend_CodeGenerator_Php_Property_DefaultValue
|
||||
*/
|
||||
public function setArrayDepth($arrayDepth)
|
||||
{
|
||||
$this->_arrayDepth = $arrayDepth;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getArrayDepth()
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getArrayDepth()
|
||||
{
|
||||
return $this->_arrayDepth;
|
||||
}
|
||||
|
||||
/**
|
||||
* _getValidatedType()
|
||||
*
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
protected function _getValidatedType($type)
|
||||
{
|
||||
if (($constName = array_search($type, self::$_constants)) !== false) {
|
||||
return $type;
|
||||
}
|
||||
|
||||
return self::TYPE_AUTO;
|
||||
}
|
||||
|
||||
/**
|
||||
* _getAutoDeterminedType()
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return string
|
||||
*/
|
||||
public function _getAutoDeterminedType($value)
|
||||
{
|
||||
switch (gettype($value)) {
|
||||
case 'boolean':
|
||||
return self::TYPE_BOOLEAN;
|
||||
case 'integer':
|
||||
return self::TYPE_INT;
|
||||
case 'string':
|
||||
return self::TYPE_STRING;
|
||||
case 'double':
|
||||
case 'float':
|
||||
case 'integer':
|
||||
return self::TYPE_NUMBER;
|
||||
case 'array':
|
||||
return self::TYPE_ARRAY;
|
||||
case 'NULL':
|
||||
return self::TYPE_NULL;
|
||||
case 'object':
|
||||
case 'resource':
|
||||
case 'unknown type':
|
||||
default:
|
||||
return self::TYPE_OTHER;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* generate()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$type = $this->_type;
|
||||
|
||||
if ($type != self::TYPE_AUTO) {
|
||||
$type = $this->_getValidatedType($type);
|
||||
}
|
||||
|
||||
$value = $this->_value;
|
||||
|
||||
if ($type == self::TYPE_AUTO) {
|
||||
$type = $this->_getAutoDeterminedType($value);
|
||||
|
||||
if ($type == self::TYPE_ARRAY) {
|
||||
$rii = new RecursiveIteratorIterator(
|
||||
$it = new RecursiveArrayIterator($value),
|
||||
RecursiveIteratorIterator::SELF_FIRST
|
||||
);
|
||||
foreach ($rii as $curKey => $curValue) {
|
||||
if (!$curValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue) {
|
||||
$curValue = new self(array('value' => $curValue));
|
||||
$rii->getSubIterator()->offsetSet($curKey, $curValue);
|
||||
}
|
||||
$curValue->setArrayDepth($rii->getDepth());
|
||||
}
|
||||
$value = $rii->getSubIterator()->getArrayCopy();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$output = '';
|
||||
|
||||
switch ($type) {
|
||||
case self::TYPE_BOOLEAN:
|
||||
case self::TYPE_BOOL:
|
||||
$output .= ( $value ? 'true' : 'false' );
|
||||
break;
|
||||
case self::TYPE_STRING:
|
||||
$output .= "'" . addcslashes($value, "'") . "'";
|
||||
break;
|
||||
case self::TYPE_NULL:
|
||||
$output .= 'null';
|
||||
break;
|
||||
case self::TYPE_NUMBER:
|
||||
case self::TYPE_INTEGER:
|
||||
case self::TYPE_INT:
|
||||
case self::TYPE_FLOAT:
|
||||
case self::TYPE_DOUBLE:
|
||||
case self::TYPE_CONSTANT:
|
||||
$output .= $value;
|
||||
break;
|
||||
case self::TYPE_ARRAY:
|
||||
$output .= 'array(';
|
||||
$curArrayMultiblock = false;
|
||||
if (count($value) > 1) {
|
||||
$curArrayMultiblock = true;
|
||||
$output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
|
||||
}
|
||||
$outputParts = array();
|
||||
$noKeyIndex = 0;
|
||||
foreach ($value as $n => $v) {
|
||||
$v->setArrayDepth($this->_arrayDepth + 1);
|
||||
$partV = $v->generate();
|
||||
$partV = substr($partV, 0, strlen($partV)-1);
|
||||
if ($n === $noKeyIndex) {
|
||||
$outputParts[] = $partV;
|
||||
$noKeyIndex++;
|
||||
} else {
|
||||
$outputParts[] = (is_int($n) ? $n : "'" . addcslashes($n, "'") . "'") . ' => ' . $partV;
|
||||
}
|
||||
|
||||
}
|
||||
$output .= implode(',' . PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1), $outputParts);
|
||||
if ($curArrayMultiblock == true) {
|
||||
$output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
|
||||
}
|
||||
$output .= ')';
|
||||
break;
|
||||
case self::TYPE_OTHER:
|
||||
default:
|
||||
require_once "Zend/CodeGenerator/Php/Exception.php";
|
||||
throw new Zend_CodeGenerator_Php_Exception(
|
||||
"Type '".get_class($value)."' is unknown or cannot be used as property default value."
|
||||
);
|
||||
}
|
||||
|
||||
$output .= ';';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
@@ -1,456 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Config implements Countable, Iterator
|
||||
{
|
||||
/**
|
||||
* Whether in-memory modifications to configuration data are allowed
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_allowModifications;
|
||||
|
||||
/**
|
||||
* Iteration index
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $_index;
|
||||
|
||||
/**
|
||||
* Number of elements in configuration data
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $_count;
|
||||
|
||||
/**
|
||||
* Contains array of configuration data
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_data;
|
||||
|
||||
/**
|
||||
* Used when unsetting values during iteration to ensure we do not skip
|
||||
* the next element
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_skipNextIteration;
|
||||
|
||||
/**
|
||||
* Contains which config file sections were loaded. This is null
|
||||
* if all sections were loaded, a string name if one section is loaded
|
||||
* and an array of string names if multiple sections were loaded.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_loadedSection;
|
||||
|
||||
/**
|
||||
* This is used to track section inheritance. The keys are names of sections that
|
||||
* extend other sections, and the values are the extended sections.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_extends = array();
|
||||
|
||||
/**
|
||||
* Load file error string.
|
||||
*
|
||||
* Is null if there was no error while file loading
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_loadFileErrorStr = null;
|
||||
|
||||
/**
|
||||
* Zend_Config provides a property based interface to
|
||||
* an array. The data are read-only unless $allowModifications
|
||||
* is set to true on construction.
|
||||
*
|
||||
* Zend_Config also implements Countable and Iterator to
|
||||
* facilitate easy access to the data.
|
||||
*
|
||||
* @param array $array
|
||||
* @param boolean $allowModifications
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $array, $allowModifications = false)
|
||||
{
|
||||
$this->_allowModifications = (boolean) $allowModifications;
|
||||
$this->_loadedSection = null;
|
||||
$this->_index = 0;
|
||||
$this->_data = array();
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$this->_data[$key] = new self($value, $this->_allowModifications);
|
||||
} else {
|
||||
$this->_data[$key] = $value;
|
||||
}
|
||||
}
|
||||
$this->_count = count($this->_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a value and return $default if there is no element set.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
$result = $default;
|
||||
if (array_key_exists($name, $this->_data)) {
|
||||
$result = $this->_data[$name];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function so that $obj->value will work.
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
return $this->get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only allow setting of a property if $allowModifications
|
||||
* was set to true on construction. Otherwise, throw an exception.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @throws Zend_Config_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
if ($this->_allowModifications) {
|
||||
if (is_array($value)) {
|
||||
$this->_data[$name] = new self($value, true);
|
||||
} else {
|
||||
$this->_data[$name] = $value;
|
||||
}
|
||||
$this->_count = count($this->_data);
|
||||
} else {
|
||||
/** @see Zend_Config_Exception */
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('Zend_Config is read only');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deep clone of this instance to ensure that nested Zend_Configs
|
||||
* are also cloned.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
$array = array();
|
||||
foreach ($this->_data as $key => $value) {
|
||||
if ($value instanceof Zend_Config) {
|
||||
$array[$key] = clone $value;
|
||||
} else {
|
||||
$array[$key] = $value;
|
||||
}
|
||||
}
|
||||
$this->_data = $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an associative array of the stored data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$array = array();
|
||||
$data = $this->_data;
|
||||
foreach ($data as $key => $value) {
|
||||
if ($value instanceof Zend_Config) {
|
||||
$array[$key] = $value->toArray();
|
||||
} else {
|
||||
$array[$key] = $value;
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Support isset() overloading on PHP 5.1
|
||||
*
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function __isset($name)
|
||||
{
|
||||
return isset($this->_data[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Support unset() overloading on PHP 5.1
|
||||
*
|
||||
* @param string $name
|
||||
* @throws Zend_Config_Exception
|
||||
* @return void
|
||||
*/
|
||||
public function __unset($name)
|
||||
{
|
||||
if ($this->_allowModifications) {
|
||||
unset($this->_data[$name]);
|
||||
$this->_count = count($this->_data);
|
||||
$this->_skipNextIteration = true;
|
||||
} else {
|
||||
/** @see Zend_Config_Exception */
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('Zend_Config is read only');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Countable interface
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return $this->_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Iterator interface
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
$this->_skipNextIteration = false;
|
||||
return current($this->_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Iterator interface
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return key($this->_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Iterator interface
|
||||
*
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
if ($this->_skipNextIteration) {
|
||||
$this->_skipNextIteration = false;
|
||||
return;
|
||||
}
|
||||
next($this->_data);
|
||||
$this->_index++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Iterator interface
|
||||
*
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->_skipNextIteration = false;
|
||||
reset($this->_data);
|
||||
$this->_index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Iterator interface
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return $this->_index < $this->_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the section name(s) loaded.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSectionName()
|
||||
{
|
||||
if(is_array($this->_loadedSection) && count($this->_loadedSection) == 1) {
|
||||
$this->_loadedSection = $this->_loadedSection[0];
|
||||
}
|
||||
return $this->_loadedSection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if all sections were loaded
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function areAllSectionsLoaded()
|
||||
{
|
||||
return $this->_loadedSection === null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Merge another Zend_Config with this one. The items
|
||||
* in $merge will override the same named items in
|
||||
* the current config.
|
||||
*
|
||||
* @param Zend_Config $merge
|
||||
* @return Zend_Config
|
||||
*/
|
||||
public function merge(Zend_Config $merge)
|
||||
{
|
||||
foreach($merge as $key => $item) {
|
||||
if(array_key_exists($key, $this->_data)) {
|
||||
if($item instanceof Zend_Config && $this->$key instanceof Zend_Config) {
|
||||
$this->$key = $this->$key->merge(new Zend_Config($item->toArray(), !$this->readOnly()));
|
||||
} else {
|
||||
$this->$key = $item;
|
||||
}
|
||||
} else {
|
||||
if($item instanceof Zend_Config) {
|
||||
$this->$key = new Zend_Config($item->toArray(), !$this->readOnly());
|
||||
} else {
|
||||
$this->$key = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent any more modifications being made to this instance. Useful
|
||||
* after merge() has been used to merge multiple Zend_Config objects
|
||||
* into one object which should then not be modified again.
|
||||
*
|
||||
*/
|
||||
public function setReadOnly()
|
||||
{
|
||||
$this->_allowModifications = false;
|
||||
foreach ($this->_data as $key => $value) {
|
||||
if ($value instanceof Zend_Config) {
|
||||
$value->setReadOnly();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if this Zend_Config object is read only or not.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function readOnly()
|
||||
{
|
||||
return !$this->_allowModifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current extends
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getExtends()
|
||||
{
|
||||
return $this->_extends;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an extend for Zend_Config_Writer
|
||||
*
|
||||
* @param string $extendingSection
|
||||
* @param string $extendedSection
|
||||
* @return void
|
||||
*/
|
||||
public function setExtend($extendingSection, $extendedSection = null)
|
||||
{
|
||||
if ($extendedSection === null && isset($this->_extends[$extendingSection])) {
|
||||
unset($this->_extends[$extendingSection]);
|
||||
} else if ($extendedSection !== null) {
|
||||
$this->_extends[$extendingSection] = $extendedSection;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception if $extendingSection may not extend $extendedSection,
|
||||
* and tracks the section extension if it is valid.
|
||||
*
|
||||
* @param string $extendingSection
|
||||
* @param string $extendedSection
|
||||
* @throws Zend_Config_Exception
|
||||
* @return void
|
||||
*/
|
||||
protected function _assertValidExtend($extendingSection, $extendedSection)
|
||||
{
|
||||
// detect circular section inheritance
|
||||
$extendedSectionCurrent = $extendedSection;
|
||||
while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
|
||||
if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
|
||||
/** @see Zend_Config_Exception */
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('Illegal circular inheritance detected');
|
||||
}
|
||||
$extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
|
||||
}
|
||||
// remember that this section extends another section
|
||||
$this->_extends[$extendingSection] = $extendedSection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle any errors from simplexml_load_file or parse_ini_file
|
||||
*
|
||||
* @param integer $errno
|
||||
* @param string $errstr
|
||||
* @param string $errfile
|
||||
* @param integer $errline
|
||||
*/
|
||||
protected function _loadFileErrorHandler($errno, $errstr, $errfile, $errline)
|
||||
{
|
||||
if ($this->_loadFileErrorStr === null) {
|
||||
$this->_loadFileErrorStr = $errstr;
|
||||
} else {
|
||||
$this->_loadFileErrorStr .= (PHP_EOL . $errstr);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to [email protected] so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Exception
|
||||
*/
|
||||
require_once 'Zend/Exception.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Config_Exception extends Zend_Exception {}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user