Mercurial > hg > cmmr2012-drupal-site
comparison modules/contrib/migrate_plus/src/DataParserPluginBase.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\migrate_plus; | |
4 | |
5 use Drupal\Core\Plugin\PluginBase; | |
6 use Symfony\Component\DependencyInjection\ContainerInterface; | |
7 | |
8 /** | |
9 * Defines a base data parser implementation. | |
10 * | |
11 * @see \Drupal\migrate_plus\Annotation\DataParser | |
12 * @see \Drupal\migrate_plus\DataParserPluginInterface | |
13 * @see \Drupal\migrate_plus\DataParserPluginManager | |
14 * @see plugin_api | |
15 */ | |
16 abstract class DataParserPluginBase extends PluginBase implements DataParserPluginInterface { | |
17 | |
18 /** | |
19 * List of source urls. | |
20 * | |
21 * @var string[] | |
22 */ | |
23 protected $urls; | |
24 | |
25 /** | |
26 * Index of the currently-open url. | |
27 * | |
28 * @var int | |
29 */ | |
30 protected $activeUrl; | |
31 | |
32 /** | |
33 * String indicating how to select an item's data from the source. | |
34 * | |
35 * @var string | |
36 */ | |
37 protected $itemSelector; | |
38 | |
39 /** | |
40 * Current item when iterating. | |
41 * | |
42 * @var mixed | |
43 */ | |
44 protected $currentItem = NULL; | |
45 | |
46 /** | |
47 * Value of the ID for the current item when iterating. | |
48 * | |
49 * @var string | |
50 */ | |
51 protected $currentId = NULL; | |
52 | |
53 /** | |
54 * The data retrieval client. | |
55 * | |
56 * @var \Drupal\migrate_plus\DataFetcherPluginInterface | |
57 */ | |
58 protected $dataFetcher; | |
59 | |
60 /** | |
61 * {@inheritdoc} | |
62 */ | |
63 public function __construct(array $configuration, $plugin_id, $plugin_definition) { | |
64 parent::__construct($configuration, $plugin_id, $plugin_definition); | |
65 $this->urls = $configuration['urls']; | |
66 $this->itemSelector = $configuration['item_selector']; | |
67 } | |
68 | |
69 /** | |
70 * {@inheritdoc} | |
71 */ | |
72 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
73 return new static($configuration, $plugin_id, $plugin_definition); | |
74 } | |
75 | |
76 | |
77 /** | |
78 * Returns the initialized data fetcher plugin. | |
79 * | |
80 * @return \Drupal\migrate_plus\DataFetcherPluginInterface | |
81 * The data fetcher plugin. | |
82 */ | |
83 public function getDataFetcherPlugin() { | |
84 if (!isset($this->dataFetcherPlugin)) { | |
85 $this->dataFetcherPlugin = \Drupal::service('plugin.manager.migrate_plus.data_fetcher')->createInstance($this->configuration['data_fetcher_plugin'], $this->configuration); | |
86 } | |
87 return $this->dataFetcherPlugin; | |
88 } | |
89 | |
90 /** | |
91 * {@inheritdoc} | |
92 */ | |
93 public function rewind() { | |
94 $this->activeUrl = NULL; | |
95 $this->next(); | |
96 } | |
97 | |
98 /** | |
99 * Implementation of Iterator::next(). | |
100 */ | |
101 public function next() { | |
102 $this->currentItem = $this->currentId = NULL; | |
103 if (is_null($this->activeUrl)) { | |
104 if (!$this->nextSource()) { | |
105 // No data to import. | |
106 return; | |
107 } | |
108 } | |
109 // At this point, we have a valid open source url, try to fetch a row from | |
110 // it. | |
111 $this->fetchNextRow(); | |
112 // If there was no valid row there, try the next url (if any). | |
113 if (is_null($this->currentItem)) { | |
114 if ($this->nextSource()) { | |
115 $this->fetchNextRow(); | |
116 } | |
117 } | |
118 if ($this->valid()) { | |
119 foreach ($this->configuration['ids'] as $id_field_name => $id_info) { | |
120 $this->currentId[$id_field_name] = $this->currentItem[$id_field_name]; | |
121 } | |
122 } | |
123 } | |
124 | |
125 /** | |
126 * Opens the specified URL. | |
127 * | |
128 * @param $url | |
129 * URL to open. | |
130 * | |
131 * @return bool | |
132 * TRUE if the URL was successfully opened, FALSE otherwise. | |
133 */ | |
134 abstract protected function openSourceUrl($url); | |
135 | |
136 /** | |
137 * Retrieves the next row of data from the open source URL, populating | |
138 * currentItem. | |
139 */ | |
140 abstract protected function fetchNextRow(); | |
141 | |
142 /** | |
143 * Advances the data parser to the next source url. | |
144 * | |
145 * @return bool | |
146 * TRUE if a valid source URL was opened | |
147 */ | |
148 protected function nextSource() { | |
149 while ($this->activeUrl === NULL || (count($this->urls) - 1) > $this->activeUrl) { | |
150 if (is_null($this->activeUrl)) { | |
151 $this->activeUrl = 0; | |
152 } | |
153 else { | |
154 // Increment the activeUrl so we try to load the next source. | |
155 $this->activeUrl = $this->activeUrl + 1; | |
156 if ($this->activeUrl >= count($this->urls)) { | |
157 return FALSE; | |
158 } | |
159 } | |
160 | |
161 if ($this->openSourceUrl($this->urls[$this->activeUrl])) { | |
162 // We have a valid source. | |
163 return TRUE; | |
164 } | |
165 } | |
166 | |
167 return FALSE; | |
168 } | |
169 | |
170 /** | |
171 * {@inheritdoc} | |
172 */ | |
173 public function current() { | |
174 return $this->currentItem; | |
175 } | |
176 | |
177 /** | |
178 * {@inheritdoc} | |
179 */ | |
180 public function key() { | |
181 return $this->currentId; | |
182 } | |
183 | |
184 /** | |
185 * {@inheritdoc} | |
186 */ | |
187 public function valid() { | |
188 return !empty($this->currentItem); | |
189 } | |
190 | |
191 /** | |
192 * {@inheritdoc} | |
193 */ | |
194 public function count() { | |
195 $count = 0; | |
196 foreach ($this as $item) { | |
197 $count++; | |
198 } | |
199 return $count; | |
200 } | |
201 | |
202 /** | |
203 * Return the selectors used to populate each configured field. | |
204 * | |
205 * @return string[] | |
206 * Array of selectors, keyed by field name. | |
207 */ | |
208 protected function fieldSelectors() { | |
209 $fields = []; | |
210 foreach ($this->configuration['fields'] as $field_info) { | |
211 if (isset($field_info['selector'])) { | |
212 $fields[$field_info['name']] = $field_info['selector']; | |
213 } | |
214 } | |
215 return $fields; | |
216 } | |
217 | |
218 } |