Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Функция возврата буфера с данными #1

Closed
batkor opened this issue Jun 17, 2018 · 2 comments
Closed

Функция возврата буфера с данными #1

batkor opened this issue Jun 17, 2018 · 2 comments

Comments

@batkor
Copy link
Contributor

batkor commented Jun 17, 2018

Привет, Спасибо большое за модуль API!
Наверно стоит создать функцию для возврата данных из буфера для использования сгенерированных данных в Контроллере?
#Например

/**
   * Generate file on all provided data.
   */
  public function generateFile($filename = 'products.xml', $destination_path = 'public://') {
    $this->writeHeader();
    $this->buildData();
    file_unmanaged_copy($this->tempFilePath, $destination_path . $filename, FILE_EXISTS_REPLACE);
  }

  protected function buildData(){
    $this->writeShopInfo();
    $this->writeCurrencies();
    $this->writeCategories();
    $this->writeDeliveryOptions();
    $this->writeOffers();
    $this->writeFooter();
  }

  public function getResponceData(){
    $this->writer->openMemory();
    $this->buildData();
    return $this->writer->outputMemory();
  }

И можно уже использовать в контроллере отдавая в качестве ответа уже готовый xml
#Например

public function build() {
    /** @var \Drupal\yandex_yml\YandexYmlGenerator $generator */
    $generator = \Drupal::service('yandex_yml.generator');

    /** @var \Drupal\yandex_yml\YandexYml\Shop\YandexYmlShop $shop_info */
    $shop_info = \Drupal::service('yandex_yml.shop')
      ->setName('Example shop name')
      ->setCompany('Fullname of the store')
      ->setAgency('https://niklan.net')
      ->setEmail('hello@niklan.net')
      ->setCpa(0);
    $generator->setShopInfo($shop_info);
    
    $response = new CacheableResponse($generator->getResponceData(), Response::HTTP_OK, [
      'content-type' => 'application/xml',
    ]);

    return $response;
  }
@Niklan
Copy link
Owner

Niklan commented Jun 18, 2018

Привет. Это готовый код или пример? Мог бы пул реквестом оформить, полезная вещь.

batkor pushed a commit to batkor/yandex_yml that referenced this issue Jun 18, 2018
Niklan added a commit that referenced this issue Jun 27, 2018
#1 Функция возврата буфера с данными
@Niklan
Copy link
Owner

Niklan commented Jul 6, 2018

Проверил, работает. Спасибо. Скоро в релиз засуну.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants