It begins with running bin/magento sampledata:deploy from your magento root install directory.

Some magic then happens…

Hold on, I get an error deploying the sample data similar to:

PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 88614883 bytes)

No problem! This can sometimes happen. Magento uses a lot of memory when installing the sample data. You can remedy this by increasing the available memory temporarily whilst installing the sample data by running the above command with some additional PHP;

php -d memory_limit=2G bin/magento sampledata:deploy

Okay. So what is this magic? Well we can start to understand this by looking inside the class Magento\SampleData\Model\Dependency within the SampleData module. More specifically in the getSampleDataPackages() function.

It searches the composer.lock file and module composer.json files for “suggest”ed modules. Suggested modules are just nodes within the composer.json file. They look like;

{
  "suggest": {
    "magento/module-catalog-sample-data": "Sample Data version:100.0.*"
  }
}

The above is found in the Catalog module’s composer.json file. Similar declarations can be found in most other modules too.

The important bit is “Sample Data version:” defined as a constant within the Dependancy class (const SAMPLE_DATA_SUGGEST = 'Sample Data version:';). Magento looks for this string (if (strpos($version, self::SAMPLE_DATA_SUGGEST) === 0) {) and if it finds it, like in the json above, it extracts the version number (substr($version, strlen(self::SAMPLE_DATA_SUGGEST)) - which in the above case returns 100.0.*) and adds it as a dependency on the project like so;

{
  "require": {
    "magento/module-catalog-sample-data": "100.0.*"
  }
}

All this is basically a wrapper for typing composer require magento/module-catalog-sample-data:100.0.*"

It does the above process for each of the sample data modules it finds. As it does this, the sample data modules are added to the projects main composer.json file and downloaded.

Once that is done, it will update the composer.lock file and let you know it is finished.

Don’t forget, to finish the installation you need to run the standard setup:upgrade command

bin/magento setup:upgrade

The above actually installs the data downloaded using the sample data modules to the database.

Load up your Magento admin and check out some of the content.

With the above in mind it shouldn’t be too difficult to roll our own sample data module :)

  • magento2

Like this post? Share it :)


Related Posts

Back