1. <?php
  2. /**
  3. * File Buffer for Scheduled Runs
  4. *
  5. * Buffers file inputs for scheduled execution based on a configuration file.
  6. */
  7. class FileBuffer
  8. {
  9. private $config;
  10. private $buffer = [];
  11. public function __construct(array $config)
  12. {
  13. $this->config = $config;
  14. }
  15. /**
  16. * Adds a file path to the buffer.
  17. *
  18. * @param string $filePath The path to the file.
  19. * @return bool True on success, false on failure.
  20. */
  21. public function addFile(string $filePath): bool
  22. {
  23. if (file_exists($filePath) && is_readable($filePath)) {
  24. $this->buffer[] = $filePath;
  25. return true;
  26. }
  27. return false;
  28. }
  29. /**
  30. * Clears the buffer.
  31. */
  32. public function clearBuffer(): void
  33. {
  34. $this->buffer = [];
  35. }
  36. /**
  37. * Gets the buffered file paths.
  38. *
  39. * @return array An array of file paths.
  40. */
  41. public function getBuffer(): array
  42. {
  43. return $this->buffer;
  44. }
  45. /**
  46. * Processes the buffered files.
  47. *
  48. * @return array An array of processed file paths. Returns empty array on error.
  49. */
  50. public function processFiles(): array
  51. {
  52. $processedFiles = [];
  53. if (empty($this->buffer)) {
  54. return $processedFiles; // Return empty array if buffer is empty
  55. }
  56. foreach ($this->buffer as $filePath) {
  57. try {
  58. // Simulate file processing - replace with actual logic
  59. $processedFiles[] = $filePath . " - Processed";
  60. //Example: file_get_contents($filePath);
  61. } catch (Exception $e) {
  62. error_log("Error processing file " . $filePath . ": " . $e->getMessage());
  63. // Optionally, remove the failed file from the buffer.
  64. //unset($this->buffer[array_search($filePath, $this->buffer)]);
  65. }
  66. }
  67. $this->clearBuffer(); // Clear the buffer after processing
  68. return $processedFiles;
  69. }
  70. }
  71. /**
  72. * Configuration File Handling
  73. *
  74. * Reads configuration from a JSON file.
  75. *
  76. * @param string $configFile Path to the configuration file.
  77. * @return array|null An array of configuration settings, or null on error.
  78. */
  79. function getConfig(string $configFile): ?array
  80. {
  81. if (file_exists($configFile)) {
  82. $json = file_get_contents($configFile);
  83. $config = json_decode($json, true);
  84. if (json_last_error() === JSON_ERROR_NONE) {
  85. return $config;
  86. } else {
  87. error_log("Error decoding JSON from " . $configFile . ": " . json_last_error_msg());
  88. return null;
  89. }
  90. } else {
  91. error_log("Configuration file not found: " . $configFile);
  92. return null;
  93. }
  94. }
  95. // Example Usage (assuming config.json exists)
  96. $config = getConfig('config.json');
  97. if ($config) {
  98. $buffer = new FileBuffer($config);
  99. // Simulate adding files
  100. $buffer->addFile('/path/to/file1.txt');
  101. $buffer->addFile('/path/to/file2.csv');
  102. $buffer->addFile('/path/to/nonexistent_file.txt'); //Test error handling
  103. $processedFiles = $buffer->processFiles();
  104. print_r($processedFiles); //Output the processed file paths.
  105. } else {
  106. echo "Failed to load configuration. Exiting.\n";
  107. }
  108. ?>

Add your comment