Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize abstracts & interfaces #6

Merged
merged 7 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ All notable changes to this project will be documented in this file. This projec
- Added [stellarwp/db](https://github.com/stellarwp/db) as a dependency.
- Swapped out direct `$wpdb` calls with the `DB` class.
- Added some tests for index checking on tables.
- Reorganized abstract classes and interfaces into `Contracts/` directories.
- Switched away from `StellarWP\Schema\Container` in favor of using `lucatume\DI52\App` and `lucatume\DI52\Container`.

## [1.0.0] 2022-08-17

Expand Down
27 changes: 18 additions & 9 deletions src/Schema/Activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace StellarWP\Schema;

use lucatume\DI52\App;
use lucatume\DI52\Container;
use StellarWP\Schema\Builder;

/**
Expand All @@ -31,9 +33,12 @@ class Activation {
* Handles the activation of the feature functions.
*
* @since 1.0.0
*
* @param Container $container The container.
*/
public static function activate() {
$schema_builder = Container::init()->make( Builder::class);
public static function activate( $container = null ) {
$container = $container ?: App::container();
$schema_builder = $container->make( Builder::class);
$schema_builder->up();
}

Expand All @@ -43,12 +48,14 @@ public static function activate() {
* This method will run once a day (using transients).
*
* @since 1.0.0
*
* @param Container $container The DI container.
*/
public static function init() {
public static function init( $container = null ) {
// Check if we ran recently.
$db_hash = get_transient( static::ACTIVATION_TRANSIENT );

$container = Container::init();
$container = $container ?: App::container();

$schema_builder = $container->make( Builder::class );
$hash = $schema_builder->get_registered_schemas_version_hash();
Expand All @@ -64,24 +71,26 @@ public static function init() {
$schema_builder->up();
}

if ( ! Container::init()->getVar( 'stellarwp_schema_fully_activated' ) ) {
if ( ! $container->getVar( 'stellarwp_schema_fully_activated' ) ) {
/**
* On new installations the full activation code will find an empty state and
* will have not activated at this point, do it now if required.
*/
Container::init()->register( Full_Activation_Provider::class );
$container->register( Full_Activation_Provider::class );
}
}

/**
* Handles the feature deactivation.
*
* @since 1.0.0
*
* @param Container $container The DI container.
*/
public static function deactivate() {
$services = Container::init();
public static function deactivate( $container = null ) {
$container = $container ?: App::container();

// @todo Should we drop the tables here, gracefully, if no data was generated?
$services->make( Builder::class )->clean();
$container->make( Builder::class )->clean();
}
}
12 changes: 7 additions & 5 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace StellarWP\Schema;

use StellarWP\Schema\Container;
use lucatume\DI52\App;
use lucatume\DI52\Container;
use StellarWP\Schema\Fields;
use StellarWP\Schema\Fields\Contracts\Schema_Interface as Field_Schema_Interface;
use StellarWP\Schema\StellarWP\DB\DB;
use StellarWP\Schema\StellarWP\DB\Database\Exceptions\DatabaseQueryException;
use StellarWP\Schema\Tables;
use StellarWP\Schema\Tables\Table_Schema_Interface;
use StellarWP\Schema\Tables\Contracts\Schema_Interface as Table_Schema_Interface;
use StellarWP\Schema\Tables\Filters\Group_FilterIterator;
use WP_CLI;

Expand All @@ -25,7 +27,7 @@ class Builder {
* @param Container $container Container instance.
*/
public function __construct( $container = null ) {
$this->container = $container ?: Container::init();
$this->container = $container ?: App::container();
}

/**
Expand Down Expand Up @@ -82,7 +84,7 @@ public function down() {
*
* @since 1.0.0
*
* @param \Iterator $table_schemas A list of Custom_Table_Interface objects that will have their tables dropped.
* @param \Iterator $table_schemas A list of Table_Schema_Interface objects that will have their tables dropped.
*/
$table_schemas = apply_filters( 'stellarwp_tables_to_drop', $table_schemas );

Expand Down Expand Up @@ -111,7 +113,7 @@ public function down() {
*
* @since 1.0.0
*
* @param \Iterator $field_classes A list of Custom_Field_Interface objects that will have their fields dropped.
* @param \Iterator $field_classes A list of Field_Schema_Interface objects that will have their fields dropped.
*/
$field_schemas = apply_filters( 'stellarwp_fields_to_drop', $field_schemas );

Expand Down
16 changes: 9 additions & 7 deletions src/Schema/Fields/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace StellarWP\Schema\Fields;

use StellarWP\Schema\Fields\Contracts\Schema_Interface;

class Collection implements \ArrayAccess, \Countable, \Iterator {
/**
* Table groups.
Expand All @@ -22,11 +24,11 @@ class Collection implements \ArrayAccess, \Countable, \Iterator {
*
* @since 1.0.0
*
* @param Field_Schema_Interface $field Field instance.
* @param Schema_Interface $field Field instance.
*
* @return mixed
*/
public function add( Field_Schema_Interface $field ) {
public function add( Schema_Interface $field ) {
$this->offsetSet( $field::get_schema_slug(), $field );

$this->register_group( $field );
Expand Down Expand Up @@ -55,9 +57,9 @@ public function current() {
*
* @param string $key Field slug.
*
* @return Field_Schema_Interface
* @return Schema_Interface
*/
public function get( string $key ): Field_Schema_Interface {
public function get( string $key ): Schema_Interface {
return $this->offsetGet( $key );
}

Expand Down Expand Up @@ -120,7 +122,7 @@ public function offsetUnset( $offset ): void {
/**
* Registers a group in the group array for the given table.
*
* @param Field_Schema_Interface $field Field instance.
* @param Schema_Interface $field Field instance.
*/
private function register_group( $field ) {
$group = $field->group_name();
Expand Down Expand Up @@ -154,11 +156,11 @@ public function rewind(): void {
* @since 1.0.0
*
* @param string $name Field name.
* @param Field_Schema_Interface $field Field instance.
* @param Schema_Interface $field Field instance.
*
* @return mixed
*/
public function set( $name, Field_Schema_Interface $field ) {
public function set( $name, Schema_Interface $field ) {
$this->offsetSet( $name, $field );

$this->register_group( $field );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
*
* @since 1.0.0
*
* @package StellarWP\Schema\Fields
* @package StellarWP\Schema\Fields\Contracts
*/

namespace StellarWP\Schema\Fields;
namespace StellarWP\Schema\Fields\Contracts;

use StellarWP\Schema\Container;
use lucatume\DI52\App;
use lucatume\DI52\Container;
use StellarWP\Schema\Schema;
use StellarWP\Schema\StellarWP\DB\DB;

/**
* Class Abstract_Field
* Class Field
*
* @since 1.0.0
*
* @package StellarWP\Schema\Fields
* @package StellarWP\Schema\Fields\Contracts
*/
abstract class Abstract_Field implements Field_Schema_Interface {
abstract class Field implements Schema_Interface {
/**
* @since 1.0.0
*
Expand Down Expand Up @@ -67,7 +68,7 @@ abstract class Abstract_Field implements Field_Schema_Interface {
* @param Container $container The container to use.
*/
public function __construct( $container = null ) {
$this->container = $container ?: Container::init();
$this->container = $container ?: App::container();
}

/**
Expand Down Expand Up @@ -133,7 +134,7 @@ public function drop() {
* @since 1.0.0
*
* @param string $schema_slug The schema slug.
* @param Field_Schema_Interface $field_schema The field schema to be dropped.
* @param Schema_Interface $field_schema The field schema to be dropped.
*/
do_action( 'stellarwp_pre_drop_field', $schema_slug, $this );

Expand All @@ -148,7 +149,7 @@ public function drop() {
* @since 1.0.0
*
* @param string $schema_slug The schema slug.
* @param Field_Schema_Interface $field_schema The field schema to be dropped.
* @param Schema_Interface $field_schema The field schema to be dropped.
*/
do_action( 'stellarwp_post_drop_field', $schema_slug, $this );

Expand All @@ -160,7 +161,7 @@ public function drop() {
* @since 1.0.0
*
* @param string $schema_slug The schema slug.
* @param Field_Schema_Interface $field_schema The field schema to be dropped.
* @param Schema_Interface $field_schema The field schema to be dropped.
*/
do_action( 'stellarwp_post_drop_field_table_version_sync', $schema_slug, $this );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
*
* @since 1.0.0
*
* @package StellarWP\Schema\Fields
* @package StellarWP\Schema\Fields\Contracts
*/
namespace StellarWP\Schema\Fields;
namespace StellarWP\Schema\Fields\Contracts;

use StellarWP\Schema\Tables\Abstract_Table;
use StellarWP\Schema\Tables\Contracts\Table;

/**
* Interface Custom_Field_Interface
*
* @since 1.0.0
*
* @package StellarWP\Schema\Fields
* @package StellarWP\Schema\Fields\Contracts
*/
interface Field_Schema_Interface {
interface Schema_Interface {
/**
* Allows extending classes that require it to run some methods
* immediately after the table creation or update.
Expand Down Expand Up @@ -75,7 +75,7 @@ public static function group_name();
*
* @since 1.0.0
*
* @return Abstract_Table|null
* @return Table|null
*/
public function table_schema();
}
12 changes: 6 additions & 6 deletions src/Schema/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Register {
*
* @param string $field Field class.
*
* @return Fields\Field_Schema_Interface
* @return Fields\Contracts\Schema_Interface
*/
public static function field( $field ) {
Schema::init();
Expand Down Expand Up @@ -60,9 +60,9 @@ public static function fields( array $fields ) {
*
* @since 1.0.0
*
* @param string|Fields\Field_Schema_Interface $field Field Schema class.
* @param string|Fields\Contracts\Schema_Interface $field Field Schema class.
*
* @return Fields\Field_Schema_Interface
* @return Fields\Contracts\Schema_Interface
*/
public static function remove_field( $field ) {
Schema::init();
Expand All @@ -86,9 +86,9 @@ public static function remove_field( $field ) {
*
* @since 1.0.0
*
* @param string|Tables\Table_Schema_Interface $table Table Schema class.
* @param string|Tables\Contracts\Schema_Interface $table Table Schema class.
*
* @return Tables\Table_Schema_Interface
* @return Tables\Contracts\Schema_Interface
*/
public static function remove_table( $table ) {
Schema::init();
Expand All @@ -114,7 +114,7 @@ public static function remove_table( $table ) {
*
* @param string $table Table class.
*
* @return Tables\Table_Schema_Interface
* @return Tables\Contracts\Schema_Interface
*/
public static function table( $table ) {
Schema::init();
Expand Down
Loading