Skip to content

Commit

Permalink
removed the Config class in favor of direct method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefelipe committed Jan 25, 2023
1 parent 05d6327 commit e759084
Show file tree
Hide file tree
Showing 20 changed files with 367 additions and 751 deletions.
135 changes: 107 additions & 28 deletions src/App/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@ class App extends Container
protected bool $is_tablet = false;
protected bool $is_desktop = false;

protected string $id;
protected string $name;
protected string $url;
protected string $timezone;

protected array $configs = [
'language',
'translation',
'multilanguage',
'app',
'cache',
'image',
'meta',
'wp',
'admin',
'admin_columns',
'html',
'api',
'sitemap',
'rss',
'view',
];
// we leave Language and Translation first, so the next ones already have the ability to translate strings


public function __construct()
{
// initialize the Container itself
Expand Down Expand Up @@ -81,11 +106,6 @@ protected function registerBaseBindings()
$this->delegate(new ReflectionContainer());
}

public function config(): Config
{
return $this->get('config');
}

public function view(): View
{
return $this->get('view');
Expand Down Expand Up @@ -132,8 +152,6 @@ public function bootstrap(string $base_path = null)
$this->setBasePath($base_path);
}

$this->config()->load($this->configPath());

// Save post/terms hook
$this->addSavePostHook();
$this->addDeletePostHook();
Expand All @@ -143,10 +161,31 @@ public function bootstrap(string $base_path = null)
// Map Links
$this->mapLinks();

// Config
$this->loadConfig();

// Calls the boot/bootAdmin method on all classes at the App folder
$this->bootAppFolder();
}

protected function loadConfig()
{
$path = app()->configPath();

if (!file_exists($path) || !is_dir($path)) {
return;
}

// load config files
foreach ($this->configs as $key) {
$filepath = $path . DIRECTORY_SEPARATOR . $key . '.php';

if (is_file($filepath)) {
require_once $filepath;
}
}
}

protected function bootAppFolder()
{
$path = $this->appPath();
Expand Down Expand Up @@ -226,19 +265,48 @@ protected function bootAppFolder()
}
}

public function id(): string
public function id(string $id = null): string
{
return $this->config()->app->id ??= $this->themeId();
if ($id) {
$this->$id = $id;
}
return $this->id ??= $this->themeId();
}

public function name(): string
public function name(string $name = null): string
{
return $this->config()->app->name ?: '';
if ($name) {
$this->$name = $name;
}
return $this->name ?? '';
}

public function url(): string
public function url(string $url = null): string
{
return $this->config()->app->url ??= \untrailingslashit(c('WP_HOME') ?: \get_site_url());
if ($url) {
$this->$url = $url;
}
return $this->url ??= \untrailingslashit(c('WP_HOME') ?: \get_site_url());
}

// use values from https://www.php.net/manual/en/timezones.php
public function timezone(string $timezone = null): string
{
if ($timezone) {
$this->$timezone = $timezone;
}
return $this->timezone ??= 'UTC';

// Note: CAN NOT use date_default_timezone_set here:

// found a major bug/feature on WordPress itself
// it seems to set the date_default_timezone_set to UTC itself
// so changing again here will create time to be wrong everywhere (ACF, etc)

// will read source WP code to understand more what to do
// may just remove, but will need doc to let users know

// date_default_timezone_set($this->timezone);
}

public function isFrontEnd(): bool
Expand Down Expand Up @@ -458,8 +526,8 @@ public function savePostHook($post_id, $post = null)
$this->removeSavePostHook();

// turn off cache
$original_state = $this->cache()->enabled();
$this->cache()->enabled(false);
$was_enabled = $this->cache()->isEnabled();
$this->cache()->disable();

// in case it's attachment, it misses the post object
if (!$post) {
Expand Down Expand Up @@ -489,7 +557,9 @@ public function savePostHook($post_id, $post = null)
// deleted, when deleted

// turn on posts cache
$this->cache()->enabled($original_state);
if ($was_enabled) {
$this->cache()->enable();
}

// re-add action
$this->addSavePostHook();
Expand Down Expand Up @@ -517,8 +587,8 @@ public function deletePostHook($post_id, $post = null)
$this->removeDeletePostHook();

// turn off cache
$original_state = $this->cache()->enabled();
$this->cache()->enabled(false);
$was_enabled = $this->cache()->isEnabled();
$this->cache()->disable();

// in case it's attachment, it misses the post object
if (!$post) {
Expand All @@ -539,7 +609,9 @@ public function deletePostHook($post_id, $post = null)
\do_action('Bond/delete_post/' . $post->post_type, Cast::post($post));
}
// turn on posts cache
$this->cache()->enabled($original_state);
if ($was_enabled) {
$this->cache()->enable();
}

// re-add action
$this->addDeletePostHook();
Expand All @@ -554,8 +626,8 @@ public function optionsSavePostHook($post_id)
}

// turn off cache
$original_state = $this->cache()->enabled();
$this->cache()->enabled(false);
$was_enabled = $this->cache()->isEnabled();
$this->cache()->disable();

// clear cache
$this->cache()->delete('options');
Expand All @@ -571,7 +643,9 @@ public function optionsSavePostHook($post_id)
}

// turn on posts cache
$this->cache()->enabled($original_state);
if ($was_enabled) {
$this->cache()->enable();
}
}


Expand All @@ -592,8 +666,8 @@ public function saveTermHook($term_id, $tt_id, $taxonomy)
$this->removeSaveTermHook();

// turn off cache
$original_state = $this->cache()->enabled();
$this->cache()->enabled(false);
$was_enabled = $this->cache()->isEnabled();
$this->cache()->disable();

// clear cache
$this->cache()->delete($taxonomy);
Expand All @@ -615,7 +689,10 @@ public function saveTermHook($term_id, $tt_id, $taxonomy)
// TODO save_term doesn't get the delete right?
// turn on posts cache
$this->cache()->enabled($original_state);
if ($was_enabled) {
$this->cache()->enable();
}


// re-add action
$this->addSaveTermHook();
Expand Down Expand Up @@ -645,8 +722,8 @@ public function saveUserHook($user_id)
$this->removeSaveUserHook();

// turn off cache
$original_state = $this->cache()->enabled();
$this->cache()->enabled(false);
$was_enabled = $this->cache()->isEnabled();
$this->cache()->disable();

// clear cache
$this->cache()->delete('bond/query');
Expand All @@ -662,7 +739,9 @@ public function saveUserHook($user_id)
}

// turn on posts cache
$this->cache()->enabled($original_state);
if ($was_enabled) {
$this->cache()->enable();
}

// re-add action
$this->addSaveUserHook();
Expand Down
Loading

0 comments on commit e759084

Please sign in to comment.