/application/libraries/Omeka/Storage.php

https://github.com/erochest/Omeka · PHP · 183 lines · 81 code · 17 blank · 85 comment · 11 complexity · 7c365ae31026219253078456697fd508 MD5 · raw file

  1. <?php
  2. /**
  3. * Omeka
  4. *
  5. * @copyright Copyright 2007-2012 Roy Rosenzweig Center for History and New Media
  6. * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
  7. */
  8. /**
  9. * Top-level helper class for handling file storage.
  10. *
  11. * @package Omeka\Storage
  12. */
  13. class Omeka_Storage
  14. {
  15. const OPTION_ADAPTER = 'adapter';
  16. const OPTION_ADAPTER_OPTIONS = 'adapterOptions';
  17. const OPTION_TEMP_DIR = 'tempDir';
  18. const MSG_NOT_INITIALIZED = 'The storage adapter is not initialized.';
  19. const MSG_NO_SUCH_METHOD = 'The storage adapter has no method "%s"';
  20. const MSG_INVALID_ADAPTER = 'Storage adapters must implement the Omeka_Storage_Adapter_AdapterInterface interface.';
  21. /**
  22. * @var Omeka_Storage_Adapter_AdapterInterface
  23. */
  24. private $_adapter;
  25. /**
  26. * @var string
  27. */
  28. private $_tempDir;
  29. /**
  30. * Allows storage options to be set immediately at construction.
  31. *
  32. * @param array $options If set, this array will be passed to
  33. * setOptions.
  34. */
  35. public function __construct(array $options = null)
  36. {
  37. if (isset($options)) {
  38. $this->setOptions($options);
  39. }
  40. }
  41. /**
  42. * Delegates calls directly to Omeka_Storage to the currently-set
  43. * storage adapter.
  44. *
  45. * All of the methods of the Adapter interface are accessible in
  46. * this way, as well as any other methods declared by the adapter.
  47. *
  48. * @param string $name Method name.
  49. * @param string $arguments Method arguments.
  50. * @return mixed
  51. */
  52. public function __call($name, $arguments)
  53. {
  54. if (!$this->_adapter) {
  55. throw new Omeka_Storage_Exception(self::MSG_NOT_INITIALIZED);
  56. }
  57. $callback = array($this->_adapter, $name);
  58. if (is_callable($callback)) {
  59. return call_user_func_array($callback, $arguments);
  60. } else {
  61. throw new Omeka_Storage_Exception(sprintf(self::MSG_NO_SUCH_METHOD, $name));
  62. }
  63. }
  64. /**
  65. * Set global options for the storage system, as well as any
  66. * adapter-specific options.
  67. *
  68. * @uses Omeka_Storage::setAdapter()
  69. * @param array $options Options to set. Valid options include:
  70. * * 'adapter': (string) Name of the storage adapter to use.
  71. * * 'adapterOptions': (array) Array of options to pass to the
  72. * adapter; see the specific adapter classes for details.
  73. * * 'temp_dir': (string) Local temporary directory where files
  74. * stored before they are handled by the adapter.
  75. */
  76. public function setOptions(array $options)
  77. {
  78. foreach ($options as $key => $value) {
  79. switch ($key) {
  80. case self::OPTION_ADAPTER:
  81. $adapterOptions = array();
  82. if (isset($options[self::OPTION_ADAPTER_OPTIONS])) {
  83. $adapterOptions = $options[self::OPTION_ADAPTER_OPTIONS];
  84. }
  85. $this->setAdapter($value, $adapterOptions);
  86. break;
  87. case self::OPTION_TEMP_DIR:
  88. $this->setTempDir($value);
  89. break;
  90. }
  91. }
  92. }
  93. /**
  94. * Set the storage adapter to be used, as well as options for that
  95. * adapter.
  96. *
  97. * You can either pass an already-constructed adapter object to this
  98. * method or use this method as a factory by passing the name of an
  99. * adapter class and options to set on it.
  100. *
  101. * @param Omeka_Storage_Adapter_AdapterInterface|string $adapter Storage
  102. * adapter to set. If an adapter object is passed, it is simply set as the
  103. * current adapter. If a string is passed, an object of that class is
  104. * created and set as the current adapter.
  105. * @param array|null $options If a string is passed to $adapter,
  106. * this array of options is passed to the class' constructor.
  107. */
  108. public function setAdapter($adapter, array $options = array())
  109. {
  110. if (is_string($adapter) && class_exists($adapter)) {
  111. $adapter = new $adapter($options);
  112. }
  113. if ($adapter instanceof Omeka_Storage_Adapter_AdapterInterface) {
  114. $this->_adapter = $adapter;
  115. } else {
  116. throw new Omeka_Storage_Exception(self::MSG_INVALID_ADAPTER);
  117. }
  118. }
  119. /**
  120. * Get the current storage adapter.
  121. *
  122. * You generally need to use the adapter object returned by this
  123. * method to perform any storage actions.
  124. *
  125. * @see Omeka_Storage::setAdapter()
  126. * @return Omeka_Storage_Adapter_AdapterInterface
  127. */
  128. public function getAdapter()
  129. {
  130. return $this->_adapter;
  131. }
  132. /**
  133. * Set the temporary file storage directory path.
  134. *
  135. * @see Omeka_Storage::getTempDir()
  136. * @param string $dir Local path to directory.
  137. */
  138. public function setTempDir($dir)
  139. {
  140. $this->_tempDir = $dir;
  141. }
  142. /**
  143. * Get the temporary file storage directory path.
  144. *
  145. * If no directory has been explicitly selected, the system's temp
  146. * directory is set as the temp dir and returned.
  147. *
  148. * @see Omeka_Storage::setTempDir()
  149. * @return string Local path to directory.
  150. */
  151. public function getTempDir()
  152. {
  153. if (!$this->_tempDir) {
  154. $this->_tempDir = sys_get_temp_dir();
  155. }
  156. return $this->_tempDir;
  157. }
  158. public function getPathByType($filename, $type = 'files')
  159. {
  160. return apply_filters(
  161. 'storage_path',
  162. $type . "/$filename",
  163. array('filename' => $filename, 'type' => $type)
  164. );
  165. }
  166. }