Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Soon/DataSync/docs/01_Getting_Started/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/corals/old/app/code/Soon/DataSync/docs/01_Getting_Started/02_Your_first_exports.md
# Your first exports

## Prepare your module

You must have created a module and have it working.
Covering this topic is out of the scope of this guide.

## Declare your job

Edit the `di.xml` file of your module in order to add your job to the 
DataSync's job stack:

```xml
<type name="Soon\DataSync\Model\JobStack">
    <arguments>
        <argument name="jobs" xsi:type="array">
            <item name="my_job_code" xsi:type="array">
                <item name="active" xsi:type="string">1</item>
                <item name="sort_order" xsi:type="string">10</item>
                <item name="type" xsi:type="object">Namespace\Module\Model\Job\FirstExport</item>
            </item>
        </argument>
    </arguments>
</type>
```

Let's review this code.

* `<item name="your_job_code" xsi:type="array">`. Is obviously where you tell DataSync "here is my job, you can identify it with the code `my_job_code`.
* `<item name="active" xsi:type="string">1</item>`. Is your job active? Override this config from another module to enable/disable a job that is outside of your own module.
* `<item name="sort_order" xsi:type="string">10</item>`. Well, you can use this to set priorities for your jobs.
* `<item name="type" xsi:type="object">Namespace\Module\Model\Job\FirstExport</item>`. Is the job that DataSync's job stack will call to actually run the job.

## Code the export

Any DataSync job must extends the `\Soon\DataSync\Model\Job\Example::sync` method.
So, according to the DI that we declared above, you must create the following class: `class \Namespace\Module\Model\Job\FirstExport extends \Soon\DataSync\Model\Job\JobAbstract
`.

And you must declare your extension for the `sync()` method:

```php
class \Namespace\Module\Model\Job\FirstExport extends \Soon\DataSync\Model\Job\JobAbstract {

    protected function sync()
    {
        // To be implemented
    }
}
```

Now, let's tell to DataSync to use our prepared data to create a `data.csv` file in the `var/soon_datasync` directory of our Magento installation.
This is as simple as that:

```php
protected function sync()
{
    /** @var $this Sync */
    $this
        ->dataOfType('csv')
        ->to('local', ['filepath' => '~/var/soon_datasync/data.csv'])
        ->treatWith(function () { return ['first' => 'John', 'last' => 'Doe']; })
        ->go();
}
```
`->dataOfType('csv')`.
Tells DataSync that we want to use the "CSV" DataType. In other words, we want our raw data to be translitted to CSV.

`->to('local'...`.
Tells 3 things to DataSync: (1) we want to run an export (thus the use of `to()`), (2) we want this export to use the "local" TransferType, 
(3) the file to write is `~/var/soon_datasync/data.csv`.

`->treatWith(function () { return ['first' => 'John', 'last' => 'Doe']; })`. This is the Treatment. As we are making an export, we **must return an array** to DataSync who will use this array to build the CSV file.  

`->go()`. Actually tells DataSync to run the job. As soon as `go()` is called, the job is launched.
This means that all methods that are called before `go()` can be called in any order.

> Note the use of "~" in `filepath`. As you guessed, this means "Magento root dir" to DataSync. This allows you to specify relative and absolute paths.

## Run the job

Go to your terminal and cd into your Magento install directory.
Then just run:

`php bin/magento soon_datasync:run my_job_code`

This generates the following content in a CSV file:
 
```csv
// magento_root_dir/var/soon_datasync/data.csv
"first","last"
"John","Doe"
```

> DataSync automatically archives the data you exported or imported.
> It is stored in magento_root_dir/var/soon_datasync/_archive

## Use defaults!

Great, you have your first export! But you could have written it in a much lighter way... like this:

```php
protected function sync()
{
    /** @var $this Sync */
    $this
        ->to('local')
        ->treatWith(function () { return ['first' => 'John', 'last' => 'Doe']; })
        ->go();
}
```

* If you don't specify a DataType with `dataOfType()`, DataSync will use CSV.
* If you don't specify a `filepath` in your `to()` call, DataSync will use the `var/soon_datasync/data.[DataType extension]` relative to the most relevant root path.
If you are doing an export to a remote FTP server, DataSync will consider the root path as being the root dir once connected to FTP.

## The same in XML

It all melts down to writing this:

```php
protected function sync()
{
    /** @var $this Sync */
    $this
        ->dataOfType('xml')
        ->to('local')
        ->treatWith(function () { return ['first' => 'John', 'last' => 'Doe']; })
        ->go();
}
```

DataSync is clever enough to write the good file extension. So, once you have run `php bin/magento soon_datasync:run my_job_code`, you have this XML file:

```xml
// magento_root_dir/var/soon_datasync/data.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
    <first>John</first>
    <last>Doe</last>
</root>
```

## Learn by example

Get inspiration and ideas from the [Examples](03_Examples.md).


Spamworldpro Mini