Download the PHP package ricorocks-digital-agency/morpher without Composer

On this page you can find all versions of the php package ricorocks-digital-agency/morpher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package morpher

Morpher

Tests

We've all been there. You have an application in production, and now one of the database tables needs a structural change. You can't manually go in and change all the affected database rows. So what do you do? Put the logic in a migration? Seems a little risky, no?

Morpher is a Laravel package that provides a unified pattern of transforming data between database migrations. It allows you to keep your migration logic clean and terse and move responsibility for data manipulation to a more appropriate location. It also provides a robust way to write tests for these transformations, which otherwise proves to be a real challenge.

TOC

Installation

It's not required, but you might want to publish the config file:

Create your first Morph

Let's set up an example scenario. Your users table has a single name column, but you now need to separate it out into first_name and last_name columns. Your application has been live for a little while, so there is going to be a need to perform a data transformation.

You start by creating a migration:

That migration's up method might look something like this:

So, how do we go about taking all of the existing names, preparing the data, and inserting it into our new table?

Let's start by creating out first Morph:

This will create a new class in database/morphs called SplitUserNames. Our next step is to link our migration to our new Morph. We can do this using the $migration property in the morph class:

If you need more complex logic, you can instead override the migration method and return a migration class name that way.

:zap: Working with anonymous migrations? You can instead use the filename of the migration as the value of the $migration property. For example: protected static $migration = "2021_05_01_000000_create_some_anonymous_table";

Our next task is to describe our Morph. In the app/Morphs/SplitUserNames class, we need to do the following:

  1. Retrieve the current names prior to the migration being run.
  2. Split the names into first and last names.
  3. Insert the names after the migration has finished.

To accomplish this, our Morph might look as follows:

Now, when we run php artisan migrate, this Morph will run automatically.

Lifecycle

It helps to understand the lifecycle that a Morph goes through in order to make full use it.

When a Morph is linked to a migration, and that migration's up method is run (usually from migrating the database), the following happens (in order):

  1. The prepare method will be called on the Morph class. You can do anything you need to prepare data here.
  2. The migration will run.
  3. The canRun method will be called on the Morph class. Returning false in this method will stop the process here.
  4. The run method will be called on the Morph class. This is where you should perform your data transformations.

Testing Morphs

One of the biggest challenges presented by data morphing is writing feature tests. It becomes very tricky to insert data to test on prior to the morph taking place. And yet, automated tests are so important when the code you're running will be modifying real data. Morpher makes the process of testing data a breeze so that you no longer have to compromise.

To get started, we recommend creating a separate test case (or more than one test case) per Morph you'd like to write tests for. Add the TestsMorphs trait to that test class, and add the supportMorphs call to end of the setUp method.

:warning: The TestsMorphs trait conflicts with other database traits, such as RefreshDatabase or DatabaseTransactions. As such, ensure that your morph test cases are isolated (in separate test classes) from other tests in your suite.

With that done, you can get to work writing your tests! In order to do this, we provide a robust inspection API to facilitate Morph tests.

As you can see, there are several inspections methods we can make use of to fully test our Morphs. Note that you only need to use the inspections relevant to your particular Morph.

beforeThisMigration

This method is run prior to the migration connected to the Morph being run on the database. It is also run prior to the prepare method on your Morph being called. Seen as your tests won't have "old" data for your Morph to alter, you can use this method to create fake data ready for your Morph to use.

Note that in most cases, your Laravel Factories will likely be outdated, so you may have to resort to manual methods such as the DB Facade. You could also create a versioned Factory that uses the old data structure.

before

This method is executed prior to the run method being called on your Morph, but after the prepare method. You could use this as an opportunity to make sure your prepare method has collected the expected data and stored it on the Morph object, if your Morph needs to perform that step.

after

This method is executed after the run method has been called on your Morph. You should use this to check that the data migration has run successfully and that your data has actually been transformed.

Disabling Morphs

It may be helpful, particularly in local development where you destroy and rebuild the database regularly, to disable Morphs from running. To do this, add the following to your environment file:

Notes and Considerations


All versions of morpher with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
illuminate/support Version ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ricorocks-digital-agency/morpher contains the following files

Loading the files please wait ....