Anonymous 3D Imageboard http://cyberia.digital/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.0KB

  1. <?php
  2. namespace Lazer\Classes\Helpers;
  3. include "FileInterface.php";
  4. use Lazer\Classes\LazerException;
  5. /**
  6. * File managing class
  7. *
  8. * @category Helpers
  9. * @author Grzegorz Kuźnik
  10. * @copyright (c) 2013, Grzegorz Kuźnik
  11. * @license http://opensource.org/licenses/MIT The MIT License
  12. * @link https://github.com/Greg0/Lazer-Database GitHub Repository
  13. */
  14. class File implements FileInterface {
  15. /**
  16. * File name
  17. * @var string
  18. */
  19. protected $name;
  20. /**
  21. * File type (data|config)
  22. * @var string
  23. */
  24. protected $type;
  25. public static function table($name)
  26. {
  27. $file = new File;
  28. $file->name = $name;
  29. return $file;
  30. }
  31. public final function setType($type)
  32. {
  33. $this->type = $type;
  34. }
  35. public final function getPath()
  36. {
  37. if (!defined('LAZER_DATA_PATH'))
  38. {
  39. throw new LazerException('Please define constant LAZER_DATA_PATH (check README.md)');
  40. }
  41. else if (!empty($this->type))
  42. {
  43. return LAZER_DATA_PATH . $this->name . '.' . $this->type . '.json';
  44. }
  45. else
  46. {
  47. throw new LazerException('Please specify the type of file in class: ' . __CLASS__);
  48. }
  49. }
  50. public final function get($assoc = false)
  51. {
  52. return json_decode(file_get_contents($this->getPath()), $assoc);
  53. }
  54. public final function put($data)
  55. {
  56. return file_put_contents($this->getPath(), json_encode($data));
  57. }
  58. public final function exists()
  59. {
  60. return file_exists($this->getPath());
  61. }
  62. public final function remove()
  63. {
  64. $type = ucfirst($this->type);
  65. if ($this->exists())
  66. {
  67. if (unlink($this->getPath()))
  68. return TRUE;
  69. throw new LazerException($type . ': Deleting failed');
  70. }
  71. throw new LazerException($type . ': File does not exists');
  72. }
  73. }