-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Monsieur Biz' Rich Editor plugin for Sylius. | ||
* | ||
* (c) Monsieur Biz <sylius@monsieurbiz.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MonsieurBiz\SyliusRichEditorPlugin\Fixture; | ||
|
||
use MonsieurBiz\SyliusRichEditorPlugin\Uploader\FixtureFileUploaderInterface; | ||
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture; | ||
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface; | ||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
|
||
final class FileFixture extends AbstractFixture implements FixtureInterface | ||
{ | ||
/** | ||
* @var FixtureFileUploaderInterface | ||
*/ | ||
private FixtureFileUploaderInterface $uploader; | ||
|
||
/** | ||
* @param FixtureFileUploaderInterface $uploader | ||
*/ | ||
public function __construct(FixtureFileUploaderInterface $uploader) | ||
{ | ||
$this->uploader = $uploader; | ||
} | ||
|
||
/** | ||
* @param array $options | ||
*/ | ||
public function load(array $options): void | ||
{ | ||
foreach ($options['files'] as $data) { | ||
$file = new UploadedFile($data['source_path'], basename($data['source_path'])); | ||
$this->uploader->upload($file, $data['target_path']); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return 'monsieurbiz_rich_editor_file'; | ||
} | ||
|
||
/** | ||
* @param ArrayNodeDefinition $optionsNode | ||
*/ | ||
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void | ||
{ | ||
/** @phpstan-ignore-next-line */ | ||
$optionsNode | ||
->children() | ||
->arrayNode('files') | ||
->arrayPrototype() | ||
->children() | ||
->scalarNode('source_path')->cannotBeEmpty()->end() | ||
->scalarNode('target_path')->cannotBeEmpty()->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Monsieur Biz' Rich Editor plugin for Sylius. | ||
* | ||
* (c) Monsieur Biz <sylius@monsieurbiz.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MonsieurBiz\SyliusRichEditorPlugin\Uploader; | ||
|
||
use Symfony\Component\HttpFoundation\File\File; | ||
|
||
interface FixtureFileUploaderInterface | ||
{ | ||
/** | ||
* @param File $file | ||
* @param string $target | ||
*/ | ||
public function upload(File $file, string $target): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Monsieur Biz' Rich Editor plugin for Sylius. | ||
* | ||
* (c) Monsieur Biz <sylius@monsieurbiz.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MonsieurBiz\SyliusRichEditorPlugin\Uploader; | ||
|
||
use Gaufrette\FilesystemInterface; | ||
use Symfony\Component\HttpFoundation\File\File; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class FixtureFixtureFileUploader implements FixtureFileUploaderInterface | ||
{ | ||
/** | ||
* @var FilesystemInterface | ||
*/ | ||
private FilesystemInterface $filesystem; | ||
|
||
/** | ||
* @param FilesystemInterface $filesystem | ||
*/ | ||
public function __construct(FilesystemInterface $filesystem) | ||
{ | ||
$this->filesystem = $filesystem; | ||
} | ||
|
||
/** | ||
* @param File $file | ||
* @param string $target | ||
*/ | ||
public function upload(File $file, string $target): void | ||
{ | ||
Assert::isInstanceOf($file, File::class); | ||
|
||
if ($this->filesystem->has($target)) { | ||
$this->remove($target); | ||
} | ||
|
||
$this->filesystem->write( | ||
$target, | ||
(string) file_get_contents($file->getPathname()) | ||
); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* | ||
* @return bool | ||
*/ | ||
public function remove(string $path): bool | ||
{ | ||
if ($this->filesystem->has($path)) { | ||
return $this->filesystem->delete($path); | ||
} | ||
|
||
return false; | ||
} | ||
} |