Skip to content

Commit

Permalink
App is a singleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
svandragt committed Aug 23, 2020
1 parent 83bf54b commit e80c245
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 22 deletions.
6 changes: 4 additions & 2 deletions decisionlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ B) Everyone contributes.

# Decision Record

- 001 Developers run Cuttlefish via the included PHP webserver. Supports simplicity.
- 002 Support static and dynamic deployments.
Decision IDs are auto incremented. IDs not in the document no longer apply.

- 003 Cuttlefish only generates static websites.
- 004 A content project consists of content assets and a php configuration file. All other code is in this project.
18 changes: 18 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class App
public $Cache;
public $Environment;

private static $instance = null;

public function __construct()
{
$this->Cache = new Cache();
Expand Down Expand Up @@ -40,4 +42,20 @@ public function run(array $routes)
new Router($routes);
$this->Cache->end();
}


/**
* Singleton. The object is created from within the class itself
* only if the class has no instance.
*
* @return void
*/
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new App();
}

return self::$instance;
}
}
3 changes: 1 addition & 2 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public function hasExistingCachefile()
*/
public function clear()
{
global $App;
$dir = $this->getCacheFolder();
$output = sprintf('Removing all files in %s<br>', $dir);
$Files = new Files(array( 'path' => $dir ));
Expand All @@ -157,7 +156,7 @@ public function clear()
foreach ($dirs as $dir) {
Filesystem::removeDirs(realpath($dir . '/.'));
}
$App->Environment->writeHtaccess();
App::getInstance()->Environment->writeAccess();

return (string) $output;
}
Expand Down
12 changes: 6 additions & 6 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public function relative(): self
return $this;
}

public function render(): void
{
$mime = $this->mime;
header("Content-Type: $mime");
readfile($this->path);
}
public function render(): void
{
$mime = $this->mime;
header("Content-Type: $mime");
readfile($this->path);
}
}
10 changes: 5 additions & 5 deletions src/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ protected function collect(string $dir = ".", $filter = null): array
return $files;
}

public function limit(int $max): array
{
$this->files = array_slice($this->files, 0, $max);
public function limit(int $max): array
{
$this->files = array_slice($this->files, 0, $max);

return $this->files;
}
return $this->files;
}

public function removeAll(): string
{
Expand Down
1 change: 1 addition & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct($routes)
if (isset($routes[$args[1]])) {
$controller_class = $routes[$args[1]];
}

$controller_arguments = array_slice($args, 2);
if (class_exists($controller_class, true)) {
$this->Controller = new $controller_class($this, $controller_arguments);
Expand Down
10 changes: 5 additions & 5 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public function __construct($file, $args = array())
$this->args = $args;
}

public function __get($name)
{
return $this->args[ $name ];
}
public function __get($name)
{
return $this->args[ $name ];
}

public function render(): void
{
$path = BASE_FILEPATH . trim(theme_dir(),'/') . "/views" . DIRECTORY_SEPARATOR . $this->file;
$path = BASE_FILEPATH . trim(theme_dir(), '/') . "/views" . DIRECTORY_SEPARATOR . $this->file;
require $path;
}
}
5 changes: 3 additions & 2 deletions src/iPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Cuttlefish;

interface iPlugin {
interface IPlugin
{
public function load($pluginConfiguration);
public function run();
public function unload();
}
}

0 comments on commit e80c245

Please sign in to comment.