Transfer.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_File_Transfer
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Loader
  23. */
  24. #require_once 'Zend/Loader.php';
  25. /**
  26. * Base class for all protocols supporting file transfers
  27. *
  28. * @category Zend
  29. * @package Zend_File_Transfer
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_File_Transfer
  34. {
  35. /**
  36. * Array holding all directions
  37. *
  38. * @var array
  39. */
  40. protected $_adapter = array();
  41. /**
  42. * Creates a file processing handler
  43. *
  44. * @param string $adapter Adapter to use
  45. * @param boolean $direction OPTIONAL False means Download, true means upload
  46. * @param array $options OPTIONAL Options to set for this adapter
  47. * @throws Zend_File_Transfer_Exception
  48. */
  49. public function __construct($adapter = 'Http', $direction = false, $options = array())
  50. {
  51. $this->setAdapter($adapter, $direction, $options);
  52. }
  53. /**
  54. * Sets a new adapter
  55. *
  56. * @param string $adapter Adapter to use
  57. * @param boolean $direction OPTIONAL False means Download, true means upload
  58. * @param array $options OPTIONAL Options to set for this adapter
  59. * @throws Zend_File_Transfer_Exception
  60. */
  61. public function setAdapter($adapter, $direction = false, $options = array())
  62. {
  63. if (Zend_Loader::isReadable('Zend/File/Transfer/Adapter/' . ucfirst($adapter). '.php')) {
  64. $adapter = 'Zend_File_Transfer_Adapter_' . ucfirst($adapter);
  65. }
  66. if (!class_exists($adapter)) {
  67. Zend_Loader::loadClass($adapter);
  68. }
  69. $direction = (integer) $direction;
  70. $this->_adapter[$direction] = new $adapter($options);
  71. if (!$this->_adapter[$direction] instanceof Zend_File_Transfer_Adapter_Abstract) {
  72. #require_once 'Zend/File/Transfer/Exception.php';
  73. throw new Zend_File_Transfer_Exception("Adapter " . $adapter . " does not extend Zend_File_Transfer_Adapter_Abstract");
  74. }
  75. return $this;
  76. }
  77. /**
  78. * Returns all set adapters
  79. *
  80. * @param boolean $direction On null, all directions are returned
  81. * On false, download direction is returned
  82. * On true, upload direction is returned
  83. * @return array|Zend_File_Transfer_Adapter
  84. */
  85. public function getAdapter($direction = null)
  86. {
  87. if ($direction === null) {
  88. return $this->_adapter;
  89. }
  90. $direction = (integer) $direction;
  91. return $this->_adapter[$direction];
  92. }
  93. /**
  94. * Calls all methods from the adapter
  95. *
  96. * @param string $method Method to call
  97. * @param array $options Options for this method
  98. * @return mixed
  99. */
  100. public function __call($method, array $options)
  101. {
  102. if (array_key_exists('direction', $options)) {
  103. $direction = (integer) $options['direction'];
  104. } else {
  105. $direction = 0;
  106. }
  107. if (method_exists($this->_adapter[$direction], $method)) {
  108. return call_user_func_array(array($this->_adapter[$direction], $method), $options);
  109. }
  110. #require_once 'Zend/File/Transfer/Exception.php';
  111. throw new Zend_File_Transfer_Exception("Unknown method '" . $method . "' called!");
  112. }
  113. }