Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Functions | Variables
pre_composer_update_2.3.php File Reference

Go to the source code of this file.

Functions

if(count($authFailed) > 0) catch(Exception $e) findUnusedFilename ($dir, $filename)
 
 runComposer ($command)
 
 deleteFilepath ($path)
 
 output ($string, $level=INFO, $label='')
 

Variables

 $_scriptName = basename(__FILE__)
 
const SYNOPSIS <<<SYNOPSIS Updates Magento with 2.3 requirements that can't be done by `composer update` or `bin/magento setup:upgrade`. Run this script after upgrading to PHP 7.1/7.2 and before running `composer update` or `bin/magento setup:upgrade`. Steps included: - Require new version of the metapackage - Update "require-dev" section - Add "Zend\\Mvc\\Controller\\": "setup/src/Zend/Mvc/Controller/" to composer.json "autoload":"psr-4" section - Update Magento/Updater if it's installed - Update name, version, and description fields in the root composer.json Usage: php -f $_scriptName -- --root='</path/to/magento/root/>' [--composer='</path/to/composer/executable>'] [--edition='<community|enterprise>'] [--repo='<composer_repo_url>'] [--version='<version_constraint>'] [--help] Required: --root='</path/to/magento/root/>' Path to the Magento installation root directory Optional: --composer='</path/to/composer/executable>' Path to the composer executable - Default: The composer found in the system PATH --edition='<community|enterprise>' Target Magento edition for the update. Open Source = 'community', Commerce = 'enterprise' - Default: The edition currently required in composer.json --repo='<composer_repo_url>' The Magento repository url to use to pull the new packages - Default: The Magento repository configured in composer.json --version='<version_constraint>' A composer version constraint for allowable 2.3 packages. Versions other than 2.3 are not handled by this script See https: - Default: The latest 2.3 version available in the Magento repository --help Display this message SYNOPSIS
 
 $opts
 
const INFO 0
 
const WARN 1
 
const ERROR 2
 
if(isset($opts['help'])) try
 
 $composerFile = "$rootDir/composer.json"
 
if(!file_exists($composerFile)) $composerData = json_decode(file_get_contents($composerFile), true)
 
 $metapackageMatcher = '/^magento\/product\-(?<edition>community|enterprise)\-edition$/'
 
foreach(array_keys($composerData['require']) as $requiredPackage) if(empty($edition)) if(!empty($opts['edition'])) $edition = strtolower($edition)
 
if($edition !=='community' && $edition !=='enterprise') $composerExec = (!empty($opts['composer']) ? $opts['composer'] : 'composer')
 
 $constraint = !empty($opts['version']) ? $opts['version'] : '2.3.*'
 
 $project = "magento/project-$edition-edition"
 
 $metapackage = "magento/product-$edition-edition"
 
 $mageUrls = []
 
 $authFailed = []
 
if(!empty($opts['repo'])) else
 
 $projectConstraint = "$project='$constraint'"
 
 $version = null
 
 $description = null
 
 $composerBackup = findUnusedFilename($rootDir, 'composer.json.bak')
 
 $repoUrls = array_map(function ($r) { return $r['url']; }, $composerData['repositories'])
 
 $composerData ['autoload']['psr-4']['Zend\\Mvc\\Controller\\'] = 'setup/src/Zend/Mvc/Controller/'
 
 $updateDir = "$rootDir/update"
 

Function Documentation

◆ deleteFilepath()

deleteFilepath (   $path)

Deletes a file or a directory and all its contents

Parameters
string$path
Exceptions
Exception

Definition at line 363 of file pre_composer_update_2.3.php.

