PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Doctrine/AuditLog/Listener/Microtime.php

https://bitbucket.org/MikaelEriksson/trimport-zend-db
PHP | 91 lines | 27 code | 6 blank | 58 comment | 1 complexity | 00eab698ded01580ca0d6b1cfaaf8c99 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. /**
  22. * Doctrine_AuditLog_Listener
  23. *
  24. * @package Doctrine
  25. * @subpackage AuditLog
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.org
  28. * @since 1.0
  29. * @version $Revision$
  30. * @author Lukas Smith <smith@pooteeweet.org>
  31. */
  32. class Doctrine_AuditLog_Listener_Microtime extends Doctrine_AuditLog_Listener
  33. {
  34. /**
  35. * The numher of digits to use from the float microtime value
  36. *
  37. * @var int
  38. */
  39. protected $accuracy = 10;
  40. /**
  41. * Instantiate AuditLog listener and set the Doctrine_AuditLog instance to the class
  42. *
  43. * @param Doctrine_AuditLog $auditLog
  44. * @return void
  45. */
  46. public function __construct(Doctrine_AuditLog $auditLog)
  47. {
  48. parent::__construct($auditLog);
  49. $version = $this->_auditLog->getOption('version');
  50. if (!empty($version['accuracy'])) {
  51. $this->accuracy = $version['accuracy'];
  52. }
  53. }
  54. /**
  55. * Get the initial version number for the audit log
  56. *
  57. * @param Doctrine_Record $record
  58. * @return integer $initialVersion
  59. */
  60. protected function _getInitialVersion(Doctrine_Record $record)
  61. {
  62. return $this->_microtime();
  63. }
  64. /**
  65. * Get the next version number for the audit log
  66. *
  67. * @param Doctrine_Record $record
  68. * @return integer $nextVersion
  69. */
  70. protected function _getNextVersion(Doctrine_Record $record)
  71. {
  72. return $this->_microtime();
  73. }
  74. /**
  75. * Compute a version out of microtime(true)
  76. *
  77. * @return string $version
  78. */
  79. protected function _microtime()
  80. {
  81. $version = microtime(true) - 1073741824; // 31 bits
  82. $version = str_replace('.', '', (string)$version);
  83. return substr($version, 0, $this->accuracy);
  84. }
  85. }