有没有更好的方法循环这些 PHP 代码,也许使用 foreach 循环?

Is there a better way cycle through these PHP codes, maybe with foreach loop?

我使用file_get_contents获取远程定价(每天更新),使用substr只保留我想要的部分(从输出中剥离货币符号和其他数据,只保留数字)并使用 file_put_contents 将其存储到我稍后提到的缓存目录中。

这是我现在拥有的:-

<?php

$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';

// Small Plan - US
$cachefile_SM_US = $cacheDirectory . 'SM_US.cache';

if(file_exists($cachefile_SM_US)) {
    if(time() - filemtime($cachefile_SM_US) > 1600) {
        // too old , re-fetch
        $cache_SM_US = file_get_contents('https://remotedomain.com/?get=price&product=10&currency=1');
        $substr_SM_US = substr($cache_SM_US,17,2);
        file_put_contents($cachefile_SM_US, $substr_SM_US);
        } else {
            // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_SM_US = file_get_contents('https://remotedomain.com/?get=price&product=10&currency=1');
    $substr_SM_US = substr($cache_SM_US,17,2);
    file_put_contents($cachefile_SM_US, $substr_SM_US);
}

// Large Plan - US
$cachefile_LG_US = $cacheDirectory . 'LG_US.cache';

if(file_exists($cachefile_LG_US)) {
    if(time() - filemtime($cachefile_LG_US) > 1600) {
        // too old , re-fetch
        $cache_LG_US = file_get_contents('https://remotedomain.com/?get=price&product=20&currency=1');
        $substr_LG_US = substr($cache_LG_US,17,2);
        file_put_contents($cachefile_LG_US, $substr_LG_US);
    } else {
        // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_LG_US = file_get_contents('https://remotedomain.com/?get=price&product=20&currency=1');
    $substr_LG_US = substr($cache_LG_US,17,2);
    file_put_contents($cachefile_LG_US, $substr_LG_US);
}

// Small Plan - EU
$cachefile_SM_EU = $cacheDirectory . 'SM_EU.cache';

if(file_exists($cachefile_SM_EU)) {
    if(time() - filemtime($cachefile_SM_EU) > 1600) {
        // too old , re-fetch
        $cache_SM_EU = file_get_contents('https://remotedomain.com/?get=price&product=10&currency=2');
        $substr_SM_EU = substr($cache_SM_EU,17,2);
        file_put_contents($cachefile_SM_EU, $substr_SM_EU);
        } else {
            // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_SM_EU = file_get_contents('https://remotedomain.com/?get=price&product=10&currency=2');
    $substr_SM_EU = substr($cache_SM_EU,17,2);
    file_put_contents($cachefile_SM_EU, $substr_SM_EU);
}

// Large Plan - EU
$cachefile_LG_EU = $cacheDirectory . 'LG_EU.cache';

if(file_exists($cachefile_LG_EU)) {
    if(time() - filemtime($cachefile_LG_EU) > 1600) {
        // too old , re-fetch
        $cache_LG_EU = file_get_contents('https://remotedomain.com/?get=price&product=20&currency=2');
        $substr_LG_EU = substr($cache_LG_EU,17,2);
        file_put_contents($cachefile_LG_EU, $substr_LG_EU);
    } else {
        // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_LG_EU = file_get_contents('https://remotedomain.com/?get=price&product=20&currency=2');
    $substr_LG_EU = substr($cache_LG_EU,17,2);
    file_put_contents($cachefile_LG_EU, $substr_LG_EU);
}

?>

当只有两种产品(1020)和两种货币(12)时,这种手动方式有效,因为我只需要这样做获得我需要的所有定价的 4 次。

但是,我打算将产品数量显着增加到至少 12 种产品和 9 种货币,因此手动完成它们是不现实的。

我相信使用 PHP foreach 循环可以更有效地完成此操作,但我尝试了几天但未能成功,这可能是因为我对这个概念的理解较弱。

我设法将其拆分为:-

<?php

$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';

$url = 'https://remotedomain.com/?get=price';
$productA = 10;
$productB = 20;
$USD = 1;
$EUR = 2;

// Small Plan - US
$cachefile_SM_US = $cacheDirectory . 'SM_US.cache';

if(file_exists($cachefile_SM_US)) {
    if(time() - filemtime($cachefile_SM_US) > 1600) {
        // too old , re-fetch
        $cache_SM_US = file_get_contents($url . '&product=' . $productA . '&currency=' . $USD);
        $substr_SM_US = substr($cache_SM_US,17,2);
        file_put_contents($cachefile_SM_US, $substr_SM_US);
        } else {
            // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_SM_US = file_get_contents($url . '&product=' . $productA . '&currency=' . $USD);
    $substr_SM_US = substr($cache_SM_US,17,2);
    file_put_contents($cachefile_SM_US, $substr_SM_US);
}

// Large Plan - US
$cachefile_LG_US = $cacheDirectory . 'LG_US.cache';

if(file_exists($cachefile_LG_US)) {
    if(time() - filemtime($cachefile_LG_US) > 1600) {
        // too old , re-fetch
        $cache_LG_US = file_get_contents($url . '&product=' . $productB . '&currency=' . $USD);
        $substr_LG_US = substr($cache_LG_US,17,2);
        file_put_contents($cachefile_LG_US, $substr_LG_US);
    } else {
        // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_LG_US = file_get_contents($url . '&product=' . $productB . '&currency=' . $USD);
    $substr_LG_US = substr($cache_LG_US,17,2);
    file_put_contents($cachefile_LG_US, $substr_LG_US);
}

// Small Plan - EU
$cachefile_SM_EU = $cacheDirectory . 'SM_EU.cache';

if(file_exists($cachefile_SM_EU)) {
    if(time() - filemtime($cachefile_SM_EU) > 1600) {
        // too old , re-fetch
        $cache_SM_EU = file_get_contents($url . '&product=' . $productA . '&currency=' . $EUR);
        $substr_SM_EU = substr($cache_SM_EU,17,2);
        file_put_contents($cachefile_SM_EU, $substr_SM_EU);
        } else {
            // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_SM_EU = file_get_contents($url . '&product=' . $productA . '&currency=' . $EUR);
    $substr_SM_EU = substr($cache_SM_EU,17,2);
    file_put_contents($cachefile_SM_EU, $substr_SM_EU);
}

// Large Plan - EU
$cachefile_LG_EU = $cacheDirectory . 'LG_EU.cache';

if(file_exists($cachefile_LG_EU)) {
    if(time() - filemtime($cachefile_LG_EU) > 1600) {
        // too old , re-fetch
        $cache_LG_EU = file_get_contents($url . '&product=' . $productB . '&currency=' . $EUR);
        $substr_LG_EU = substr($cache_LG_EU,17,2);
        file_put_contents($cachefile_LG_EU, $substr_LG_EU);
    } else {
        // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_LG_EU = file_get_contents($url . '&product=' . $productB . '&currency=' . $EUR);
    $substr_LG_EU = substr($cache_LG_EU,17,2);
    file_put_contents($cachefile_LG_EU, $substr_LG_EU);
}

?>

我现在面临的挑战是如何将其转换为循环遍历每种产品和每种货币的 foreach 循环。

感谢正确方向的指示。

谢谢!

当然可以。看看这个例子:)

<?php declare(strict_types=1);

interface CacheNormalizer
{
    public function normalize(string $text): string;
}

interface PlanDomainToCache
{
    public function buildUrl(Plan $plan): string;
}

final class CachedRemoteSiteManager
{
    /** @var int Time To Live Cache */
    private $timeToLive;

    /** @var CacheNormalizer */
    private $cacheNormalizer;

    /** @var PlanDomainToCache */
    private $planDomainToCache;

    public function __construct(
        int $timeToLive,
        CacheNormalizer $cacheNormalizer,
        PlanDomainToCache $planDomainToCache
    ) {
        $this->timeToLive = $timeToLive;
        $this->cacheNormalizer = $cacheNormalizer;
        $this->planDomainToCache = $planDomainToCache;
    }

    public function updateIfNecessary(Plan $plan): void
    {
        if ($this->shouldCreateOrUpdateCache($plan)) {
            $this->createOrUpdateCache($plan);
        }
    }

    private function shouldCreateOrUpdateCache(Plan $plan): bool
    {
        return !file_exists($plan->cacheDirectory())
            || time() - filemtime($plan->cacheDirectory()) > $this->timeToLive;
    }

    private function createOrUpdateCache(Plan $plan): void
    {
        $urlToCache = $this->planDomainToCache->buildUrl($plan);
        $textToCache = file_get_contents($urlToCache);

        file_put_contents(
            $plan->cacheDirectory(),
            $this->cacheNormalizer->normalize($textToCache)
        );
    }
}

final class Plan
{
    /** @var string */
    private $cacheDirectory;

    /** @var int */
    private $product;

    /** @var int */
    private $currency;

    public function __construct(string $cacheDir, int $product, int $currency)
    {
        $this->cacheDirectory = $cacheDir;
        $this->product = $product;
        $this->currency = $currency;
    }

    public function cacheDirectory(): string
    {
        return $this->cacheDirectory;
    }

    public function product(): int
    {
        return $this->product;
    }

    public function currency(): int
    {
        return $this->currency;
    }
}

// Usage example:

$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';
$productA = 10;
$productB = 20;
$USD = 1;
$EUR = 2;

/** @var Plan[] */
$plansToCache = [
    new Plan($cacheDirectory . 'SM_US.cache', $productA, $USD),
    new Plan($cacheDirectory . 'LG_US.cache', $productB, $USD),
    new Plan($cacheDirectory . 'SM_EU.cache', $productA, $EUR),
    new Plan($cacheDirectory . 'LG_EU.cache', $productB, $EUR),
];

$cacheManager = new CachedRemoteSiteManager(
    $cacheTtl = 1600,
    new class implements CacheNormalizer {
        public function normalize(string $text): string
        {
            return substr($text, 17, 2);
        }
    },
    new class implements PlanDomainToCache {
        public function buildUrl(Plan $plan): string
        {
            return sprintf(
                'https://remotedomain.com/?get=price&product=%d&currency=%d',
                $plan->product(),
                $plan->currency()
            );
        }
    }
);

foreach ($plansToCache as $plan) {
    $cacheManager->updateIfNecessary($plan);
}

正如您在底部看到的,在 "usage example" 中,我提取了所有细节(几乎所有细节),因此我们可以轻松定义:

  • 我们希望如何规范化缓存数据(使用 CacheNormalizer)
  • 我们要如何构建要缓存的 URL(使用 PlanDomainToCache)。

更新:

如果您想查看如何 extract/decouple 结束代码中的每个细节,即使对于 "Persistence" 层也向上反转依赖关系:https://gist.github.com/Chemaclass/01d3f42685ff69f6897192202a32014d

如果我对代码的解释正确,您希望针对两种货币查找两种产品。这可以在您定义产品和货币后使用嵌套的 foreach 循环来完成。

$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';
$url = 'https://remotedomain.com/?get=price';

const MAX_CACHE_TIME = 1600;

// Optional
$output = [];

$productList = [
    [
        'id'   => 10,
        'name' => 'SM',
    ],
    [
        'id'   => 20,
        'name' => 'LG',
    ]
];

$currencies = [
    'US' => 1,
    'EU' => 2,
];

foreach ($productList as $product) {
    foreach ($currencies as $currencyName => $currencyId) {
        $cacheFile = $cacheDirectory . $product['name'] . '_' . $currencyName . '.cache';

        if (!file_exists($cacheFile) || filemtime($cacheFile) > MAX_CACHE_TIME) {
            // No cache or too old
            $data = file_get_contents($url . '&product=' . $product['id'] . '&currency=' . $currencyId);
            $relevantData = substr($data, 17, 2);
            file_put_contents($cacheFile, $relevantData);
            // Optional, put the data in an array
            $output[$product['id']][$currencyId] = $relevantData;
        } else {
            $output[$product['id']][$currencyId] = file_get_contents($cacheFile);
        }

    }
}

// Read output with $output[10]['US'] for example