Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryProcessor.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  const DELIMITER_CATEGORY = '/';
20 
25 
31  protected $categories = [];
32 
38  protected $categoriesCache = [];
39 
45  protected $categoryFactory;
46 
53  protected $failedCategories = [];
54 
59  public function __construct(
60  \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryColFactory,
61  \Magento\Catalog\Model\CategoryFactory $categoryFactory
62  ) {
63  $this->categoryColFactory = $categoryColFactory;
64  $this->categoryFactory = $categoryFactory;
65  $this->initCategories();
66  }
67 
73  protected function initCategories()
74  {
75  if (empty($this->categories)) {
76  $collection = $this->categoryColFactory->create();
77  $collection->addAttributeToSelect('name')
78  ->addAttributeToSelect('url_key')
79  ->addAttributeToSelect('url_path');
81  /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
82  foreach ($collection as $category) {
83  $structure = explode(self::DELIMITER_CATEGORY, $category->getPath());
84  $pathSize = count($structure);
85 
86  $this->categoriesCache[$category->getId()] = $category;
87  if ($pathSize > 1) {
88  $path = [];
89  for ($i = 1; $i < $pathSize; $i++) {
90  $name = $collection->getItemById((int)$structure[$i])->getName();
91  $path[] = $this->quoteDelimiter($name);
92  }
94  $index = $this->standardizeString(
95  implode(self::DELIMITER_CATEGORY, $path)
96  );
97  $this->categories[$index] = $category->getId();
98  }
99  }
100  }
101  return $this;
102  }
103 
111  protected function createCategory($name, $parentId)
112  {
114  $category = $this->categoryFactory->create();
115  if (!($parentCategory = $this->getCategoryById($parentId))) {
116  $parentCategory = $this->categoryFactory->create()->load($parentId);
117  }
118  $category->setPath($parentCategory->getPath());
119  $category->setParentId($parentId);
120  $category->setName($this->unquoteDelimiter($name));
121  $category->setIsActive(true);
122  $category->setIncludeInMenu(true);
123  $category->setAttributeSetId($category->getDefaultAttributeSetId());
124  $category->save();
125  $this->categoriesCache[$category->getId()] = $category;
126  return $category->getId();
127  }
128 
135  protected function upsertCategory($categoryPath)
136  {
138  $index = $this->standardizeString($categoryPath);
139 
140  if (!isset($this->categories[$index])) {
141  $pathParts = preg_split('~(?<!\\\)' . preg_quote(self::DELIMITER_CATEGORY, '~') . '~', $categoryPath);
143  $path = '';
144 
145  foreach ($pathParts as $pathPart) {
146  $path .= $this->standardizeString($pathPart);
147  if (!isset($this->categories[$path])) {
148  $this->categories[$path] = $this->createCategory($pathPart, $parentId);
149  }
150  $parentId = $this->categories[$path];
152  }
153  }
154 
155  return $this->categories[$index];
156  }
157 
165  public function upsertCategories($categoriesString, $categoriesSeparator)
166  {
167  $categoriesIds = [];
168  $categories = explode($categoriesSeparator, $categoriesString);
169 
170  foreach ($categories as $category) {
171  try {
172  $categoriesIds[] = $this->upsertCategory($category);
173  } catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
174  $this->addFailedCategory($category, $e);
175  }
176  }
177 
178  return $categoriesIds;
179  }
180 
189  private function addFailedCategory($category, $exception)
190  {
191  $this->failedCategories[] =
192  [
193  'category' => $category,
194  'exception' => $exception,
195  ];
196  return $this;
197  }
198 
205  public function getFailedCategories()
206  {
208  }
209 
216  public function clearFailedCategories()
217  {
218  $this->failedCategories = [];
219  return $this;
220  }
221 
229  public function getCategoryById($categoryId)
230  {
231  return $this->categoriesCache[$categoryId] ?? null;
232  }
233 
242  private function standardizeString($string)
243  {
244  return mb_strtolower($string);
245  }
246 
253  private function quoteDelimiter($string)
254  {
255  return str_replace(self::DELIMITER_CATEGORY, '\\' . self::DELIMITER_CATEGORY, $string);
256  }
257 
264  private function unquoteDelimiter($string)
265  {
266  return str_replace('\\' . self::DELIMITER_CATEGORY, self::DELIMITER_CATEGORY, $string);
267  }
268 }
__construct(\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryColFactory, \Magento\Catalog\Model\CategoryFactory $categoryFactory)
$i
Definition: gallery.phtml:31
$index
Definition: list.phtml:44
if(!isset($_GET['name'])) $name
Definition: log.php:14