Node.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\FS;
  4. use Sabre\DAV\Exception\Forbidden;
  5. use Sabre\DAV\INode;
  6. use Sabre\Uri;
  7. /**
  8. * Base node-class.
  9. *
  10. * The node class implements the method used by both the File and the Directory classes
  11. *
  12. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  13. * @author Evert Pot (http://evertpot.com/)
  14. * @license http://sabre.io/license/ Modified BSD License
  15. */
  16. abstract class Node implements INode
  17. {
  18. /**
  19. * The path to the current node.
  20. *
  21. * @var string
  22. */
  23. protected $path;
  24. /**
  25. * The overridden name of the node.
  26. *
  27. * @var string
  28. */
  29. protected $overrideName;
  30. /**
  31. * Sets up the node, expects a full path name.
  32. *
  33. * If $overrideName is set, this node shows up in the tree under a
  34. * different name. In this case setName() will be disabled.
  35. *
  36. * @param string $path
  37. * @param string $overrideName
  38. */
  39. public function __construct($path, $overrideName = null)
  40. {
  41. $this->path = $path;
  42. $this->overrideName = $overrideName;
  43. }
  44. /**
  45. * Returns the name of the node.
  46. *
  47. * @return string
  48. */
  49. public function getName()
  50. {
  51. if ($this->overrideName) {
  52. return $this->overrideName;
  53. }
  54. list(, $name) = Uri\split($this->path);
  55. return $name;
  56. }
  57. /**
  58. * Renames the node.
  59. *
  60. * @param string $name The new name
  61. */
  62. public function setName($name)
  63. {
  64. if ($this->overrideName) {
  65. throw new Forbidden('This node cannot be renamed');
  66. }
  67. list($parentPath) = Uri\split($this->path);
  68. list(, $newName) = Uri\split($name);
  69. $newPath = $parentPath.'/'.$newName;
  70. rename($this->path, $newPath);
  71. $this->path = $newPath;
  72. }
  73. /**
  74. * Returns the last modification time, as a unix timestamp.
  75. *
  76. * @return int
  77. */
  78. public function getLastModified()
  79. {
  80. return filemtime($this->path);
  81. }
  82. }