Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
zf.php
Go to the documentation of this file.
1 <?php
32 class ZF
33 {
34 
38  protected $_clientLoaded = false;
39 
43  protected $_mode = 'runTool';
44 
48  protected $_messages = array();
49 
53  protected $_homeDirectory = null;
54 
58  protected $_storageDirectory = null;
59 
63  protected $_configFile = null;
64 
70  public static function main()
71  {
72  $zf = new self();
73  $zf->bootstrap();
74  $zf->run();
75  }
76 
82  public function bootstrap()
83  {
84  // detect settings
85  $this->_mode = $this->_detectMode();
86  $this->_homeDirectory = $this->_detectHomeDirectory();
87  $this->_storageDirectory = $this->_detectStorageDirectory();
88  $this->_configFile = $this->_detectConfigFile();
89 
90  // setup
91  $this->_setupPHPRuntime();
92  $this->_setupToolRuntime();
93  }
94 
100  public function run()
101  {
102  switch ($this->_mode) {
103  case 'runError':
104  $this->_runError();
105  $this->_runInfo();
106  break;
107  case 'runSetup':
108  if ($this->_runSetup() === false) {
109  $this->_runInfo();
110  }
111  break;
112  case 'runInfo':
113  $this->_runInfo();
114  break;
115  case 'runTool':
116  default:
117  $this->_runTool();
118  break;
119  }
120 
121  return $this;
122  }
123 
129  protected function _detectMode()
130  {
131  $arguments = $_SERVER['argv'];
132 
133  $mode = 'runTool';
134 
135  if (!isset($arguments[0])) {
136  return $mode;
137  }
138 
139  if ($arguments[0] == $_SERVER['PHP_SELF']) {
140  $this->_executable = array_shift($arguments);
141  }
142 
143  if (!isset($arguments[0])) {
144  return $mode;
145  }
146 
147  if ($arguments[0] == '--setup') {
148  $mode = 'runSetup';
149  } elseif ($arguments[0] == '--info') {
150  $mode = 'runInfo';
151  }
152 
153  return $mode;
154  }
155 
156 
164  protected function _detectHomeDirectory($mustExist = true, $returnMessages = true)
165  {
166  $homeDirectory = null;
167 
168  $homeDirectory = getenv('ZF_HOME'); // check env var ZF_HOME
169  if ($homeDirectory) {
170  $this->_logMessage('Home directory found in environment variable ZF_HOME with value ' . $homeDirectory, $returnMessages);
171  if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
172  return $homeDirectory;
173  } else {
174  $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
175  }
176  }
177 
178  $homeDirectory = getenv('HOME'); // HOME environment variable
179 
180  if ($homeDirectory) {
181  $this->_logMessage('Home directory found in environment variable HOME with value ' . $homeDirectory, $returnMessages);
182  if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
183  return $homeDirectory;
184  } else {
185  $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
186  }
187 
188  }
189 
190  $homeDirectory = getenv('HOMEPATH');
191 
192  if ($homeDirectory) {
193  $this->_logMessage('Home directory found in environment variable HOMEPATH with value ' . $homeDirectory, $returnMessages);
194  if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
195  return $homeDirectory;
196  } else {
197  $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
198  }
199  }
200 
201  $homeDirectory = getenv('USERPROFILE');
202 
203  if ($homeDirectory) {
204  $this->_logMessage('Home directory found in environment variable USERPROFILE with value ' . $homeDirectory, $returnMessages);
205  if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
206  return $homeDirectory;
207  } else {
208  $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
209  }
210  }
211 
212  return false;
213  }
214 
222  protected function _detectStorageDirectory($mustExist = true, $returnMessages = true)
223  {
224  $storageDirectory = false;
225 
226  $storageDirectory = getenv('ZF_STORAGE_DIR');
227  if ($storageDirectory) {
228  $this->_logMessage('Storage directory path found in environment variable ZF_STORAGE_DIR with value ' . $storageDirectory, $returnMessages);
229  if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
230  return $storageDirectory;
231  } else {
232  $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
233  }
234  }
235 
236  $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
237 
238  if ($homeDirectory) {
239  $storageDirectory = $homeDirectory . '/.zf/';
240  $this->_logMessage('Storage directory assumed in home directory at location ' . $storageDirectory, $returnMessages);
241  if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
242  return $storageDirectory;
243  } else {
244  $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
245  }
246  }
247 
248  return false;
249  }
250 
258  protected function _detectConfigFile($mustExist = true, $returnMessages = true)
259  {
260  $configFile = null;
261 
262  $configFile = getenv('ZF_CONFIG_FILE');
263  if ($configFile) {
264  $this->_logMessage('Config file found environment variable ZF_CONFIG_FILE at ' . $configFile, $returnMessages);
265  if (!$mustExist || ($mustExist && file_exists($configFile))) {
266  return $configFile;
267  } else {
268  $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
269  }
270  }
271 
272  $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
273  if ($homeDirectory) {
274  $configFile = $homeDirectory . '/.zf.ini';
275  $this->_logMessage('Config file assumed in home directory at location ' . $configFile, $returnMessages);
276  if (!$mustExist || ($mustExist && file_exists($configFile))) {
277  return $configFile;
278  } else {
279  $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
280  }
281  }
282 
283  $storageDirectory = ($this->_storageDirectory) ? $this->_storageDirectory : $this->_detectStorageDirectory(true, false);
284  if ($storageDirectory) {
285  $configFile = $storageDirectory . '/zf.ini';
286  $this->_logMessage('Config file assumed in storage directory at location ' . $configFile, $returnMessages);
287  if (!$mustExist || ($mustExist && file_exists($configFile))) {
288  return $configFile;
289  } else {
290  $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
291  }
292  }
293 
294  return false;
295  }
296 
297 
303  protected function _setupPHPRuntime()
304  {
305  // set php runtime settings
306  ini_set('display_errors', true);
307 
308  // support the changing of the current working directory, necessary for some providers
309  $cwd = getenv('ZEND_TOOL_CURRENT_WORKING_DIRECTORY');
310  if ($cwd != '' && realpath($cwd)) {
311  chdir($cwd);
312  }
313 
314  if (!$this->_configFile) {
315  return;
316  }
317  $zfINISettings = parse_ini_file($this->_configFile);
318  $phpINISettings = ini_get_all();
319  foreach ($zfINISettings as $zfINIKey => $zfINIValue) {
320  if (substr($zfINIKey, 0, 4) === 'php.') {
321  $phpINIKey = substr($zfINIKey, 4);
322  if (array_key_exists($phpINIKey, $phpINISettings)) {
323  ini_set($phpINIKey, $zfINIValue);
324  }
325  }
326  }
327  }
328 
335  protected function _setupToolRuntime()
336  {
337 
338  $includePathPrepend = getenv('ZEND_TOOL_INCLUDE_PATH_PREPEND');
339  $includePathFull = getenv('ZEND_TOOL_INCLUDE_PATH');
340 
341  // check if the user has not provided anything
342  if (!($includePathPrepend || $includePathFull)) {
343  if ($this->_tryClientLoad()) {
344  return;
345  }
346  }
347 
348  // if ZF is not in the include_path, but relative to this file, put it in the include_path
349  if ($includePathPrepend || $includePathFull) {
350  if (isset($includePathPrepend) && ($includePathPrepend !== false)) {
351  set_include_path($includePathPrepend . PATH_SEPARATOR . get_include_path());
352  } elseif (isset($includePathFull) && ($includePathFull !== false)) {
353  set_include_path($includePathFull);
354  }
355  }
356 
357  if ($this->_tryClientLoad()) {
358  return;
359  }
360 
361  $zfIncludePath['relativePath'] = dirname(__FILE__) . '/../library/';
362  if (file_exists($zfIncludePath['relativePath'] . 'Zend/Tool/Framework/Client/Console.php')) {
363  set_include_path(realpath($zfIncludePath['relativePath']) . PATH_SEPARATOR . get_include_path());
364  }
365 
366  if (!$this->_tryClientLoad()) {
367  $this->_mode = 'runError';
368  return;
369  }
370  }
371 
380  protected function _tryClientLoad()
381  {
382  $this->_clientLoaded = false;
383  $fh = @fopen('Zend/Tool/Framework/Client/Console.php', 'r', true);
384  if (!$fh) {
385  return $this->_clientLoaded; // false
386  } else {
387  fclose($fh);
388  unset($fh);
389  include 'Zend/Tool/Framework/Client/Console.php';
390  $this->_clientLoaded = class_exists('Zend_Tool_Framework_Client_Console');
391  }
392 
393  return $this->_clientLoaded;
394  }
395 
402  protected function _runError()
403  {
404 
405  echo <<<EOS
406 
407 ***************************** ZF ERROR ********************************
408 In order to run the zf command, you need to ensure that Zend Framework
409 is inside your include_path. There are a variety of ways that you can
410 ensure that this zf command line tool knows where the Zend Framework
411 library is on your system, but not all of them can be described here.
412 
413 The easiest way to get the zf command running is to give it the include
414 path via an environment variable ZEND_TOOL_INCLUDE_PATH or
415 ZEND_TOOL_INCLUDE_PATH_PREPEND with the proper include path to use,
416 then run the command "zf --setup". This command is designed to create
417 a storage location for your user, as well as create the zf.ini file
418 that the zf command will consult in order to run properly on your
419 system.
420 
421 Example you would run:
422 
423 $ ZEND_TOOL_INCLUDE_PATH=/path/to/library zf --setup
424 
425 Your are encourged to read more in the link that follows.
426 
427 EOS;
428 
429  }
430 
437  protected function _runInfo()
438  {
439  echo 'Zend_Tool & CLI Setup Information' . PHP_EOL
440  . '(available via the command line "zf --info")'
441  . PHP_EOL;
442 
443  echo ' * ' . implode(PHP_EOL . ' * ', $this->_messages) . PHP_EOL;
444 
445  echo PHP_EOL;
446 
447  echo 'To change the setup of this tool, run: "zf --setup"';
448 
449  echo PHP_EOL;
450 
451  }
452 
458  protected function _runSetup()
459  {
460  $setupCommand = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : null;
461 
462  switch ($setupCommand) {
463  case 'storage-directory':
464  $this->_runSetupStorageDirectory();
465  break;
466  case 'config-file':
467  $this->_runSetupConfigFile();
468  break;
469  default:
470  $this->_runSetupMoreInfo();
471  break;
472  }
473  }
474 
480  protected function _runSetupStorageDirectory()
481  {
482  $storageDirectory = $this->_detectStorageDirectory(false, false);
483 
484  if (file_exists($storageDirectory)) {
485  echo 'Directory already exists at ' . $storageDirectory . PHP_EOL
486  . 'Cannot create storage directory.';
487  return;
488  }
489 
490  mkdir($storageDirectory);
491 
492  echo 'Storage directory created at ' . $storageDirectory . PHP_EOL;
493  }
494 
500  protected function _runSetupConfigFile()
501  {
502  $configFile = $this->_detectConfigFile(false, false);
503 
504  if (file_exists($configFile)) {
505  echo 'File already exists at ' . $configFile . PHP_EOL
506  . 'Cannot write new config file.';
507  return;
508  }
509 
510  $includePath = get_include_path();
511 
512  $contents = 'php.include_path = "' . $includePath . '"';
513 
514  file_put_contents($configFile, $contents);
515 
516  $iniValues = ini_get_all();
517  if ($iniValues['include_path']['global_value'] != $iniValues['include_path']['local_value']) {
518  echo 'NOTE: the php include_path to be used with the tool has been written' . PHP_EOL
519  . 'to the config file, using ZEND_TOOL_INCLUDE_PATH (or other include_path setters)' . PHP_EOL
520  . 'is no longer necessary.' . PHP_EOL . PHP_EOL;
521  }
522 
523  echo 'Config file written to ' . $configFile . PHP_EOL;
524  }
525 
531  protected function _runSetupMoreInfo()
532  {
533  $homeDirectory = $this->_detectHomeDirectory(false, false);
534  $storageDirectory = $this->_detectStorageDirectory(false, false);
535  $configFile = $this->_detectConfigFile(false, false);
536 
537  echo <<<EOS
538 
539 ZF Command Line Tool - Setup
540 ----------------------------
541 
542 Current Paths (Existing or not):
543  Home Directory: {$homeDirectory}
544  Storage Directory: {$storageDirectory}
545  Config File: {$configFile}
546 
547 Important Environment Variables:
548  ZF_HOME
549  - the directory this tool will look for a home directory
550  - directory must exist
551  ZF_STORAGE_DIR
552  - where this tool will look for a storage directory
553  - directory must exist
554  ZF_CONFIG_FILE
555  - where this tool will look for a configuration file
556  ZF_TOOL_INCLUDE_PATH
557  - set the include_path for this tool to use this value
558  ZF_TOOL_INCLUDE_PATH_PREPEND
559  - prepend the current php.ini include_path with this value
560 
561 Search Order:
562  Home Directory:
563  - ZF_HOME, then HOME (*nix), then HOMEPATH (windows)
564  Storage Directory:
565  - ZF_STORAGE_DIR, then {home}/.zf/
566  Config File:
567  - ZF_CONFIG_FILE, then {home}/.zf.ini, then {home}/zf.ini,
568  then {storage}/zf.ini
569 
570 Commands:
571  zf --setup storage-directory
572  - setup the storage directory, directory will be created
573  zf --setup config-file
574  - create the config file with some default values
575 
576 
577 EOS;
578  }
579 
585  protected function _runTool()
586  {
587 
588  $configOptions = array();
589  if (isset($this->_configFile) && $this->_configFile) {
590  $configOptions['configOptions']['configFilepath'] = $this->_configFile;
591  }
592  if (isset($this->_storageDirectory) && $this->_storageDirectory) {
593  $configOptions['storageOptions']['directory'] = $this->_storageDirectory;
594  }
595 
596  // ensure that zf.php loads the Zend_Tool_Project features
597  $configOptions['classesToLoad'] = 'Zend_Tool_Project_Provider_Manifest';
598 
599  $console = new Zend_Tool_Framework_Client_Console($configOptions);
600  $console->dispatch();
601  }
602 
610  protected function _logMessage($message, $storeMessage = true)
611  {
612  if (!$storeMessage) {
613  return;
614  }
615 
616  $this->_messages[] = $message;
617  }
618 
619 
620 }
621 
622 if (!getenv('ZF_NO_MAIN')) {
623  ZF::main();
624 }
_runSetupStorageDirectory()
Definition: zf.php:480
_detectStorageDirectory($mustExist=true, $returnMessages=true)
Definition: zf.php:222
_runInfo()
Definition: zf.php:437
$contents
Definition: website.php:14
_detectHomeDirectory($mustExist=true, $returnMessages=true)
Definition: zf.php:164
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9
_tryClientLoad()
Definition: zf.php:380
ini_set($varName, $newValue)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_runSetupMoreInfo()
Definition: zf.php:531
$_messages
Definition: zf.php:48
$_mode
Definition: zf.php:43
_logMessage($message, $storeMessage=true)
Definition: zf.php:610
static main()
Definition: zf.php:70
_detectMode()
Definition: zf.php:129
defined('MTF_BOOT_FILE')||define('MTF_BOOT_FILE' __FILE__
Definition: bootstrap.php:7
bootstrap()
Definition: zf.php:82
_setupPHPRuntime()
Definition: zf.php:303
$message
run()
Definition: zf.php:100
_runSetup()
Definition: zf.php:458
_runSetupConfigFile()
Definition: zf.php:500
$_homeDirectory
Definition: zf.php:53
$_storageDirectory
Definition: zf.php:58
$_clientLoaded
Definition: zf.php:38
_runError()
Definition: zf.php:402
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
_setupToolRuntime()
Definition: zf.php:335
$_configFile
Definition: zf.php:63
taxRateField this edit on("click.mselect-delete", ".mselect-delete", function() { if(!confirm('<?=/*@escapeNotVerified */__( 'Do you really want to delete this tax rate?') ?>')) { return;} var that=$(this), select=that.closest('.mselect-list').prev(), rateValue=that.parent().find( 'input[type="checkbox"]').val();$( 'body').trigger( 'processStart');var ajaxOptions={ type:'POST', data:{ tax_calculation_rate_id:rateValue, form_key:$( 'input[name="form_key"]').val() }, dataType:'json', url:'<?=/*@escapeNotVerified */$block->getTaxRateDeleteUrl() ?>', success:function(result, status) { $( 'body').trigger( 'processStop');if(result.success) { that.parent().remove();select.find( 'option').each(function() { if(this.value===rateValue) { $(this).remove();} });select.trigger( 'change.hiddenSelect');} else { if(result.error_message) alert({ content:result.error_message });else alert({ content:'<?=/*@escapeNotVerified */__( 'An error occurred') ?>' });} }, error:function() { $( 'body').trigger( 'processStop');alert({ content:'<?=/*@escapeNotVerified */__( 'An error occurred') ?>' });} };$.ajax(ajaxOptions);}) .on( 'click.mselectAdd'
Definition: edit.phtml:164
$arguments
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25
_detectConfigFile($mustExist=true, $returnMessages=true)
Definition: zf.php:258
_runTool()
Definition: zf.php:585
Definition: zf.php:32