14 Commits
v2.0.0 ... v3.1

Author SHA1 Message Date
Jani Gyllenberg
734a6a3cbd Ability to export existing models as sync files 2022-09-07 12:32:39 -04:00
Jani Gyllenberg
a241493142 Adds support for config defined Model namespace 2022-09-07 12:31:56 -04:00
Victor Gonzalez
9980bc7e9a update to Testbench v7 & composer update 2022-05-17 07:02:05 -05:00
Jani Gyllenberg
9935cb78cb Merge pull request #32 from nullthoughts/dependabot/composer/league/flysystem-1.1.4
Bump league/flysystem from 1.1.3 to 1.1.4
2021-07-02 12:17:42 -04:00
Jani Gyllenberg
9ade30325c Merge pull request #33 from nullthoughts/github-actions-phpunit
Github Actions: PHPUnit
2021-07-02 12:16:48 -04:00
Ismo Vuorinen
effd3be10f composer update on php 7.4 2021-06-29 14:37:23 +03:00
412a03516b Github Actions: PHPUnit
Added Github Actions runner for PHPUnit tests.
It runs on every push and makes sure all contributions (be it from humans, or bots) pass our existing tests.
2021-06-29 13:49:20 +03:00
dependabot[bot]
730fe0e2ac Bump league/flysystem from 1.1.3 to 1.1.4
Bumps [league/flysystem](https://github.com/thephpleague/flysystem) from 1.1.3 to 1.1.4.
- [Release notes](https://github.com/thephpleague/flysystem/releases)
- [Changelog](https://github.com/thephpleague/flysystem/blob/2.x/CHANGELOG.md)
- [Commits](https://github.com/thephpleague/flysystem/compare/1.1.3...1.1.4)

---
updated-dependencies:
- dependency-name: league/flysystem
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2021-06-29 04:05:34 +00:00
Victor Gonzalez
f830c5b264 update to accept php 8 2021-06-23 09:41:47 -04:00
95635719d6 Merge pull request #30 from nullthoughts/dependabot/composer/laravel/framework-7.30.4
Bump laravel/framework from 7.2.0 to 7.30.4
2021-02-02 19:53:59 +02:00
dependabot[bot]
e57f989c68 Bump laravel/framework from 7.2.0 to 7.30.4
Bumps [laravel/framework](https://github.com/laravel/framework) from 7.2.0 to 7.30.4.
- [Release notes](https://github.com/laravel/framework/releases)
- [Changelog](https://github.com/laravel/framework/blob/8.x/CHANGELOG-6.x.md)
- [Commits](https://github.com/laravel/framework/compare/v7.2.0...v7.30.4)

Signed-off-by: dependabot[bot] <support@github.com>
2021-02-02 17:51:33 +00:00
3a54f48c65 Removed non-existing Feature tests 2021-02-02 19:42:03 +02:00
c0f21612fc Merge pull request #28 from nullthoughts/dependabot/composer/symfony/http-foundation-5.0.7
Bump symfony/http-foundation from 5.0.5 to 5.0.7
2020-04-06 08:59:44 +03:00
dependabot[bot]
8007b2ae88 Bump symfony/http-foundation from 5.0.5 to 5.0.7
Bumps [symfony/http-foundation](https://github.com/symfony/http-foundation) from 5.0.5 to 5.0.7.
- [Release notes](https://github.com/symfony/http-foundation/releases)
- [Changelog](https://github.com/symfony/http-foundation/blob/master/CHANGELOG.md)
- [Commits](https://github.com/symfony/http-foundation/compare/v5.0.5...v5.0.7)

Signed-off-by: dependabot[bot] <support@github.com>
2020-03-30 22:43:51 +00:00
9 changed files with 4494 additions and 1220 deletions

20
.github/workflows/phpunit.yml vendored Normal file
View File

@@ -0,0 +1,20 @@
name: PHPUnit
on: [push]
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: php-actions/composer@v5
- name: PHPUnit Tests
uses: php-actions/phpunit@v2
with:
php_extensions: xdebug mbstring
bootstrap: vendor/autoload.php
configuration: phpunit.xml
args: --coverage-text

View File

@@ -10,10 +10,10 @@
],
"require": {
"ext-json": "*",
"php": "^7.2"
"php": "^8.0"
},
"require-dev": {
"orchestra/testbench": "^5.0"
"orchestra/testbench": "^7.0"
},
"autoload": {
"psr-4" : {

5510
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,19 @@
<?php
return [
/**
* Namespace for Laravel Models
*/
'namespace' => '\\App\\Models\\',
/**
* Path to directory containing JSON files for synchronization
*/
'path' => base_path('sync'),
/**
* Array of Model names which controls the synchronization order
*/
'order' => [
//
],

View File

@@ -1,25 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false"
bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true"
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>

View File

@@ -0,0 +1,24 @@
<?php
namespace nullthoughts\LaravelDataSync\Console\Commands;
use nullthoughts\LaravelDataSync\Exporter;
use Illuminate\Console\Command;
class Export extends Command
{
protected $signature = 'data:export {model} {--criteria=*} {--except=*} {--only=*}';
protected $description = 'Export Model data for synchronization';
public function handle()
{
$model = $this->argument('model');
$this->info('Exporting ' . $model . ' model to sync data file');
(new Exporter($model, $this->option('criteria'), $this->option('except'), $this->option('only')))->run();
$this->comment('Export for data sync completed');
}
}

View File

@@ -17,6 +17,7 @@ class DataSyncBaseServiceProvider extends ServiceProvider
{
$this->commands([
Console\Commands\Sync::class,
Console\Commands\Export::class,
]);
}

96
src/Exporter.php Normal file
View File

@@ -0,0 +1,96 @@
<?php
namespace nullthoughts\LaravelDataSync;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
class Exporter
{
private $modelName;
private $criteria;
private $except;
private $only;
/**
* Get files in sync directory.
*
* @param string $model
* @param array $criteria
*
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
*/
public function __construct(string $modelName, array $criteria = [], $except = [], $only = [])
{
$this->modelName = $modelName;
$this->criteria = $criteria;
$this->except = $except;
$this->only = $only;
}
/**
* Export Model data to sync file.
*
* @return mixed
*/
public function run()
{
$class = config('data-sync.namespace', '\\App\\Models\\') . $this->modelName;
$filename = $this->getDirectory() . '/' . $this->modelName . '.json';
$data = $this->prepareData($class);
file_put_contents($filename, $data);
}
/**
* Get directory path for sync files.
*
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
*
* @return string
*/
protected function getDirectory()
{
$directory = config('data-sync.path', base_path('sync'));
if (! file_exists($directory)) {
throw new FileDirectoryNotFoundException();
}
return $directory;
}
protected function prepareData($class)
{
$class = new $class;
$keys = $this->prepareKeys($class);
return $class->select($keys)->get()->toJson();
}
protected function prepareKeys($class)
{
$keys = Schema::getColumnListing($class->getTable());
if ($this->only) {
$keys = array_intersect($keys, $this->only);
}
if ($this->except) {
$keys = array_diff($keys, $this->except);
}
return collect($keys)->map(function ($key) {
if (in_array($key, $this->criteria)) {
return $key . ' as _' . $key;
}
return $key;
})->toArray();
}
}

View File

@@ -10,7 +10,6 @@ use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
use nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
use nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException;
use nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException;
use stdClass;
class Updater
{
@@ -37,7 +36,7 @@ class Updater
/**
* @var string
*/
private $baseNamespace = '\\App\\';
private $modelNamespace;
/**
* Get files in sync directory.
@@ -54,6 +53,7 @@ class Updater
{
$this->remote = $remote;
$this->disk = $disk;
$this->modelNamespace = config('data-sync.namespace', '\\App\\Models\\');
$this->directory = $this->getDirectory($path);
$this->files = $this->getFiles($this->directory, $model);
@@ -66,7 +66,7 @@ class Updater
*/
public function setNamespace($namespace)
{
$this->baseNamespace = $namespace;
$this->modelNamespace = $namespace;
}
/**
@@ -195,13 +195,13 @@ class Updater
/**
* Filter record criteria.
*
* @param stdClass $record
* @param object $record
*
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException
*
* @return \Illuminate\Support\Collection
*/
protected function getCriteria(stdClass $record)
protected function getCriteria(object $record)
{
$criteria = collect($record)->filter(function ($value, $key) {
return $this->isCriteria($key);
@@ -219,11 +219,11 @@ class Updater
/**
* Filter record values.
*
* @param stdClass $record
* @param object $record
*
* @return \Illuminate\Support\Collection
*/
protected function getValues(stdClass $record)
protected function getValues(object $record)
{
return collect($record)->reject(function ($value, $key) {
if ($this->isCriteria($key)) {
@@ -247,7 +247,7 @@ class Updater
*/
protected function getModel(string $name)
{
return $this->baseNamespace.Str::studly(pathinfo($name, PATHINFO_FILENAME));
return $this->modelNamespace . Str::studly(pathinfo($name, PATHINFO_FILENAME));
}
/**
@@ -288,11 +288,11 @@ class Updater
* Return ID for nested key-value pairs.
*
* @param string $key
* @param stdClass $values
* @param object $values
*
* @return array
*/
protected function resolveId(string $key, stdClass $values)
protected function resolveId(string $key, object $values)
{
$model = $this->getModel($key);