FileCacheManager.php 2.84 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
<?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace PhpCsFixer\Cache;

/**
 * Class supports caching information about state of fixing files.
 *
 * Cache is supported only for phar version and version installed via composer.
 *
 * File will be processed by PHP CS Fixer only if any of the following conditions is fulfilled:
 *  - cache is corrupt
 *  - fixer version changed
 *  - rules changed
 *  - file is new
 *  - file changed
 *
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * @internal
 */
final class FileCacheManager implements CacheManagerInterface
{
    /**
     * @var FileHandlerInterface
     */
    private $handler;

    /**
     * @var SignatureInterface
     */
    private $signature;

    /**
     * @var CacheInterface
     */
    private $cache;

    /**
     * @var bool
     */
    private $isDryRun;

    /**
     * @var DirectoryInterface
     */
    private $cacheDirectory;

    /**
     * @param FileHandlerInterface    $handler
     * @param SignatureInterface      $signature
     * @param bool                    $isDryRun
     * @param null|DirectoryInterface $cacheDirectory
     */
    public function __construct(
        FileHandlerInterface $handler,
        SignatureInterface $signature,
        $isDryRun = false,
        DirectoryInterface $cacheDirectory = null
    ) {
        $this->handler = $handler;
        $this->signature = $signature;
        $this->isDryRun = $isDryRun;
        $this->cacheDirectory = $cacheDirectory ?: new Directory('');

        $this->readCache();
    }

    public function __destruct()
    {
        $this->writeCache();
    }

    public function needFixing($file, $fileContent)
    {
        $file = $this->cacheDirectory->getRelativePathTo($file);

        return !$this->cache->has($file) || $this->cache->get($file) !== $this->calcHash($fileContent);
    }

    public function setFile($file, $fileContent)
    {
        $file = $this->cacheDirectory->getRelativePathTo($file);

        $hash = $this->calcHash($fileContent);

        if ($this->isDryRun && $this->cache->has($file) && $this->cache->get($file) !== $hash) {
            $this->cache->clear($file);

            return;
        }

        $this->cache->set($file, $hash);
    }

    private function readCache()
    {
        $cache = $this->handler->read();

        if (!$cache || !$this->signature->equals($cache->getSignature())) {
            $cache = new Cache($this->signature);
        }

        $this->cache = $cache;
    }

    private function writeCache()
    {
        $this->handler->write($this->cache);
    }

    private function calcHash($content)
    {
        return crc32($content);
    }
}