Repository: laravel/framework (php)

Issue:
Using a custom FormRequest always modify the language of \Locale class from intl PHP extension
<!-- DO NOT THROW THIS AWAY -->
<!-- Fill out the FULL versions with patch versions -->

- Laravel Version: 9.21.0
- PHP Version: 8.1
- Database Driver & Version: MySQL / N/A

### Description:
We are using the intl PHP extension to localize dates in our views. Since the [pull request #42080](https://github.com/laravel/framework/pull/42080) was merged, the selected locale is always overriden by the framework when we use a custom `FormRequest` in the controller. The problem is not present if we use the default `Request` instead of a custom one.

We are setting the locale of the extension in the `app/Providers/AppServiceProvider.php`, in the `boot` method like this:

```php
    public function boot(): void
    {
        \Locale::setDefault(\App::getLocale());
    }
```

When the custom request is created from the base request, the default language is set in the file `src/Illuminate/Http/Request.php`, line 426:
```php
    $request->setDefaultLocale($from->getDefaultLocale());
```
and because the locale is null, the default Locale is also modified (line 1335):
```php
    /**
     * Sets the default locale.
     */
    public function setDefaultLocale(string $locale)
    {
        $this->defaultLocale = $locale;

        if (null === $this->locale) {
            $this->setPhpDefaultLocale($locale);
        }
    }
```
line 1869 of the Symfony vendor file `symfony/http-foundation/Request.php`:
```php
    private function setPhpDefaultLocale(string $locale): void
    {
        // if either the class Locale doesn't exist, or an exception is thrown when
        // setting the default locale, the intl module is not installed, and
        // the call can be ignored:
        try {
            if (class_exists(\Locale::class, false)) {
                \Locale::setDefault($locale);
            }
        } catch (\Exception) {
        }
    }
```

### Steps To Reproduce:
As requested, here is a repo that replicate the issue: https://github.com/faeby/laravel-issue-43371

1. create a new laravel project
2. set the default language in french in app.php
```php
    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'fr',
```
4. set the intl locale in the default locale of the app in the app service provider (shown above) 
```php
    public function boot(): void
    {
        \Locale::setDefault(\App::getLocale());
    }
```
5. create a custom request with the command `php artisan make:request ShouldBeInFrench` and authorize every to access it
6. add the followings routes in `routes/web.php`
```php
Route::get('/requests/custom', function (\App\Http\Requests\ShouldBeInFrench $request) {
    dump("INTL locale: " . \Locale::getDefault());
    return "I'm the custom request. <a href='/requests/base'>Go to base request</a>";
});

Route::get('/requests/base', function (\Illuminate\Http\Request $request) {
    dump("INTL locale: " . \Locale::getDefault());
    return "I'm the original request. <a href='/requests/custom'>Go to custom request</a>";
});
```
7. serve the app and navigate to the given URLs, and the intl locale shown is different
![image](https://user-images.githubusercontent.com/7632591/180475428-b5bf71de-04a0-4b83-b9ba-d5008feed638.png)
![image](https://user-images.githubusercontent.com/7632591/180475483-b2be0de5-9f76-40e6-a852-aaab3c68f095.png)

<!-- If possible, please provide a GitHub repository to demonstrate your issue -->

<!-- laravel new bug-report --github="--public" -->

### Workaround:
As a workaround, we are manually setting the request default locale in the `AppServiceprovider` boot method. This is not perfect but we did not found a way to configure the request locale in a better way.
```php
    public function boot()
    {
        // Functional workaround.
        $this->app->afterResolving(FormRequest::class, function ($request, $app) {
            $app['request']->setDefaultLocale(App::getLocale());
        });

        \Locale::setDefault(App::getLocale());
    }
```

Pull request:
[9.x] Fix overriding global locale
This PR fixes a bug where the global Locale value would be overwritten by calling setLocale. Underneath this method, Symfony sets the global Locale. Since we're not initializing the original request of the application but 
rather copying an existing request, we need to prevent from overwriting the default PHP locale. We can achieve this by just setting the plain locale and plain default locale without modifying the PHP locale. I've added new methods to achieve this so there's no side effects.

Fixes #43371


Patch:
diff --git a/src/Illuminate/Console/View/Components/Factory.php b/src/Illuminate/Console/View/Components/Factory.php
index 4331304f34..3e2e998540 100644
--- a/src/Illuminate/Console/View/Components/Factory.php
+++ b/src/Illuminate/Console/View/Components/Factory.php
@@ -13,7 +13,7 @@
  * @method void info(string $string, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL)
  * @method void line(string $style, string $string, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL)
  * @method void task(string $description, ?callable $task = null, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL)
- * @method void twoColumnDetail(string $first, ?string $second = null, int $verbosity = OutputInterface::VERBOSITY_NORMAL)
+ * @method void twoColumnDetail(string $first, ?string $second = null, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL)
  * @method void warn(string $string, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL)
  */
 class Factory
diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php
index 5834657500..6e0c2eddc0 100644
--- a/src/Illuminate/Http/Request.php
+++ b/src/Illuminate/Http/Request.php
@@ -451,9 +451,9 @@ public static function createFrom(self $from, $to = null)
 
         $request->headers->replace($from->headers->all());
 
-        $request->setLocale($from->getLocale());
+        $request->setRequestLocale($from->getLocale());
 
-        $request->setDefaultLocale($from->getDefaultLocale());
+        $request->setDefaultRequestLocale($from->getDefaultLocale());
 
         $request->setJson($from->json());
 
@@ -572,6 +572,28 @@ public function setLaravelSession($session)
         $this->session = $session;
     }
 
+    /**
+     * Set the locale for the request instance.
+     *
+     * @param  string  $locale
+     * @return void
+     */
+    public function setRequestLocale(string $locale)
+    {
+        $this->locale = $locale;
+    }
+
+    /**
+     * Set the default locale for the request instance.
+     *
+     * @param  string  $locale
+     * @return void
+     */
+    public function setDefaultRequestLocale(string $locale)
+    {
+        $this->defaultLocale = $locale;
+    }
+
     /**
      * Get the user making the request.
      *


Test patch:
diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php
index 0153b69851..04d75d8822 100755
--- a/tests/Support/SupportCollectionTest.php
+++ b/tests/Support/SupportCollectionTest.php
@@ -1161,6 +1161,15 @@ public function testValues($collection)
         })->values()->all());
     }
 
+    /**
+     * @dataProvider collectionClassProvider
+     */
+    public function testValuesResetKey($collection)
+    {
+        $data = new $collection([1 => 'a', 2 => 'b', 3 => 'c']);
+        $this->assertEquals([0 => 'a', 1 => 'b', 2 => 'c'], $data->values()->all());
+    }
+
     /**
      * @dataProvider collectionClassProvider
      */