You are here

class WeatherCommands in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Commands/WeatherCommands.php \Drupal\weather\Commands\WeatherCommands

Drush command to add support to weather module (import weather csv file).

In addition to this file, you need a drush.services.yml in root of your module, and a composer.json file that provides the name of the services file to use.

See these files for an example of injecting Drupal services:

Hierarchy

  • class \Drupal\weather\Commands\WeatherCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of WeatherCommands

1 string reference to 'WeatherCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses WeatherCommands
weather.commands in ./drush.services.yml
\Drupal\weather\Commands\WeatherCommands

File

src/Commands/WeatherCommands.php, line 18

Namespace

Drupal\weather\Commands
View source
class WeatherCommands extends DrushCommands {

  /**
   * Items from csv.
   *
   * @var array
   */
  private $items;

  /**
   * Import data from .csv to database and log.
   *
   * @usage weather-import
   *   No args needed.
   *
   * @command weather:import
   * @aliases weather-i
   */
  public function import() {
    $this
      ->del()
      ->read()
      ->add()
      ->logger()
      ->success(dt('Ok.'));
  }

  /**
   * Delete.
   */
  private function del() : self {
    $all = $this
      ->store()
      ->loadByProperties([
      'status' => 'original',
    ]);
    $i = 0;
    $c = count($all);
    foreach ($all as $del) {
      $del
        ->delete();
      if (++$i % 50 === 0) {
        $this
          ->logger()
          ->info("del {$i} / {$c}");
      }
    }
    return $this;
  }

  /**
   * Open file.
   */
  private function csv() {
    return fopen(drupal_get_path('module', 'weather') . '/files/weather_data.csv', 'r');
  }

  /**
   * Save.
   */
  private function store() {
    return \Drupal::service('entity_type.manager')
      ->getStorage('weather_place');
  }

  /**
   * Read.
   */
  private function read() : self {
    $file = $this
      ->csv();
    $i = 0;
    $items = [];
    while (($line = fgetcsv($file, 0, '	')) !== FALSE) {
      $items[] = $line;
      if (++$i % 500 === 0) {
        $this
          ->logger()
          ->info("read {$i}");
      }
    }
    fclose($file);
    $this->items = $items;
    return $this;
  }

  /**
   * Add.
   */
  private function add() : self {
    $s = $this
      ->store();
    $i = 0;
    $c = count($this->items);
    $this
      ->logger()
      ->info("will add: {$c}");
    foreach ($this->items as $item) {
      $s
        ->create([
        'geoid' => $item[0],
        'latitude' => $item[1],
        'longitude' => $item[2],
        'country' => $item[3],
        'name' => $item[4],
        'link' => trim($item[5]),
        'status' => 'original',
      ])
        ->save();
      if (++$i % 50 === 0) {
        $this
          ->logger()
          ->success("add {$i} / {$c}");
      }
    }
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WeatherCommands::$items private property Items from csv.
WeatherCommands::add private function Add.
WeatherCommands::csv private function Open file.
WeatherCommands::del private function Delete.
WeatherCommands::import public function Import data from .csv to database and log.
WeatherCommands::read private function Read.
WeatherCommands::store private function Save.