diff --git a/core/lib/Drupal/Core/Batch/Batch.php b/core/lib/Drupal/Core/Batch/BatchBuilder.php similarity index 60% rename from core/lib/Drupal/Core/Batch/Batch.php rename to core/lib/Drupal/Core/Batch/BatchBuilder.php index b764dbc..99bf194 100644 --- a/core/lib/Drupal/Core/Batch/Batch.php +++ b/core/lib/Drupal/Core/Batch/BatchBuilder.php @@ -7,17 +7,17 @@ * * Example code to create a batch: * @code - * $batch = Batch::create() + * $batch_builder = (new BatchBuilder()) * ->setTitle('Batch Title') * ->setFinishCallback('batch_example_finished_callback') * ->setInitMessage('The initialization message (optional)'); - * for ($i = 0; $i < 1000; $i++) { - * $batch->addOperation('batch_example_callback', [$i + 1]); + * foreach ($ids as $id) { + * $batch_builder->addOperation('batch_example_callback', [$id]); * } - * $batch->enqueue(); + * $batch_builder->queue(); * @endcode */ -class Batch implements BatchInterface { +class BatchBuilder implements BatchBuilderInterface { /** * The set of operations to be processed. @@ -34,28 +34,28 @@ class Batch implements BatchInterface { * * @var string */ - protected $title; + protected $title = 'Processing'; /** * The initializing message for the batch. * * @var string */ - protected $initMessage; + protected $initMessage = 'Initializing.'; /** * The message to be shown while the batch is in progress. * * @var string */ - protected $progressMessage; + protected $progressMessage = 'Completed @current of @total.'; /** * The message to be shown if a problem occurs. * * @var string */ - protected $errorMessage; + protected $errorMessage = 'An error has occurred.'; /** * The name of a function / method to be called when the batch finishes. @@ -108,91 +108,6 @@ class Batch implements BatchInterface { protected $queue; /** - * Constructs a new Batch instance. - */ - public function __construct() { - $this->setTitle('Processing'); - $this->setInitMessage('Initializing.'); - $this->setProgressMessage('Completed @current of @total.'); - $this->setErrorMessage('An error has occurred.'); - } - - /** - * {@inheritdoc} - */ - public static function create() { - return new static(); - } - - /** - * {@inheritdoc} - */ - public static function createFromTitle($title) { - return static::create()->setTitle($title); - } - - /** - * {@inheritdoc} - */ - public static function createFromArray(array $batch_definition) { - $new_batch = static::create(); - - if (isset($batch_definition['operations'])) { - foreach ($batch_definition['operations'] as list($callback, $arguments)) { - $new_batch->addOperation($callback, $arguments); - } - } - - if (isset($batch_definition['title'])) { - $new_batch->setTitle($batch_definition['title']); - } - - if (isset($batch_definition['init_message'])) { - $new_batch->setInitMessage($batch_definition['init_message']); - } - - if (isset($batch_definition['progress_message'])) { - $new_batch->setProgressMessage($batch_definition['progress_message']); - } - - if (isset($batch_definition['error_message'])) { - $new_batch->setErrorMessage($batch_definition['error_message']); - } - - if (isset($batch_definition['finished'])) { - $new_batch->setFinishCallback($batch_definition['finished']); - } - - if (isset($batch_definition['file'])) { - $new_batch->setFile($batch_definition['file']); - } - - if (isset($batch_definition['library'])) { - $new_batch->addLibraries($batch_definition['library']); - } - - if (isset($batch_definition['url_options'])) { - $new_batch->setUrlOptions($batch_definition['url_options']); - } - - if (isset($batch_definition['progressive'])) { - $new_batch->setProgressive($batch_definition['progressive']); - } - - if ( - isset($batch_definition['queue']['name']) && - isset($batch_definition['queue']['class']) - ) { - $new_batch->setQueue( - $batch_definition['queue']['name'], - $batch_definition['queue']['class'] - ); - } - - return $new_batch; - } - - /** * {@inheritdoc} */ public function setTitle($title) { @@ -203,7 +118,7 @@ public function setTitle($title) { /** * {@inheritdoc} */ - public function setFinishCallback($callback) { + public function setFinishCallback(callable $callback) { $this->finished = $callback; return $this; } @@ -283,7 +198,7 @@ public function setQueue($name, $class) { /** * {@inheritdoc} */ - public function addOperation($callback, array $arguments = []) { + public function addOperation(callable $callback, array $arguments = []) { $this->operations[] = [$callback, $arguments]; return $this; } @@ -292,17 +207,16 @@ public function addOperation($callback, array $arguments = []) { * {@inheritdoc} */ public function addLibraries($libraries) { - if (!is_array($libraries)) { - $libraries = [$libraries]; - } - $this->libraries = $this->libraries + $libraries; + $this->libraries = array_unique( + array_merge($this->libraries, (array) $libraries) + ); return $this; } /** * {@inheritdoc} */ - public function enqueue() { + public function queue() { batch_set($this->toArray()); return $this; } diff --git a/core/lib/Drupal/Core/Batch/BatchInterface.php b/core/lib/Drupal/Core/Batch/BatchBuilderInterface.php similarity index 80% rename from core/lib/Drupal/Core/Batch/BatchInterface.php rename to core/lib/Drupal/Core/Batch/BatchBuilderInterface.php index 8355ba8..dbf4acb 100644 --- a/core/lib/Drupal/Core/Batch/BatchInterface.php +++ b/core/lib/Drupal/Core/Batch/BatchBuilderInterface.php @@ -2,37 +2,10 @@ namespace Drupal\Core\Batch; -interface BatchInterface { - - /** - * Static constructor for a batch process. - * - * @return \Drupal\Core\Batch\BatchInterface - * A new object. - */ - public static function create(); - - /** - * Static constructor for a batch process with a title. - * - * @param string $title - * The title. - * - * @return static - * A new object. - */ - public static function createFromTitle($title); - - /** - * Creates a \Drupal\Core\Batch\Batch object from an array. - * - * @param array $batch_definition - * An array of values to use for the new object. - * - * @return \Drupal\Core\Batch\BatchInterface - * A new initialized object. - */ - public static function createFromArray(array $batch_definition); +/** + * Defines an interface for batch builder classes. + */ +interface BatchBuilderInterface { /** * Sets the title. @@ -49,12 +22,12 @@ public function setTitle($title); * * This callback will be executed if the batch process is done. * - * @param string $callback + * @param callable $callback * The callback. * * @return $this */ - public function setFinishCallback($callback); + public function setFinishCallback(callable $callback); /** * Sets the displayed message while processing is initialized. @@ -159,14 +132,14 @@ public function setQueue($name, $class); /** * Adds a batch operation. * - * @param string $callback + * @param callable $callback * The name of the callback function. * @param array $arguments * An array of arguments to pass to the callback function. * * @return $this */ - public function addOperation($callback, array $arguments = []); + public function addOperation(callable $callback, array $arguments = []); /** * Adds libraries to be used on the process page. @@ -183,7 +156,7 @@ public function addLibraries($libraries); * * @return $this */ - public function enqueue(); + public function queue(); /** * Converts a \Drupal\Core\Batch\Batch object into an array.