diff --git a/src/Changelog.php b/src/Changelog.php new file mode 100644 index 0000000..c4bbd14 --- /dev/null +++ b/src/Changelog.php @@ -0,0 +1,83 @@ +monitoredPath = $monitoredPath; + } + + public function capture(): void + { + $this->registerFiles($this->monitoredPath); + } + + public function makeDiff(?string $monitoredPath = null): void + { + if (!$monitoredPath) { + $monitoredPath = $this->monitoredPath; + } + $previous = $this->monitoredFiles; + $this->registerFiles($monitoredPath); + + foreach ($this->monitoredFiles as $index => $file) { + if (!array_key_exists($index, $previous)) { + $this->newFiles[] = $file; + continue; + } + + if ($previous[$index]['md5'] != $file['md5']) { + $this->changedFiles[] = $file; + } + } + } + + public function hasNewFiles(): bool + { + return count($this->newFiles) > 0; + } + + public function hasChangedFiles(): bool + { + return count($this->changedFiles) > 0; + } + + public function hasChanges(): bool + { + return $this->hasNewFiles() || $this->hasChangedFiles(); + } + + public function getChangesSummary(): string + { + if ($this->hasChanges()) { + return sprintf("%s files added and %s files changed.", + count($this->newFiles), + count($this->changedFiles) + ); + } + + return "No changes."; + } + + public function registerFiles(string $monitoredPath): void + { + foreach (glob($monitoredPath . '/*') as $filename) { + if (is_dir($filename)) { + $this->registerFiles($filename); + } + + $index = md5($filename); + $this->monitoredFiles[$index] = [ + 'path' => $filename, + 'isDir' => is_dir($filename) ? "yes" : "no", + 'md5' => is_dir($filename) ? $index : md5_file($filename), + ]; + } + } +} diff --git a/src/Service/AutodocsService.php b/src/Service/AutodocsService.php index 18cf62b..d0d0c0b 100644 --- a/src/Service/AutodocsService.php +++ b/src/Service/AutodocsService.php @@ -41,6 +41,13 @@ public function load(App $app): void } } } + + /** + * Registers Json data feeds from cache dir + * @param string $identifier + * @param JsonDataFeed $dataFeed + * @return void + */ protected function registerDataFeed(string $identifier, JsonDataFeed $dataFeed): void { $this->dataFeeds[$identifier] = $dataFeed; @@ -73,15 +80,15 @@ public function registerPage(ReferencePageInterface $page): void public function buildPages(string $pages="all", array $parameters = []): void { $buildPages = explode(",", $pages); + /** @var ReferencePageInterface $referencePage */ foreach ($this->referencePages as $referencePage) { if ($pages === "all" || in_array($referencePage->getName(), $buildPages)) { $referencePage->loadData($parameters); $savePath = $this->config['output'] . '/' . $referencePage->getSavePath(); - if (!is_dir(dirname($savePath))) { - mkdir(dirname($savePath), 0755, true); + if(!$this->storage->hasDir(dirname($savePath))) { + $this->storage->createDir(dirname($savePath)); } - $this->storage->saveFile($savePath, $this->builder->buildPage($referencePage)); } } diff --git a/src/Storage/FileStorage.php b/src/Storage/FileStorage.php index f63170b..d421938 100644 --- a/src/Storage/FileStorage.php +++ b/src/Storage/FileStorage.php @@ -4,6 +4,18 @@ class FileStorage implements StorageInterface { + public function createDir(string $path): void + { + if (!is_dir($path)) { + mkdir($path, 0755, true); + } + } + + public function hasDir(string $path): bool + { + return is_dir($path); + } + public function saveFile(string $path, string $content): void { file_put_contents($path, $content); diff --git a/src/Storage/StorageInterface.php b/src/Storage/StorageInterface.php index 701bb35..6058702 100644 --- a/src/Storage/StorageInterface.php +++ b/src/Storage/StorageInterface.php @@ -4,5 +4,9 @@ interface StorageInterface { + public function hasDir(string $path): bool; + + public function createDir(string $path): void; + public function saveFile(string $path, string $content): void; } \ No newline at end of file