Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Commit

Permalink
Fix installer
Browse files Browse the repository at this point in the history
  • Loading branch information
rratsun committed Apr 6, 2020
1 parent 341bb41 commit ab9fdab
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 222 deletions.
9 changes: 0 additions & 9 deletions app/Treo/Listeners/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ protected function generateTreoId(): void
// set to config
$this->getConfig()->set('treoId', $treoId);
$this->getConfig()->save();

$data = json_decode(file_get_contents(Composer::$composer), true);
$data['repositories'][] = [
'type' => 'composer',
'url' => 'https://packagist.treopim.com/packages.json?id=' . $treoId
];

// create repositories file
file_put_contents(Composer::$composer, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

/**
Expand Down
213 changes: 0 additions & 213 deletions app/Treo/Services/TreoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
namespace Treo\Services;

use Espo\Core\Templates\Services\Base;
use Treo\Core\Utils\Util;

/**
* Class TreoStore
Expand All @@ -46,216 +45,4 @@
*/
class TreoStore extends Base
{
const PACKAGES = 'https://packagist.treopim.com/packages.json';

/**
* @inheritDoc
*/
public function findEntities($params)
{
// update store cache
$this->updateStoreCache();

return parent::findEntities($params);
}

/**
* Update store cache if it needs
*/
protected function updateStoreCache(): void
{
// prepare cache path
$path = 'data/cache/store-last-update-time.json';

// prepare last update
$lastUpdate = strtotime('2019-01-01 00:00:00');
if (file_exists($path)) {
$lastUpdate = json_decode(file_get_contents($path), true)['time'];
}

// get diff in minutes
$minutes = (time() - $lastUpdate) / 60;

if ($minutes > 120 && !empty($packages = $this->getRemotePackages())) {
// caching
$this->caching($packages);

// create dir if it needs
if (!file_exists('data/cache')) {
mkdir('data/cache', 0777, true);
}

// save cache file
file_put_contents($path, json_encode(['time' => time()]));
}
}

/**
* @param array $data
*/
protected function caching(array $data): void
{
// delete all
$sth = $this
->getEntityManager()
->getPDO()
->prepare("DELETE FROM treo_store");
$sth->execute();

foreach ($data as $package) {
$entity = $this->getEntityManager()->getEntity("TreoStore");
$entity->id = $package['treoId'];
$entity->set('packageId', $package['packageId']);
$entity->set('url', $package['url']);
$entity->set('status', $package['status']);
$entity->set('versions', $package['versions']);
foreach ($package['name'] as $locale => $value) {
if ($locale == 'default') {
$entity->set('name', $value);
} else {
$entity->set('name' . Util::toCamelCase(strtolower($locale), "_", true), $value);
}
}
foreach ($package['description'] as $locale => $value) {
if ($locale == 'default') {
$entity->set('description', $value);
} else {
$entity->set('description' . Util::toCamelCase(strtolower($locale), "_", true), $value);
}
}
if (!empty($package['tags']) && is_array($package['tags'])) {
$entity->set('tags', $package['tags']);
}

$this->getEntityManager()->saveEntity($entity, ['skipAll' => true]);
}
}

/**
* @return array
*/
protected function getRemotePackages(): array
{
// get all
$all = self::getPathContent(self::PACKAGES);

// get public
$public = self::getPathContent(self::PACKAGES . '?id=public');

// get private
$private = [];
if (!empty($treoId = $this->getConfig()->get('treoId'))) {
$private = self::getPathContent(self::PACKAGES . '?id=' . $treoId);
}

// parse all
$packages = $this->parsePackages($all);

// parse public
if (!empty($public)) {
foreach ($this->parsePackages($public, 'available') as $id => $row) {
$packages[$id] = $row;
}
}

// parse private
if (!empty($private)) {
foreach ($this->parsePackages($private, 'available') as $id => $row) {
$packages[$id] = $row;
}
}

return array_values($packages);
}

/**
* @param string $path
*
* @return array
*/
private static function getPathContent(string $path): array
{
$content = @file_get_contents($path);

return (empty($content)) ? [] : json_decode($content, true);
}

/**
* @param array $packages
* @param string $status
*
* @return array
*/
private function parsePackages(array $packages, string $status = 'buyable'): array
{
/** @var array $result */
$result = [];

/** @var array $data */
$data = [];

foreach ($packages['packages'] as $repository => $versions) {
if (is_array($versions)) {
foreach ($versions as $version => $row) {
if (!empty($row['extra']['treoId'])) {
$treoId = $row['extra']['treoId'];
$version = strtolower($version);
if (preg_match_all('/^v\d+.\d+.\d+$/', $version, $matches)
|| preg_match_all('/^v\d+.\d+.\d+-rc\d+$/', $version, $matches)
|| preg_match_all('/^\d+.\d+.\d+$/', $version, $matches)
|| preg_match_all('/^\d+.\d+.\d+-rc\d+$/', $version, $matches)
) {
// prepare version
$version = str_replace('v', '', $matches[0][0]);

// skip if unstable version
if (strpos($version, 'rc') !== false) {
continue;
}

// push
$data[$treoId][$version] = $row;
}
}
}
}
}

foreach ($data as $treoId => $rows) {
// find max version
$versions = array_keys($rows);
natsort($versions);
$versions = array_reverse($versions);
$max = $versions[0];

// prepare tags
$tags = [];
if (!empty($rows[$max]['extra']['tags'])) {
$tags = $rows[$max]['extra']['tags'];
}

// prepare item
$item = [
'treoId' => $treoId,
'packageId' => $rows[$max]['name'],
'url' => $rows[$max]['source']['url'],
'name' => $rows[$max]['extra']['name'],
'description' => $rows[$max]['extra']['description'],
'tags' => $tags,
'status' => $status
];

foreach ($versions as $version) {
$item['versions'][] = [
'version' => $version,
'require' => $rows[$version]['require'],
];
}

// push
$result[$treoId] = $item;
}

return $result;
}
}

0 comments on commit ab9fdab

Please sign in to comment.