365  {
366  if (!file_exists($path)) {
367  return;
368  }
369  if (is_dir($path)) {
370  $files = array_diff(scandir($path), array('..', '.'));
371  foreach ($files as $file) {
372  deleteFilepath("$path/$file");
373  }
374  rmdir($path);
375  }
376  else {
377  unlink($path);
378  }
379  if (file_exists($path)) {
380  throw new Exception("Failed to delete $path");
deleteFilepath($path)
foreach($appDirs as $dir) $files

◆ findUnusedFilename()

if (count( $authFailed) > 0) catch (Exception $e) findUnusedFilename (   $dir,
  $filename 
)

Gets a variant of a filename that doesn't already exist so we don't overwrite anything

Parameters
string$dir
string$filename
Returns
string

Definition at line 314 of file pre_composer_update_2.3.php.

316  {
317  $unique = "$dir/$filename";
318  if (file_exists($unique)) {
319  $unique = tempnam($dir, "$filename.");
320  unlink($unique);
321  }

◆ output()

output (   $string,
  $level = INFO,
  $label = '' 
)

Logs the given text with
newline replacement and log level formatting

Parameters
string$stringText to log
int$levelOne of INFO, WARN, or ERROR
string$labelOptional message label; defaults to WARNING for $level = WARN and ERROR for $level = ERROR

Definition at line 389 of file pre_composer_update_2.3.php.

391  {
392  $string = str_replace('\n', PHP_EOL, $string);
393 
394  if (!empty($label)) {
395  $label = "$label: ";
396  }
397  else if ($level == WARN) {
398  $label = 'WARNING: ';
399  }
400  else if ($level == ERROR) {
401  $label = 'ERROR: ';
402  }
403  $string = "$label$string";
404 
405  if ($level == WARN) {
406  error_log($string);
407  }
408  elseif ($level == ERROR) {
409  error_log(PHP_EOL . $string);
410  }
411  else {
412  echo $string . PHP_EOL;
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$label
Definition: details.phtml:21

◆ runComposer()

runComposer (   $command)

Execute a composer command, reload $composerData afterwards, and check for repo authentication warnings

Parameters
string$command
Returns
array Command output split by lines
Exceptions
RuntimeException

Definition at line 330 of file pre_composer_update_2.3.php.

333 {
335  $command = "$composerExec $command --no-interaction";
336  output(" Running command:\\n $command");
337  exec("$command 2>&1", $lines, $exitCode);
338  $output = ' ' . join('\n ', $lines);
339 
340  // Reload composer object from the updated composer.json
341  $composerData = json_decode(file_get_contents($composerFile), true);
342 
343  if (0 !== $exitCode) {
344  $output = "Error encountered running command:\\n $command\\n$output";
345  throw new RuntimeException($output, $exitCode);
346  }
347  output($output);
348 
349  if (strpos($output, 'URL required authentication.') !== false) {
350  preg_match("/'(https?:\/\/)?(?<url>[^\/']+)(\/[^']*)?' URL required authentication/", $output, $matches);
351  $authUrl = $matches['url'];
352  $authFailed[$authUrl] = 1;
353  output("Repository authentication failed; make sure '$authUrl' exists in auth.json", WARN);
354  }
355 
output($string, $level=INFO, $label='')
if($edition !=='community' && $edition !=='enterprise') $composerExec
exec($command, array &$output=null, &$return_var=null)
if(!file_exists($composerFile)) $composerData

Variable Documentation

◆ $_scriptName

$_scriptName = basename(__FILE__)

Definition at line 9 of file pre_composer_update_2.3.php.

◆ $authFailed

$authFailed = []

Definition at line 143 of file pre_composer_update_2.3.php.

◆ $composerBackup

$composerBackup = findUnusedFilename($rootDir, 'composer.json.bak')

Definition at line 202 of file pre_composer_update_2.3.php.

◆ $composerData [1/2]

if (!file_exists( $composerFile)) $composerData = json_decode(file_get_contents($composerFile), true)

Definition at line 91 of file pre_composer_update_2.3.php.

◆ $composerData [2/2]

$composerData[ 'autoload'][ 'psr-4'][ 'Zend\\Mvc\\Controller\\'] = 'setup/src/Zend/Mvc/Controller/'

Definition at line 244 of file pre_composer_update_2.3.php.

◆ $composerExec

if ( $composerFailed) $composerExec = (!empty($opts['composer']) ? $opts['composer'] : 'composer')

Definition at line 114 of file pre_composer_update_2.3.php.

◆ $composerFile

$composerFile = "$rootDir/composer.json"

Definition at line 86 of file pre_composer_update_2.3.php.

◆ $constraint

$constraint = !empty($opts['version']) ? $opts['version'] : '2.3.*'

Definition at line 135 of file pre_composer_update_2.3.php.

◆ $description

$description = null

Definition at line 162 of file pre_composer_update_2.3.php.

◆ $edition

foreach (array_keys( $composerData[ 'require']) as $requiredPackage) if (empty( $edition)) if (!empty( $opts[ 'edition'])) $edition = strtolower($edition)

Definition at line 108 of file pre_composer_update_2.3.php.

◆ $mageUrls

$mageUrls = []

Definition at line 142 of file pre_composer_update_2.3.php.

◆ $metapackage

$metapackage = "magento/product-$edition-edition"

Definition at line 139 of file pre_composer_update_2.3.php.

◆ $metapackageMatcher

$metapackageMatcher = '/^magento\/product\-(?<edition>community|enterprise)\-edition$/'

Definition at line 93 of file pre_composer_update_2.3.php.

◆ $opts

$opts
Initial value:
= getopt('', [
'root:',
'composer:',
'edition:',
'repo:',
'version:',
'help'
])

Definition at line 53 of file pre_composer_update_2.3.php.

◆ $project

$project = "magento/project-$edition-edition"

Definition at line 138 of file pre_composer_update_2.3.php.

◆ $projectConstraint

$projectConstraint = "$project='$constraint'"

Definition at line 160 of file pre_composer_update_2.3.php.

◆ $repoUrls

$repoUrls = array_map(function ($r) { return $r['url']; }, $composerData['repositories'])

Definition at line 207 of file pre_composer_update_2.3.php.

◆ $updateDir

$updateDir = "$rootDir/update"

Definition at line 256 of file pre_composer_update_2.3.php.

◆ $version

$version = null

Definition at line 161 of file pre_composer_update_2.3.php.

◆ else

if (!empty( $opts[ 'repo'])) else
Initial value:
{
foreach ($composerData['repositories'] as $label => $repo) {
if (strpos(strtolower($label), 'mage') !== false || strpos($repo['url'], '.mage') !== false) {
$mageUrls[] = $repo['url'];
}
}
if (count($mageUrls) == 0) {
throw new InvalidArgumentException('No Magento repository urls found in composer.json');
}
}
$tempDir = findUnusedFilename($rootDir, 'temp_project')
if(!file_exists($composerFile)) $composerData
if(count($authFailed) > 0) catch(Exception $e) findUnusedFilename($dir, $filename)
$label
Definition: details.phtml:21
$rootDir
Definition: website.php:12

Definition at line 147 of file pre_composer_update_2.3.php.

◆ ERROR

const ERROR 2

Definition at line 65 of file pre_composer_update_2.3.php.

◆ INFO

const INFO 0

Definition at line 63 of file pre_composer_update_2.3.php.

◆ SYNOPSIS

const SYNOPSIS <<<SYNOPSIS Updates Magento with 2.3 requirements that can't be done by `composer update` or `bin/magento setup:upgrade`. Run this script after upgrading to PHP 7.1/7.2 and before running `composer update` or `bin/magento setup:upgrade`. Steps included: - Require new version of the metapackage - Update "require-dev" section - Add "Zend\\Mvc\\Controller\\": "setup/src/Zend/Mvc/Controller/" to composer.json "autoload":"psr-4" section - Update Magento/Updater if it's installed - Update name, version, and description fields in the root composer.json Usage: php -f $_scriptName -- --root='</path/to/magento/root/>' [--composer='</path/to/composer/executable>'] [--edition='<community|enterprise>'] [--repo='<composer_repo_url>'] [--version='<version_constraint>'] [--help] Required: --root='</path/to/magento/root/>' Path to the Magento installation root directory Optional: --composer='</path/to/composer/executable>' Path to the composer executable - Default: The composer found in the system PATH --edition='<community|enterprise>' Target Magento edition for the update. Open Source = 'community', Commerce = 'enterprise' - Default: The edition currently required in composer.json --repo='<composer_repo_url>' The Magento repository url to use to pull the new packages - Default: The Magento repository configured in composer.json --version='<version_constraint>' A composer version constraint for allowable 2.3 packages. Versions other than 2.3 are not handled by this script See https: - Default: The latest 2.3 version available in the Magento repository --help Display this message SYNOPSIS

Definition at line 11 of file pre_composer_update_2.3.php.

◆ try

if (isset( $opts[ 'help'])) try
Initial value:
{
if (version_compare(PHP_VERSION, '7.1', '<') || version_compare(PHP_VERSION, '7.3', '>=')) {
preg_match('/^\d+\.\d+\.\d+/',PHP_VERSION, $matches);
$phpVersion = $matches[0];
throw new Exception("Invalid PHP version '$phpVersion'. Magento 2.3 requires PHP 7.1 or 7.2");
}
if (empty($opts['root']) || !is_dir($opts['root'])) {
throw new BadMethodCallException('Existing Magento root directory must be supplied with --root');
}
$rootDir = $opts['root']
$rootDir
Definition: website.php:12

Definition at line 72 of file pre_composer_update_2.3.php.

◆ WARN

const WARN 1

Definition at line 64 of file pre_composer_update_2.3.php.