ba59cd54aa2931bd2df999c0921c7308020addd6
[yaffs-website] / web / core / modules / file / src / FileUsage / FileUsageBase.php
1 <?php
2
3 namespace Drupal\file\FileUsage;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\file\FileInterface;
7
8 /**
9  * Defines the base class for database file usage backend.
10  */
11 abstract class FileUsageBase implements FileUsageInterface {
12
13   /**
14    * The config factory.
15    *
16    * @var \Drupal\Core\Config\ConfigFactoryInterface
17    */
18   protected $configFactory;
19
20   /**
21    * Creates a FileUsageBase object.
22    *
23    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
24    *   (optional) The config factory. Defaults to NULL and will use
25    *   \Drupal::configFactory() instead.
26    *
27    * @deprecated The $config_factory parameter will become required in Drupal
28    *   9.0.0.
29    */
30   public function __construct(ConfigFactoryInterface $config_factory = NULL) {
31     $this->configFactory = $config_factory ?: \Drupal::configFactory();
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function add(FileInterface $file, $module, $type, $id, $count = 1) {
38     // Make sure that a used file is permanent.
39     if (!$file->isPermanent()) {
40       $file->setPermanent();
41       $file->save();
42     }
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $count = 1) {
49     // Do not actually mark files as temporary when the behavior is disabled.
50     if (!$this->configFactory->get('file.settings')->get('make_unused_managed_files_temporary')) {
51       return;
52     }
53     // If there are no more remaining usages of this file, mark it as temporary,
54     // which result in a delete through system_cron().
55     $usage = \Drupal::service('file.usage')->listUsage($file);
56     if (empty($usage)) {
57       $file->setTemporary();
58       $file->save();
59     }
60   }
61
62 }