public function run(): int
    {
        // If the cache file already exists, delete it
        if (file_exists(config()['cacheFilePath'])) {
            unlink(config()['cacheFilePath']);
        }

        app()->setup(
            [
                'app' => [
                    'debug' => false,
                    'env'   => 'production',
                ],
            ],
            true
        );

        $containerCache = container()->getCacheable();
        $consoleCache   = console()->getCacheable();
        $eventsCache    = events()->getCacheable();
        $routesCache    = router()->getCacheable();
        $configCache    = config();

        $configCache['cache']['container'] = $containerCache;
        $configCache['cache']['console']   = $consoleCache;
        $configCache['cache']['events']    = $eventsCache;
        $configCache['cache']['routing']   = $routesCache;

        $configCache['container']['useCache'] = true;
        $configCache['console']['useCache']   = true;
        $configCache['events']['useCache']    = true;
        $configCache['routing']['useCache']   = true;

        // Get the results of the cache attempt
        $result = file_put_contents(
            config()['cacheFilePath'],
            '<?php return ' . var_export($configCache, true) . ';',
            LOCK_EX
        );

        if ($result === false) {
            output()->writeMessage(
                'An error occurred while optimizing the application.',
                true
            );

            return 1;
        }

        output()->writeMessage('Application optimized successfully', true);

        return 0;
    }