همین طور که می دانید لاراول بروزرسانی های منظمی دارد.حالا چه طور می توانیم وقتی دستور
را فراخوانی می کنیم ، نسخه جدید لاراول را نیز بررسی کنیم؟
کافی هست یک فایل به نام checkupdate در پوشه جاری پروژه یا در پوشه vendor/bin ایجاد کنید.
حالا کد زیر را در آن قرار دهید:
<?php
//check if file execute in console
if (php_sapi_name() !== 'cli') {
echo consoleMessage ("This script must be executed in the command line.","red").PHP_EOL;
exit;
}
echo consoleMessage ("Checking for installed laravel and latest laravel version ... "."\u{1F680}",'default').PHP_EOL.PHP_EOL;
if (!($laravel_version = getLaravelVersion()) || !($last_laravel_version = getNewVersion('laravel/framework'))) {
echo consoleMessage("Laravel version not found in composer.lock!",'red').PHP_EOL;
exit;
}
if ($laravel_version === $last_laravel_version) {
echo consoleMessage ("Laravel version is up to date "."\u{2713}","green").PHP_EOL;
} else {
echo consoleMessage ("Laravel version is not up to date","red").PHP_EOL;
echo consoleMessage ("Current version: ",'default').consoleMessage($laravel_version,'yellow').PHP_EOL;
echo consoleMessage ("Latest version: ",'default').consoleMessage($last_laravel_version,'yellow').PHP_EOL;
}
echo PHP_EOL.PHP_EOL;
/**
* Gets the version of the Laravel framework from the composer.lock file.
*
* @return string The version of Laravel or an empty string if not found.
*/
function getLaravelVersion(): string
{
$composerLockFile = getComposerLockFile();
if (!file_exists($composerLockFile)) {
echo consoleMessage ("composer.lock file not found!",'red').PHP_EOL;
exit;
}
$composerJsonData = json_decode(file_get_contents($composerLockFile), true);
if (isset($composerJsonData['require']['laravel/framework'])) {
// If the version is in the require section of the composer.lock file.
return standardizeVersion($composerJsonData['require']['laravel/framework']);
} elseif (isset($composerJsonData['packages'])) {
// If the version is in the packages section of the composer.lock file.
foreach ($composerJsonData['packages'] as $package) {
if ($package['name'] === 'laravel/framework') {
return standardizeVersion($package['version']);
}
}
}
return '';
}
/**
* Gets the latest version of the given package from packagist.org.
*
* @param string $packageName The name of the package to get the version of.
* @return string The latest version of the package or an empty string if not found.
*/
function getNewVersion(string $packageName = "laravel/framework"): string
{
$packagistUrl = "https://repo.packagist.org/p2/{$packageName}.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $packagistUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if (isset($data['packages'][$packageName]) && isset($data['packages'][$packageName][0]['version'])) {
return standardizeVersion($data['packages'][$packageName][0]['version']);
}
return '';
}
/**
* Standardize a version string by removing any non-numeric characters and keeping
* only the first fractional part (i.e. the minor version).
*
* @param string $version The version string to standardize.
*
* @return string The standardized version string.
*/
function standardizeVersion(string $version): string
{
return (string) filter_var($version, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
}
/**
* Print a colored message to the console.
*
* @param string $str The message to print.
* @param string $color The color of the message. Defaults to 'green'.
*
* @return string The colored message.
*/
function consoleMessage(string $str, string $color = 'green'): string
{
$colors = [
'green' => "\033[0;32m",
'red' => "\033[0;31m",
'blue' => "\033[0;34m",
'yellow' => "\033[0;33m",
'black' => "\033[0;30m",
'default' => "\033[0m",
];
return $colors[$color] . $str . "\033[0m";
}
/**
* Checks if the current script is executed from the "vendor/bin" directory.
*
* @return bool true if the script is executed from "vendor/bin", false otherwise.
*/
function checkExecutedFromVendor(): bool
{
return strpos(__DIR__, 'vendor' . DIRECTORY_SEPARATOR . 'bin') !== false;
}
/**
* Gets the path to the composer.lock file.
*
* If the script is executed from the "vendor/bin" directory, it returns the path
* to the composer.lock file in the project root. Otherwise, it returns the path
* to the composer.lock file in the same directory as the script.
*
* @return string The path to the composer.lock file.
*/
function getComposerLockFile(): string
{
return checkExecutedFromVendor()
? dirname(__DIR__, 2) . DIRECTORY_SEPARATOR.'composer.lock'
: __DIR__ .DIRECTORY_SEPARATOR. 'composer.lock';
}
فایل را ذخیره کنید.
حالا فایل composer.json را باز کنید و در بخش scripts و قسمت post-update-cmd دستور زیر را اضافه کنید:
"@php ./vendor/bin/checkupdate"
یعنی چیزی شبیه این :
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force",
"@php ./vendor/bin/checkupdate"
],
حالا دستور زیر را بزنید
composer update
همچنین می توانید به صورت مستقیم این کار را انجام دهید:
(اگر در پوشه اصلی پروژه ترمینال را باز کنید)
php "./vendor/bin/checkupdate"
باید خروجی زیر را مشاهده کنید
> @php ./vendor/bin/checkupdate Checking for installed laravel and latest laravel version ... 🚀 Laravel version is not up to date Current version: 10.48.22 Latest version: 11.23.5
امید وارم لذت برده باشید.
صفحه گیت هاب من و راه های تماس با من: