Skip to content

Commit

Permalink
Adds support for WordPress environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
hasukmistry committed Apr 23, 2024
1 parent 297a8e5 commit 1526702
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function instance(): Plugin {
* Initialize the plugin.
*
* @since 1.0.0
* @since 1.0.1 Utilised set_wp_envs to set environment variables.
*
* @return void
*
Expand Down Expand Up @@ -73,6 +74,7 @@ function () {
$config_dir,
$config_file
)
->set_wp_envs()
->set_services();
}
);
Expand Down
73 changes: 64 additions & 9 deletions src/core/class-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ class Service {
*/
private static $instance;

/**
* Container instance.
*
* @since 1.0.1
*
* @var Container
*
* @access protected
*/
protected Container $container;

/**
* Service constructor.
*
* @since 1.0.1
*/
public function __construct() {
$this->container = Container::instance();
}

/**
* Get the service instance.
*
Expand Down Expand Up @@ -70,53 +90,88 @@ public function set_config( string $path, string $file ): Service {
* Register WordPress classes
*
* @since 1.0.0
* @since 1.0.1 Refactored container and container builder usage.
*
* @return Service
*/
public function set_services(): Service {
$container = Container::instance();

$services = apply_filters(
'wp_dev_load_services',
array()
);

foreach ( $services as $service ) {
// Call the __invoke() method of the class.
$container->get( $service )();
$this->container->get( $service )();
}

return $this;
}

/**
* Set environment variables from WordPress.
*
* @since 1.0.1 Added support for WordPress environment variables.
*
* @return Service
*/
public function set_wp_envs(): Service {
$builder = $this->container->get_container_builder();
$upload_dir = wp_upload_dir();

$builder->setParameter( 'wp_env(WP_ENVIRONMENT_TYPE)', WP_ENVIRONMENT_TYPE );

$builder->setParameter( 'wp_env(DB_NAME)', DB_NAME );
$builder->setParameter( 'wp_env(DB_USER)', DB_USER );
$builder->setParameter( 'wp_env(DB_PASSWORD)', DB_PASSWORD );
$builder->setParameter( 'wp_env(DB_HOST)', DB_HOST );
$builder->setParameter( 'wp_env(DB_CHARSET)', DB_CHARSET );
$builder->setParameter( 'wp_env(DB_COLLATE)', DB_COLLATE );

$builder->setParameter( 'wp_env(WP_CONTENT_DIR)', WP_CONTENT_DIR );
$builder->setParameter( 'wp_env(WP_CONTENT_URL)', content_url() );

$builder->setParameter( 'wp_env(WP_PLUGIN_DIR)', WP_PLUGIN_DIR );
$builder->setParameter( 'wp_env(WP_PLUGIN_URL)', plugins_url() );

$builder->setParameter( 'wp_env(WP_UPLOAD_DIR)', $upload_dir['basedir'] );
$builder->setParameter( 'wp_env(WP_UPLOAD_URL)', $upload_dir['baseurl'] );

$builder->setParameter( 'wp_env(WP_SITE_URL)', site_url() );
$builder->setParameter( 'wp_env(WP_HOME_URL)', home_url() );

return $this;
}

/**
* Register default config service.
*
* Keep in mind that,
* ContainerBuilder is available without loaded config file.
*
* @since 1.0.0
* @since 1.0.1 Refactored container and container builder usage.
*
* @param string $path The plugin path.
*
* @return ConfigInterface
*/
private function register_config( string $path ): ConfigInterface {
$container = Container::instance();
$builder = $this->container->get_container_builder();

$container
$this->container
->register( 'fileLocator', FileLocator::class )
->addArgument( $path );

$container
$this->container
->register( 'yamlFileLoader', YamlFileLoader::class )
->addArgument( $container->get_container_builder() )
->addArgument( $builder )
->addArgument( new Reference( 'fileLocator' ) );

$container
$this->container
->register( 'config', Config::class )
->addArgument( new Reference( 'yamlFileLoader' ) );

return $container->get( 'config' );
return $this->container->get( 'config' );
}
}

0 comments on commit 1526702

Please sign in to comment.