Skip to content

Instantly share code, notes, and snippets.

@kierzniak
Last active September 22, 2022 15:23
Show Gist options
  • Save kierzniak/acf82c20ed454b10da9c174d1c6a2ec3 to your computer and use it in GitHub Desktop.
Save kierzniak/acf82c20ed454b10da9c174d1c6a2ec3 to your computer and use it in GitHub Desktop.
Simple dependency injection example in WordPress plugin/theme code
<?php
/**
* Plugin bootsrap file.
*
* @author Motivast motivast.com
* @copyright 2018 - present, Motivast
*
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL-2.0-or-later
*
* @link https://gist.github.com/kierzniak/acf82c20ed454b10da9c174d1c6a2ec3
*/
require_once plugin_dir_path( __FILE__ ) . 'class-init.php';
$init = new Motivast\Init();
$init->load_dependencies();
$init->run();
<?php
namespace Motivast;
/**
* The core plugin class.
*
* This is used to load dependecies and run plugin.
*
* @author Motivast motivast.com
* @copyright 2018 - present, Motivast
*
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL-2.0-or-later
*
* @link https://gist.github.com/kierzniak/acf82c20ed454b10da9c174d1c6a2ec3
*/
class Init {
/**
* The object responsible for displaying shortcode
*
* @var Shortcode
*/
private $shortcode;
/**
* The object responsible for displaying widget
*
* @var Widget
*/
private $widget;
/**
* Load the required dependencies.
*
* @return void
*/
public function load_dependencies() {
require_once plugin_dir_path( __FILE__ ) . 'class-repository.php';
require_once plugin_dir_path( __FILE__ ) . 'class-shortcode.php';
require_once plugin_dir_path( __FILE__ ) . 'class-widget.php';
$repository = new Repository();
$this->shortcode = new Shortcode( $repository );
$this->widget = new Widget( $repository );
}
/**
* Run the loader to execute all of the hooks with WordPress.
*
* @return void
*/
public function run() {
$this->shortcode->run();
$this->widget->run();
}
}
<?php
namespace Motivast;
/**
* The class responsible for retrieving data.
*
* @author Motivast motivast.com
* @copyright 2018 - present, Motivast
*
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL-2.0-or-later
*
* @link https://gist.github.com/kierzniak/acf82c20ed454b10da9c174d1c6a2ec3
*/
class Repository {
/**
* Get the data.
*
* @return array
*/
private function get_data() {
// Get and return data
return array();
}
}
<?php
namespace Motivast;
/**
* The class responsible for displaying shortcode.
*
* @author Motivast motivast.com
* @copyright 2018 - present, Motivast
*
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL-2.0-or-later
*
* @link https://gist.github.com/kierzniak/acf82c20ed454b10da9c174d1c6a2ec3
*/
class Shortcode {
/**
* The object responsible for retrieving data
*
* @var Repository
*/
private $repository;
/**
* Class constructor
*/
public function __constructor( $repository ) {
$this->repository = $repository;
}
/**
* Display shortcode
*
* @return string
*/
public function display() {
// This is how you will get data from repository
$data = $this->repository->get_data();
}
/**
* Register the filters and actions related to shortcode.
*
* @return void
*/
public function run() {
// Add hooks to register and display shortcode
add_shortcode( 'shortcode', array( $this, 'display' ) );
}
}
<?php
namespace Motivast;
/**
* The class responsible for displaying widget.
*
* @author Motivast motivast.com
* @copyright 2018 - present, Motivast
*
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL-2.0-or-later
*
* @link https://gist.github.com/kierzniak/acf82c20ed454b10da9c174d1c6a2ec3
*/
class Widget {
/**
* The object responsible for retrieving data
*
* @var Repository
*/
private $repository;
/**
* Class constructor
*/
public function __constructor( $repository ) {
$this->repository = $repository;
}
/**
* Display widget
*
* @return string
*/
public function display() {
// This is how you will get data from repository
$data = $this->repository->get_data();
}
/**
* Register the filters and actions related to widget.
*
* @return void
*/
public function run() {
// Add hooks to register and display widget
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment