optimizer_service = $optimizer_service; } /** * Run a file through the AMP Optimizer. (EXPERIMENTAL) * * Note: The Optimizer CLI commands are to be considered experimental, as * the output they produce is currently not guaranteed to be consistent * with the corresponding output from the web server code path. * * ## OPTIONS * * [] * : Input file to run through the AMP Optimizer. Omit or use '-' to read from STDIN. * * ## EXAMPLES * * # Test SSR transformations and store them in a new file named 'output.html'. * $ echo '' | wp amp optimizer optimize > output.html * * @param array $args Array of positional arguments. * @param array $assoc_args Associative array of associative arguments. * @throws WP_CLI\ExitException If the requested file could not be read. */ public function optimize( $args, /** @noinspection PhpUnusedParameterInspection */ $assoc_args ) { $file = '-'; if ( count( $args ) > 0 ) { $file = array_shift( $args ); } if ( '-' !== $file && ( ! is_file( $file ) || ! is_readable( $file ) ) ) { WP_CLI::error( "Could not read file: '{$file}'." ); } if ( '-' === $file ) { $file = 'php://stdin'; } $html = file_get_contents( $file ); $errors = new ErrorCollection(); $optimized_html = $this->optimizer_service->optimizeHtml( $html, $errors ); WP_CLI::line( $optimized_html ); /** @var Error $error */ foreach ( $errors as $error ) { WP_CLI::warning( "[{$error->getCode()}] {$error->getMessage()}" ); } } }