Refactoring and cleanup
* Moved logic from entrypoint to a dedicated class. * Disabled automatic flush of state. * Added locking support to prevent multiple instances. * Added logging * Added base interface for CredentialsLoader
This commit is contained in:
		
							
								
								
									
										36
									
								
								src/State/Lockfile.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/State/Lockfile.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace NoccyLabs\FreshDocker\State;
 | 
			
		||||
 | 
			
		||||
class Lockfile
 | 
			
		||||
{
 | 
			
		||||
    private string $filename;
 | 
			
		||||
 | 
			
		||||
    private int $maxLock = 3600;
 | 
			
		||||
 | 
			
		||||
    public function __construct(string $filename)
 | 
			
		||||
    {
 | 
			
		||||
        $this->filename = $filename;
 | 
			
		||||
        register_shutdown_function([$this,"release"]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function lock()
 | 
			
		||||
    {
 | 
			
		||||
        if (file_exists($this->filename)) {
 | 
			
		||||
            if (time() - filemtime($this->filename) < $this-$maxLock) {
 | 
			
		||||
                throw new \RuntimeException("Lockfile {$this->filename} already exists");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        touch($this->filename);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function release()
 | 
			
		||||
    {
 | 
			
		||||
        if (file_exists($this->filename)) {
 | 
			
		||||
            unlink($this->filename);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								src/State/Log.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/State/Log.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace NoccyLabs\FreshDocker\State;
 | 
			
		||||
 | 
			
		||||
class Log
 | 
			
		||||
{
 | 
			
		||||
    private bool $verbose = false;
 | 
			
		||||
 | 
			
		||||
    private array $log = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct(bool $verbose=false)
 | 
			
		||||
    {
 | 
			
		||||
        $this->verbose = $verbose;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setVerbose(bool $verbose)
 | 
			
		||||
    {
 | 
			
		||||
        $this->verbose = $verbose;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function append(string $message)
 | 
			
		||||
    {
 | 
			
		||||
        array_push($this->log, $message);
 | 
			
		||||
        if ($this->verbose) {
 | 
			
		||||
            fwrite(STDOUT, $message . "\n");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function flush()
 | 
			
		||||
    {
 | 
			
		||||
        if ($this->verbose) return;
 | 
			
		||||
        fwrite(STDOUT, join("\n", $this->log)."\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function asString(): string
 | 
			
		||||
    {
 | 
			
		||||
        return join("\n", $this->log);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -15,15 +15,17 @@ class PersistentState
 | 
			
		||||
    public function __construct(string $filename)
 | 
			
		||||
    {
 | 
			
		||||
        $this->filename = $filename;
 | 
			
		||||
        register_shutdown_function([$this,"flush"]);
 | 
			
		||||
        // register_shutdown_function([$this,"flush"]);
 | 
			
		||||
        if (file_exists($filename)) {
 | 
			
		||||
            $this->state = Yaml::parseFile($filename);
 | 
			
		||||
            $this->state = Yaml::parseFile($filename) ?? [];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function flush()
 | 
			
		||||
    {
 | 
			
		||||
        if (!$this->dirty) return;
 | 
			
		||||
        $this->dirty = false;
 | 
			
		||||
 | 
			
		||||
        $yaml = Yaml::dump($this->state);
 | 
			
		||||
        file_put_contents($this->filename."~", $yaml);
 | 
			
		||||
        rename($this->filename."~", $this->filename);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user