Compiler.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Illuminate\View\Compilers;
  3. use InvalidArgumentException;
  4. use Illuminate\Filesystem\Filesystem;
  5. abstract class Compiler
  6. {
  7. /**
  8. * The Filesystem instance.
  9. *
  10. * @var \Illuminate\Filesystem\Filesystem
  11. */
  12. protected $files;
  13. /**
  14. * Get the cache path for the compiled views.
  15. *
  16. * @var string
  17. */
  18. protected $cachePath;
  19. /**
  20. * Create a new compiler instance.
  21. *
  22. * @param \Illuminate\Filesystem\Filesystem $files
  23. * @param string $cachePath
  24. * @return void
  25. *
  26. * @throws \InvalidArgumentException
  27. */
  28. public function __construct(Filesystem $files, $cachePath)
  29. {
  30. if (! $cachePath) {
  31. throw new InvalidArgumentException('Please provide a valid cache path.');
  32. }
  33. $this->files = $files;
  34. $this->cachePath = $cachePath;
  35. }
  36. /**
  37. * Get the path to the compiled version of a view.
  38. *
  39. * @param string $path
  40. * @return string
  41. */
  42. public function getCompiledPath($path)
  43. {
  44. return $this->cachePath.'/'.sha1($path).'.php';
  45. }
  46. /**
  47. * Determine if the view at the given path is expired.
  48. *
  49. * @param string $path
  50. * @return bool
  51. */
  52. public function isExpired($path)
  53. {
  54. $compiled = $this->getCompiledPath($path);
  55. // If the compiled file doesn't exist we will indicate that the view is expired
  56. // so that it can be re-compiled. Else, we will verify the last modification
  57. // of the views is less than the modification times of the compiled views.
  58. if (! $this->files->exists($compiled)) {
  59. return true;
  60. }
  61. return $this->files->lastModified($path) >=
  62. $this->files->lastModified($compiled);
  63. }
  64. }