Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active January 6, 2024 14:00
Show Gist options
  • Save hissy/e8764f3453fd5e51632d6e783d90639e to your computer and use it in GitHub Desktop.
Save hissy/e8764f3453fd5e51632d6e783d90639e to your computer and use it in GitHub Desktop.
Concrete CMS: A helper class to install CIF format xml files without duplicated run
<?php
namespace Acme\Updater;
use Concrete\Core\Entity\Package as PackageEntity;
use Concrete\Core\Package\Package;
use Symfony\Component\Finder\Finder;
class Updater
{
private Package $controller;
public function __construct(PackageEntity $package)
{
$this->controller = $package->getController();
}
public function update(): void
{
$files = $this->getXmlFiles();
$installed = $this->getRecentInstalledXmlFile();
foreach ($files as $file) {
if ($installed) {
if ($file > $installed) {
$this->controller->installContentFile('update/' . $file);
$this->saveRecentInstalledXmlFile($file);
}
}
}
}
public function getXmlFiles(): \Generator
{
$finder = new Finder();
$finder->files()->name('*.xml')->in($this->controller->getPackagePath() . '/update')->sortByName();
foreach ($finder as $file) {
yield $file->getFilename();
}
}
public function getRecentInstalledXmlFile()
{
return $this->controller->getFileConfig()->get('update.xml_installed', '20000000000000');
}
public function saveRecentInstalledXmlFile(string $filename): void
{
$this->controller->getFileConfig()->save('update.xml_installed', $filename);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment