gremo/subscription-bundle

该软件包已 废弃 且不再维护。未建议替代软件包。

Symfony2 订阅管理 Bundle

v1.0.0 2012-12-14 20:55 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:21:02 UTC


README

Symfony2 订阅管理 Bundle

安装

将以下内容添加到您的 deps 文件(适用于 Symfony 2.0.*)

[GremoSubscriptionBundle]
    git=https://github.com/gremo/GremoSubscriptionBundle.git
    target=bundles/Gremo/SubscriptionBundle

然后使用自动加载器注册命名空间(app/autoload.php

$loader->registerNamespaces(array(
    // ...
    'Gremo' => __DIR__.'/../vendor/bundles',
    // ...
));

或者,如果您正在使用 Composer 和 Symfony 2.1.*,请将其添加到 composer.json 文件中

{
    "require": {
        "gremo/subscription-bundle": "*"
    }
}

最后,在 app/appKernel.php 中将软件包注册到您的内核中

public function registerBundles()
{
    $bundles = array(
        // ...
        new Gremo\SubscriptionBundle\GremoSubscriptionBundle(),
        // ...
    );

    // ...
}

配置

Bundle 配置很简单:您首先需要指定订阅周期的间隔。使用 \DateInterval 间隔指定格式。例如,P30D,即 30 天。间隔至少应为一天。

然后实现 Gremo\SubscriptionBundle\Provider\ActivationDateProviderInterface,以便提供激活日期。将此类作为服务实现,并在配置中将名称设置为 activation_provider

gremo_subscription:
    interval: P30D
    activation_provider: my_activation_provider

激活日期提供者示例

一个激活日期提供者的示例,其中激活日期是当前登录用户的创建日期

use Gremo\SubscriptionBundle\Provider\ActivationDateProviderInterface;
use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Component\Security\Core\SecurityContext;

/**
 * @DI\Service("my_activation_provider")
 */
class MyActivationProvider implements ActivationDateProviderInterface
{
    /**
     * @var \Symfony\Component\Security\Core\SecurityContext
     */
    private $context;

    /**
     * @DI\InjectParams({"context" = @DI\Inject("security.context")})
     */
    public function __construct(SecurityContext $context)
    {
        $this->context = $context;
    }

    /**
     * @return \DateTime
     */
    public function getActivationDate()
    {
        $user = $this->context->getToken()->getUser();

        return $user->getCreatedAt();
    }
}

使用方法

您可以通过服务容器访问订阅服务,例如在您的控制器代码中

$subscription = $this->get('gremo_subscription'):

假设今天是 2012-12-12,间隔是 30 天,激活日期是 2012-09-01。周期将如下

  • 从 2012-09-01 到 2012-09-30(含),第一个周期
  • 从 2012-10-01 到 2012-10-30(含)
  • 从 2012-10-31 到 2012-11-29(含)
  • 从 2012-11-30 到 2012-12-29(含),即当前周期

从订阅中访问当前周期

$currentPeriod = $subscription->getCurrentPeriod(); // Period from 2012-11-30 to 2012-12-29

$firstDate = $currentPeriod->getFirstDate(); // DateTime object (2012-11-30)
$lastDate  = $currentPeriod->getLastDate();  // DateTime object (2012-12-29)

BaseSubscription 实现了 PHP 的 CountableArrayAccessIterator 接口,因此您可以轻松地计数、访问和遍历每个周期。BaseSubscriptionPeriod 从 PHP 的 DatePeriod 对象继承,允许您遍历周期中的每一天

// Get periods count
$numPeriods = count($subscription); // 4

// Get the previous period
$previusPeriod = $subscription[$numPeriods - 1]; // Period from 2012-10-31 to 2012-11-29

// Loop over each day of the previous period
foreach($previusPeriod as date)
{
    // ...
}

// Loop over each period
foreach($subscription as $period)
{
    // ...
}

// Find out a period for the given date (may return null)
$period = $subscription->getPeriod(new \DateTime('2012-11-25')); // Period form 2012-10-01 to 2012-10-30