Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
State.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class State implements StateInterface
12 {
16  protected $fileName = '.sample-data-state.flag';
17 
21  protected $filePath;
22 
26  protected $filesystem;
27 
32  {
33  $this->filesystem = $filesystem;
34  }
35 
39  public function hasError()
40  {
41  $isError = false;
42  $stream = $this->openStream('r');
43  if (!$stream) {
44  return $isError;
45  } elseif (strpos(trim($stream->read(400)), self::ERROR) !== false) {
46  $isError = true;
47  }
48  $this->closeStream($stream);
49  return $isError;
50  }
51 
55  public function setError()
56  {
57  if (!$this->hasError()) {
58  $this->writeStream(self::ERROR);
59  }
60  }
61 
65  public function isInstalled()
66  {
67  $isInstalled = false;
69  $stream = $this->openStream('r');
70  if (!$stream) {
71  return $isInstalled;
72  } else {
73  $state = trim($stream->read(400));
74  if (strpos($state, self::ERROR) !== false || strpos($state, self::INSTALLED) !== false) {
75  $isInstalled = true;
76  }
77  }
78  $this->closeStream($stream);
79  return $isInstalled;
80  }
81 
85  public function setInstalled()
86  {
87  if (!$this->isInstalled()) {
88  $this->writeStream(self::INSTALLED);
89  }
90  }
91 
95  public function clearState()
96  {
97  if ($this->openStream('w')) {
98  $this->writeStream('');
99  }
100  }
101 
105  protected function getStream()
106  {
107  if (!$stream = $this->openStream('w')) {
108  $stream = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->openFile($this->fileName);
109  }
110  return $stream;
111  }
112 
117  protected function openStream($mode = 'w')
118  {
120  $stream = false;
121  $directoryWrite = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
122  if ($directoryWrite->isExist($fileName)) {
123  $stream = $directoryWrite->openFile($fileName, $mode);
124  }
125  return $stream;
126  }
127 
133  protected function writeStream($data)
134  {
135  $stream = $this->getStream();
136  if ($stream === false) {
137  throw new \Exception(
138  'Please ensure that the ' . $this->fileName
139  . ' file exists in the var directory and is writable.'
140  );
141  }
142  $stream->write($data);
143  $this->closeStream($stream);
144  }
145 
152  protected function closeStream($stream)
153  {
154  if ($stream) {
155  $stream->close();
156  }
157  }
158 }
__construct(Filesystem $filesystem)
Definition: State.php:31
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15