Coder Social home page Coder Social logo

leepeterson / mixtape Goto Github PK

View Code? Open in Web Editor NEW

This project forked from automattic/mixtape

0.0 2.0 0.0 342 KB

Model, Data Store, Data Transfer Object and REST API Controller Library for WordPress

License: GNU General Public License v2.0

JavaScript 3.42% PHP 92.46% Shell 2.83% Python 1.30%

mixtape's Introduction

Mixtape

Model, Data Store, Data Transfer Object and REST API Controller Library for WordPress

Build Status

Features

Easy-to-use Fluent Interface

Provides a DSL-Like way of defining Models, Controllers, Etc.(see https://martinfowler.com/bliki/FluentInterface.html)

Defining a Model's Fields

<?php
public function declare_fields( $d ) {
			return array(
				$d->field( 'id' )
					->with_map_from( 'ID' )
					->with_type( $d->type( 'uint' ) )
					->with_description( 'Unique identifier for the object.' ),

				$d->field( 'title', 'The casette title.' )
					->with_map_from( 'post_title' )
					->with_type( $d->type( 'string' ) )
					->with_required(),

				$d->field( 'author', __( 'The author identifier.', 'casette' ) )
					->with_map_from( 'post_author' )
					->with_type( $d->type( 'uint' ) )
					->with_validations( 'validate_author' )
					->with_default( 0 )
					->with_dto_name( 'authorID' ),

				$d->field( 'status', 'The casette status.' )
					->with_type( $d->type( 'string' ) )
					->with_validations( 'validate_status' )
					->with_default( 'draft' )
					->with_map_from( 'post_status' ),

				$d->field( 'ratings', 'The casette ratings' )
					->derived( array( $this, 'get_ratings' ) )
					->with_dto_name( 'the_ratings' ),

				$d->field( 'songs', 'The casette songs', 'meta' )
					->with_map_from( '_casette_song_ids' )
					->with_type( $d->type( 'array' ) )
					->with_deserializer( array( $this, 'song_before_return' ) )
					->with_serializer( array( $this, 'song_before_save' ) )
					->with_dto_name( 'song_ids' ),
			);
	}

Setting Up a new Plugin's Environment

static function register( $bootstrap ) {
		$env = $bootstrap->environment();
		$cpt_data_store = $env->data_store()
			->with_class( 'MT_Data_Store_CustomPostType' )
			->with_args( array(
				'post_type' => 'mixtape_cassette',
			) );

		$env->define_model( 'Casette' )
			->with_data_store( $cpt_data_store );

		$env->define_model( 'CasetteSettings' )
			->with_data_store( $env->data_store()->with_class( 'MT_Data_Store_Option' ) );

		$rest_api = $env->rest_api( 'mixtape-example/v1' );

		$rest_api->endpoint()
			->with_base( '/casettes' )
			->with_class( 'MT_Controller_CRUD' )
			->for_model( $env->model( 'Casette' ) );

		$rest_api->endpoint()
			->with_class( 'CasetteApiEndpointVersion' );

		$rest_api->endpoint()
			->with_base( '/settings' )
			->with_class( 'MT_Controller_Settings' )
			->for_model( $env->model( 'CasetteSettings' ) );

		$env->auto_start();
	}
  • Reusable (Delegation, Composition, DI, SOLID)
  • Testable (A side effect of dependency injection, objects can be swapped/stubbed/mocked)
  • Generates prefixed classes of the library to avoid plugin conflicts (remixing)

Starting a new Project

Mixtape is meant to be used with a unique custom class prefix per project. There is a script for creating a new project with a custom prefix. You can run it like so.

/scripts/new_project.sh Custom_Prefix ./../plugin-name/lib/custom_prefix

The above will rename all mixtape classes: e.g. Mixtape_Bootstrap -> Custom_Prefix_Bootstrap

Testing

Mixtape has a PHPUnit test suite. It can be setup like this (look into the script for possible options you can override such as db pass)

./tests/bin/install.sh
./vendor/bin/phpunit

You can also start a watcher script for fast TDD cycles

./tests/bin/watch.py

You will need Python 3 and pip (for installing dependencies).

mixtape's People

Contributors

pgk avatar

Watchers

James Cloos avatar Lee Peterson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.