Skip to content

Commit

Permalink
Adding Changelog feature
Browse files Browse the repository at this point in the history
  • Loading branch information
erikaheidi committed Sep 8, 2023
1 parent 6382a68 commit cbfed0f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
83 changes: 83 additions & 0 deletions src/Changelog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Autodocs;

class Changelog
{
public string $monitoredPath;
public array $monitoredFiles = [];
public array $changedFiles = [];
public array $newFiles = [];
public function __construct(string $monitoredPath)
{
$this->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),
];
}
}
}
13 changes: 10 additions & 3 deletions src/Service/AutodocsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit cbfed0f

Please sign in to comment.