Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SftpLoader.php #146

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6343e75
fix: change ElementInterface | null to ?ElementInterface (#110)
youwe-petervanderwal Oct 29, 2021
fadc6cc
Merge branch '1.3' into 1.x
dvesh3 Nov 12, 2021
46153c5
Adding Datarow information in the logs (#123)
shamoh19 Nov 16, 2021
d4e4f06
Apply php-cs-fixer changes
mcop1 Nov 16, 2021
68e4a22
Update installation documentation (#126)
Yivan Nov 29, 2021
5f7d308
Update 02_File_Formats.md (#133)
kaurov Dec 1, 2021
316521d
Update 02_Transformation_Pipeline.md (#134)
kaurov Dec 1, 2021
9be61ba
Add folder groups to data importer configurations (#129)
flamadrid-jsmdg Dec 7, 2021
73d004b
Config Name Limit Increase (#120)
shamoh19 Dec 10, 2021
a2f0fdd
Improved logging in operator "LoadDataObject" (#117)
aweichler Dec 10, 2021
b6af7c4
Apply php-cs-fixer changes
mcop1 Dec 10, 2021
8ecddef
Merge remote-tracking branch 'origin/1.3' into 1.x
fashxp Dec 14, 2021
9de77df
Merge remote-tracking branch 'origin/1.3' into 1.x
fashxp Dec 14, 2021
6569c18
[Configuration] Add more fine-grained permissions to configurations (…
dvesh3 Jan 5, 2022
e9f2a03
Setup PhpStan Test (#141)
blankse Jan 5, 2022
e4cbb7b
Adding Datarow information in the logs (#123)
shamoh19 Nov 16, 2021
1922620
Apply php-cs-fixer changes
mcop1 Nov 16, 2021
cc0cf8b
Update 02_File_Formats.md (#133)
kaurov Dec 1, 2021
e1f6fb4
Add folder groups to data importer configurations (#129)
flamadrid-jsmdg Dec 7, 2021
2209f43
Config Name Limit Increase (#120)
shamoh19 Dec 10, 2021
c5e5b38
Improved logging in operator "LoadDataObject" (#117)
aweichler Dec 10, 2021
f107936
Apply php-cs-fixer changes
mcop1 Dec 10, 2021
b2e2ead
Update SftpLoader.php
Daniel-Ateles Dec 22, 2021
8dbf8e1
Merge branch '1.x' into sftloader-fix-2
Daniel-Ateles Jan 19, 2022
c25b45b
Apply php-cs-fixer changes
Daniel-Ateles Jan 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ci/files/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_ENV=test
5 changes: 5 additions & 0 deletions .github/ci/scripts/setup-pimcore-environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -eu -o xtrace

cp .github/ci/files/.env .
42 changes: 42 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Static Analysis"

on:
pull_request:
branches:
- "[0-9]+.[0-9]+"
- "[0-9]+.x"
push:
branches:
- "[0-9]+.[0-9]+"
- "[0-9]+.x"

jobs:
static-analysis-phpstan:
name: "Static Analysis with PHPStan"
runs-on: "ubuntu-20.04"
strategy:
matrix:
include:
- { php-version: "8.0", dependencies: "lowest" }
- { php-version: "8.1", dependencies: "highest" }
steps:
- name: "Checkout code"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"

- name: "Setup Pimcore environment"
run: |
.github/ci/scripts/setup-pimcore-environment.sh

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v1"
with:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Run a static analysis with phpstan/phpstan"
run: "vendor/bin/phpstan analyse --memory-limit=-1"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Thumbs.db
*.log

/.env
/phpunit.xml

# PHP-CS-Fixer
Expand All @@ -15,4 +16,4 @@ Thumbs.db
!/vendor/.gitkeep

# PhpStorm / IDEA
.idea
.idea
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
"sort-packages": true
},
"require": {
"php": ">=7.4",
"pimcore/pimcore": "^6.9 || ^10.0",
"pimcore/data-hub": "^0.7 || ^1.0",
"phpoffice/phpspreadsheet": "^1.1",
"php": "^8.0",
"pimcore/pimcore": "^10.2",
"pimcore/data-hub": "^1.2@dev",
"phpoffice/phpspreadsheet": "^1.15",
"dragonmantank/cron-expression": "^3.1",
"nesbot/carbon": "^2.27",
"league/flysystem": "^2.0",
"league/flysystem-sftp": "^2.0",
"league/flysystem-bundle" : "^2.0",
"ext-json": "*",
"ext-fileinfo": "*"
},
"require-dev": {
"phpstan/phpstan": "^1.2"
},
"autoload": {
"psr-4": {
"Pimcore\\Bundle\\DataImporterBundle\\": "src/"
Expand Down
4 changes: 2 additions & 2 deletions doc/01_Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ to be installed first.
To install Pimcore Data Importer use following commands:

```bash
composer require pimcore/data-importer
composer require pimcore/data-importer --with-all-dependencies
./bin/console pimcore:bundle:enable PimcoreDataImporterBundle
```

Expand All @@ -21,4 +21,4 @@ Configure following commands to be executed on a regular base. The actual interv
# Process import queue items that need to be executed sequentially
*/5 * * * * php /home/project/www/bin/console datahub:data-importer:process-queue-sequential
```
See [Import Execution Details](04_Import_Execution_Details.md) for more information about sequential and parallel execution.
See [Import Execution Details](04_Import_Execution_Details.md) for more information about sequential and parallel execution.
2 changes: 2 additions & 0 deletions doc/03_Configuration/02_File_Formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,6 @@ below, this the sub elements are interpreted as array in one field (and needs to
Internally the adapter uses [Symfony `XmlUtils`](https://github.com/symfony/config/blob/master/Util/XmlUtils.php) to read
and validate the data.

### Custom File Formats
You can import any file format using custom adapters.

19 changes: 19 additions & 0 deletions phpstan-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

if (file_exists(__DIR__ . '/vendor/autoload.php')) {
define('PIMCORE_PROJECT_ROOT', __DIR__);
} elseif (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
define('PIMCORE_PROJECT_ROOT', __DIR__ . '/../../..');
} elseif (getenv('PIMCORE_PROJECT_ROOT')) {
define('PIMCORE_PROJECT_ROOT', getenv('PIMCORE_PROJECT_ROOT'));
} else {
throw new \Exception('Unknown configuration! Pimcore project root not found, please set env variable PIMCORE_PROJECT_ROOT.');
}

include PIMCORE_PROJECT_ROOT . '/vendor/autoload.php';
\Pimcore\Bootstrap::setProjectRoot();
\Pimcore\Bootstrap::bootstrap();

if (!defined('PIMCORE_TEST')) {
define('PIMCORE_TEST', true);
}
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: 4
paths:
- src
bootstrapFiles:
- phpstan-bootstrap.php
excludePaths:
- src/Command/DummyDataCommand.php
stubFiles:
- stubs/ElementInterface.stub
4 changes: 2 additions & 2 deletions src/Cleanup/CleanupStrategyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public function __construct(array $cleanupStrategies)
/**
* @param string $type
*
* @return mixed
* @return CleanupStrategyInterface
*
* @throws InvalidConfigurationException
*/
public function loadCleanupStrategy(string $type)
{
if (empty($type) || !array_key_exists($type, $this->cleanupStrategies)) {
throw new InvalidConfigurationException('Unknown loader type `' . ($type ?? '') . '`');
throw new InvalidConfigurationException('Unknown loader type `' . $type . '`');
}

return $this->cleanupStrategies[$type];
Expand Down
4 changes: 3 additions & 1 deletion src/Command/DummyDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'email',
];

$count = $input->getOption('items');
$count = (int) $input->getOption('items');

for ($i = 0; $i < $count; $i++) {
$output->writeln('Generating item ' . $i . ' ...');
Expand Down Expand Up @@ -143,6 +143,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
default:
throw new \Exception('Invalid format: ' . $format);
}

return 0;
}

protected function writeCsv(string $filename, array $data)
Expand Down
17 changes: 12 additions & 5 deletions src/Controller/ConfigDataObjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,19 @@ public function saveAction(Request $request): ?JsonResponse
$name = $dataDecoded['general']['name'];
$dataDecoded['general']['active'] = $dataDecoded['general']['active'] ?? false;
$config = Dao::getByName($name);

$oldConfig = $config->getConfiguration();

if (!$config->isAllowed('update')) {
throw $this->createAccessDeniedHttpException();
}
$config->setConfiguration($dataDecoded);
$config->save();

return $this->json(['success' => true, 'modificationDate' => Dao::getConfigModificationDate()]);
// @phpstan-ignore-next-line isAllowed return can changed now
if ($config->isAllowed('read') && $config->isAllowed('update')) {
$config->save();

return $this->json(['success' => true, 'modificationDate' => Dao::getConfigModificationDate()]);
} else {
return $this->json(['success' => false, 'permissionError' => true]);
}
} catch (\Exception $e) {
return $this->json(['success' => false, 'message' => $e->getMessage()]);
}
Expand Down Expand Up @@ -143,6 +149,7 @@ public function getAction(Request $request,
[
'name' => $name,
'configuration' => $config,
'userPermissions' => $config['userPermissions'],
'modificationDate' => Dao::getConfigModificationDate(),
'columnHeaders' => $this->loadAvailableColumnHeaders($name, $config, $interpreterFactory)
]
Expand Down
2 changes: 1 addition & 1 deletion src/DataSource/Interpreter/DeltaChecker/DeltaChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(Db\ConnectionInterface $connection)
protected function createTableIfNotExisting(\Closure $callable = null)
{
$this->db->executeQuery(sprintf('CREATE TABLE IF NOT EXISTS %s (
configName varchar(50) NOT NULL,
configName varchar(80) NOT NULL,
id varchar(50) NOT NULL,
hash varchar(100) NOT NULL,
PRIMARY KEY (configName, id))
Expand Down
15 changes: 7 additions & 8 deletions src/DataSource/Interpreter/JsonFileInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
class JsonFileInterpreter extends AbstractInterpreter
{
/**
* @var array
* @var array|null
*/
protected $cachedContent = null;

/**
* @var string
* @var string|null
*/
protected $cachedFilePath = null;

Expand Down Expand Up @@ -57,9 +58,9 @@ public function setSettings(array $settings): void
/**
* remove BOM bytes to have a proper UTF-8
*
* @param $content
* @param string $content
*
* @return false|mixed|string
* @return string
*/
protected function prepareContent($content)
{
Expand Down Expand Up @@ -132,10 +133,8 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo
$previewData[$index] = $columnData;
}

if (empty($columns)) {
$keys = array_keys($previewData);
$columns = array_combine($keys, $keys);
}
$keys = array_keys($previewData);
$columns = array_combine($keys, $keys);
}

return new PreviewData($columns, $previewData, $readRecordNumber, $mappedColumns);
Expand Down
9 changes: 5 additions & 4 deletions src/DataSource/Interpreter/XmlFileInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@
class XmlFileInterpreter extends AbstractInterpreter
{
/**
* @var string
* @var string|null
*/
protected $xpath;

/**
* @var string
* @var string|null
*/
protected $schema;

/**
* @var \DOMDocument
* @var \DOMDocument|null
*/
protected $cachedContent = null;

/**
* @var string
* @var string|null
*/
protected $cachedFilePath = null;

Expand Down
9 changes: 8 additions & 1 deletion src/DataSource/Loader/SftpLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\PhpseclibV2\SftpAdapter;
use League\Flysystem\PhpseclibV2\SftpConnectionProvider;
use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
Expand Down Expand Up @@ -83,12 +84,18 @@ public function loadData(): string
);

$filesystem = new Filesystem(new SftpAdapter($connectionProvider, '/'));
$filesystemLocal = new Filesystem(new LocalFilesystemAdapter('/'));
try {
$filesystem->copy($this->remotePath, $this->importFilePath);
$readStream = $filesystem->readStream($this->remotePath);
$filesystemLocal->writeStream($this->importFilePath, $readStream);

return $this->importFilePath;
} catch (FilesystemException $e) {
Logger::error($e);
throw new InvalidConfigurationException(sprintf('Could not copy from remote location `%s` to local tmp file `%s`', $loggingRemoteUrl, $this->importFilePath));
}

throw new \Exception(sprintf('Could not find any files to load in path `%s`', $this->remotePath));
}

public function cleanup(): void
Expand Down
19 changes: 18 additions & 1 deletion src/DependencyInjection/PimcoreDataImporterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class PimcoreDataImporterExtension extends Extension
class PimcoreDataImporterExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritdoc}
Expand All @@ -38,4 +40,19 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}

/**
* @param ContainerBuilder $container
*/
public function prepend(ContainerBuilder $container)
{
if ($container->hasExtension('doctrine_migrations')) {
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
);

$loader->load('doctrine_migrations.yml');
}
}
}
8 changes: 2 additions & 6 deletions src/EventListener/ConfigurationEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public static function getSubscribedEvents()
*/
public function postDelete(GenericEvent $event)
{
/**
* @var $config Configuration
*/
/** @var Configuration $config */
$config = $event->getSubject();

if ($config->getType() === 'dataImporterDataObject') {
Expand Down Expand Up @@ -117,9 +115,7 @@ public function postDelete(GenericEvent $event)

public function postSave(GenericEvent $event)
{
/**
* @var $config Configuration
*/
/** @var Configuration $config */
$config = $event->getSubject();

if ($config->getType() === 'dataImporterDataObject') {
Expand Down