Skip to main content

Application

Keeping it all together

Application Container

An IoC container managing the entire application.

Introduction

In simple terms: IoC, or inversion of control, is a design pattern in which you have a central system for managing dependencies via dependency injection. This means you don't have to manage a large "construction chain" of dependencies, but you can have each part request another, at runtime, dynamically.

Morningmedley extends the Illuminate container and, mostly, implements the Laravel Application Contract.

note

There are a few features of a Laravel application that are not properly supported.

Usage

To construct the Application Container simply follow the Getting Started.

To access the application object at runtime you can use the global function app() If you call app() without args it will return the current Application instance. With args it is a wrapper for Application::make()

// These two do the same thing
$fooInstance = app()->make(Foo::class);
$fooInstance = app(Foo::class);

On this object you have access to every service and provider, the config object, caching and more.

Environment type

There are two wrapper functions on the app for wp_environment_type() to check if the app is running on a local or production setup. This is used for determining whether to cache files or not.

$app = app();
$app->isProduction();
$app->isLocal();

The IoC part!

If you need to access a class that has been bound by the application or change a binding you can do so as well. The app() function also wraps make and makeWith.

// These two are identical
app()->make('filecachemanager')->clearCache();
app('filecachemanager')->clearCache();

// These are too!
app()->makeWith(MyClass::class, ['foo' => 'bar']);
app(MyClass::class, ['foo' => 'bar']);

Caching

Illuminate Cache is available for use with file, array, database and our custom transient stores available, where file is default.

WP Context

When Morningmedley boots it also creates an object representing the current theme or plugin. Through this class you can get the name, text-domain, version, URL and more. This way you can use these without needing to know whether your module is installed in a theme or plugin.

note

Some parts use this behind the scenes such as asset() and our enqueueing helpers.

Example

Here is an example for enqueueing a stylesheet:

/app/Hooks/EnqueueExample.php
#[Action('wp_enqueue_scripts')]
public function enqueueStylesheet(){
\enqueue_style('my-style', "/build/style.css");
}

Helpers

All of Illuminate/Foundation helper methods are loaded, but not all are properly implemented (yet).

Some noteworthy ones are:

app()

Get the Application instance or ::make something

$app = app();
$foo = app(Foo::class);

config()

You can access the configuration by using the global config() function.

$configRepo = config();     // Get the entire repository object
config('foo'); // Get the value of 'foo'. Returns null if not found
config('foo', 'bar'); // Get the value of 'foo'. Returns 'bar' if not found
config(['foo' => 'bar']); // Set the value of 'foo' to 'bar'
config('app.providers'); // You can use dot notation

view()

There are two wrapper functions for the View module.

use Illuminate\Support\Facades\View;

View::composer('post-card',function($view){
$view->with('post', \get_the_post($view['postId'] ?? null));
});

echo view('post-card', ['postId' => 1]);

base_path()

You can get the base path of your project using base_path(string $path = ''). This will return the absolute path of your project.

base_path();                     // .../wp-content/themes/my-theme
base_path('storage/some.file'); // .../wp-content/themes/my-theme/storage/some.file

asset()

Get the url of an asset.

asset('public/images/placeholder.png')

enqueue_*() and register_*

Enqueue or register a script or a stylesheet. This uses asset() for the URL and automatically looks for a *.asset.php file for scripts.

register_script('main-script', 'build/js/main.js')

cache()

Get the cache class or get or set a cache value from the default cache.

cache('someKey');                 // Get cached value
cache('someKey','default'); // Get cached value - with a fallback value
cache(['someKey','someValue']); // Set someKeys value as someValue

storage_path()

Get the absolute path to the storage dir