Skip to content

Commit

Permalink
some Contao 5 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Mar 10, 2023
1 parent 3ab4044 commit e303cb2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"symfony/console": "^4.4 || ^5.0 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.0 || ^6.0",
"symfony/event-dispatcher": "^4.4 || ^5.0 || ^6.0",
"symfony/http-kernel": "^4.4 || ^5.0 || ^6.0"
"symfony/http-kernel": "^4.4 || ^5.0 || ^6.0",
"webmozart/path-util": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16"
Expand Down
6 changes: 3 additions & 3 deletions src/Event/ImportWordPressPostEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Client $client, $post, NewsModel $news)
$this->news = $news;
}

/**
/**
* The HTTP client used for accessing the WordPress API.
*/
public function getClient(): Client
Expand All @@ -48,15 +48,15 @@ public function getClient(): Client

/**
* The WordPress post received from the API.
*
*
* @return object
*/
public function getPost()
{
return $this->post;
}

/**
/**
* The generated NewsModel instance for this WordPress post.
*/
public function getNews(): NewsModel
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
- '@contao.framework'
- '@event_dispatcher'
- '@logger'
- '%kernel.project_dir%'

wordpressimport_command:
class: WordPressImportBundle\Command\ImportCommand
Expand All @@ -18,4 +19,4 @@ services:
wordpressimport_cron:
public: true
class: WordPressImportBundle\Utils\Cron
arguments: ['@wordpressimporter', '@logger']
arguments: ['@wordpressimporter', '@logger', '@contao.framework']
32 changes: 20 additions & 12 deletions src/Service/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Contao\NewsModel;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\System;
use Contao\UserModel;
use Doctrine\DBAL\Connection;
use GuzzleHttp\Client;
Expand All @@ -33,6 +32,7 @@
use PHPHtmlParser\Exceptions\ChildNotFoundException;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Webmozart\PathUtil\Path;
use WordPressImportBundle\Event\ImportWordPressPostEvent;

class Importer
Expand Down Expand Up @@ -76,29 +76,37 @@ class Importer
*/
protected $framework;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* Logger.
*
* @var LoggerInterface
*/
protected $logger;

/**
* Contao root dir.
*
* @var string
*/
protected $projectDir;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* Constructor for Importer service.
*
* @param Connection $db Database connection
*/
public function __construct(Connection $db, ContaoFramework $framework, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
public function __construct(Connection $db, ContaoFramework $framework, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, string $projectDir)
{
$this->db = $db;
$this->framework = $framework;
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
$this->projectDir = $projectDir;
}

/**
Expand Down Expand Up @@ -422,13 +430,13 @@ protected function downloadFile($strUrl, $strTargetFolder, $strBase): ?FilesMode
$strFilePath = $strTargetFolder.'/'.$strSubFolder.'/'.$strFileName;

// check if file exists already
if (file_exists(TL_ROOT.'/'.$strFilePath)) {
if (file_exists(Path::join($this->projectDir, $strFilePath))) {
return Dbafs::addResource($strFilePath);
}

// check if subdirectory exists
if (!file_exists(TL_ROOT.'/'.$strTargetFolder.'/'.$strSubFolder)) {
mkdir(TL_ROOT.'/'.$strTargetFolder.'/'.$strSubFolder, 0777, true);
if (!file_exists(Path::join($this->projectDir, $strTargetFolder, $strSubFolder))) {
mkdir(Path::join($this->projectDir, $strTargetFolder, $strSubFolder), 0777, true);
}

// Prepend base if necessary
Expand All @@ -438,14 +446,14 @@ protected function downloadFile($strUrl, $strTargetFolder, $strBase): ?FilesMode

// download the file
try {
(new Client())->get($strUrl, ['sink' => TL_ROOT.'/'.$strFilePath]);
(new Client())->get($strUrl, ['sink' => Path::join($this->projectDir, $strFilePath)]);
} catch (\Exception $e) {
$this->logger->error('Could not download "'.$strUrl.'": '.$e->getMessage());

return null;
}

if (file_exists(TL_ROOT.'/'.$strFilePath)) {
if (file_exists(Path::join($this->projectDir, $strFilePath))) {
return Dbafs::addResource($strFilePath);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Utils/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace WordPressImportBundle\Utils;

use Contao\Config;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Monolog\ContaoContext;
use Contao\CoreBundle\ServiceAnnotation\CronJob;
use Psr\Log\LoggerInterface;
Expand All @@ -23,11 +24,13 @@ class Cron
{
private $importer;
private $logger;
private $framework;

public function __construct(Importer $importer, LoggerInterface $logger)
public function __construct(Importer $importer, LoggerInterface $logger, ContaoFramework $framework)
{
$this->importer = $importer;
$this->logger = $logger;
$this->framework = $framework;
}

/**
Expand All @@ -37,6 +40,8 @@ public function __construct(Importer $importer, LoggerInterface $logger)
*/
public function import(): void
{
$this->framework->initialize();

try {
$this->importer->import(Config::get('wpImportLimit'), true);
} catch (\Exception $e) {
Expand Down

0 comments on commit e303cb2

Please sign in to comment.