Cache.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer;
  12. use Composer\IO\IOInterface;
  13. /**
  14. * Reads/writes to a filesystem cache
  15. *
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class Cache
  19. {
  20. private $io;
  21. private $root;
  22. private $enabled = true;
  23. public function __construct(IOInterface $io, $cacheDir)
  24. {
  25. $this->io = $io;
  26. $this->root = rtrim($cacheDir, '/\\') . '/';
  27. if (!is_dir($this->root)) {
  28. if (!@mkdir($this->root, 0777, true)) {
  29. $this->enabled = false;
  30. }
  31. }
  32. }
  33. public function getRoot()
  34. {
  35. return $this->root;
  36. }
  37. public function read($file)
  38. {
  39. if ($this->enabled && file_exists($this->root . $file)) {
  40. return file_get_contents($this->root . $file);
  41. }
  42. }
  43. public function write($file, $contents)
  44. {
  45. if ($this->enabled) {
  46. file_put_contents($this->root . $file, $contents);
  47. }
  48. }
  49. public function sha1($file)
  50. {
  51. if ($this->enabled && file_exists($this->root . $file)) {
  52. return sha1_file($this->root . $file);
  53. }
  54. }
  55. }