README.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ============
  2. jmespath.php
  3. ============
  4. JMESPath (pronounced "jaymz path") allows you to declaratively specify how to
  5. extract elements from a JSON document. *jmespath.php* allows you to use
  6. JMESPath in PHP applications with PHP data structures. It requires PHP 7.2.5 or
  7. greater and can be installed through `Composer <http://getcomposer.org/doc/00-intro.md>`_
  8. using the ``mtdowling/jmespath.php`` package.
  9. .. code-block:: php
  10. require 'vendor/autoload.php';
  11. $expression = 'foo.*.baz';
  12. $data = [
  13. 'foo' => [
  14. 'bar' => ['baz' => 1],
  15. 'bam' => ['baz' => 2],
  16. 'boo' => ['baz' => 3]
  17. ]
  18. ];
  19. JmesPath\search($expression, $data);
  20. // Returns: [1, 2, 3]
  21. - `JMESPath Tutorial <http://jmespath.org/tutorial.html>`_
  22. - `JMESPath Grammar <http://jmespath.org/specification.html#grammar>`_
  23. - `JMESPath Python library <https://github.com/jmespath/jmespath.py>`_
  24. PHP Usage
  25. =========
  26. The ``JmesPath\search`` function can be used in most cases when using the
  27. library. This function utilizes a JMESPath runtime based on your environment.
  28. The runtime utilized can be configured using environment variables and may at
  29. some point in the future automatically utilize a C extension if available.
  30. .. code-block:: php
  31. $result = JmesPath\search($expression, $data);
  32. // or, if you require PSR-4 compliance.
  33. $result = JmesPath\Env::search($expression, $data);
  34. Runtimes
  35. --------
  36. jmespath.php utilizes *runtimes*. There are currently two runtimes:
  37. AstRuntime and CompilerRuntime.
  38. AstRuntime is utilized by ``JmesPath\search()`` and ``JmesPath\Env::search()``
  39. by default.
  40. AstRuntime
  41. ~~~~~~~~~~
  42. The AstRuntime will parse an expression, cache the resulting AST in memory,
  43. and interpret the AST using an external tree visitor. AstRuntime provides a
  44. good general approach for interpreting JMESPath expressions that have a low to
  45. moderate level of reuse.
  46. .. code-block:: php
  47. $runtime = new JmesPath\AstRuntime();
  48. $runtime('foo.bar', ['foo' => ['bar' => 'baz']]);
  49. // > 'baz'
  50. CompilerRuntime
  51. ~~~~~~~~~~~~~~~
  52. ``JmesPath\CompilerRuntime`` provides the most performance for
  53. applications that have a moderate to high level of reuse of JMESPath
  54. expressions. The CompilerRuntime will walk a JMESPath AST and emit PHP source
  55. code, resulting in anywhere from 7x to 60x speed improvements.
  56. Compiling JMESPath expressions to source code is a slower process than just
  57. walking and interpreting a JMESPath AST (via the AstRuntime). However,
  58. running the compiled JMESPath code results in much better performance than
  59. walking an AST. This essentially means that there is a warm-up period when
  60. using the ``CompilerRuntime``, but after the warm-up period, it will provide
  61. much better performance.
  62. Use the CompilerRuntime if you know that you will be executing JMESPath
  63. expressions more than once or if you can pre-compile JMESPath expressions
  64. before executing them (for example, server-side applications).
  65. .. code-block:: php
  66. // Note: The cache directory argument is optional.
  67. $runtime = new JmesPath\CompilerRuntime('/path/to/compile/folder');
  68. $runtime('foo.bar', ['foo' => ['bar' => 'baz']]);
  69. // > 'baz'
  70. Environment Variables
  71. ^^^^^^^^^^^^^^^^^^^^^
  72. You can utilize the CompilerRuntime in ``JmesPath\search()`` by setting
  73. the ``JP_PHP_COMPILE`` environment variable to "on" or to a directory
  74. on disk used to store cached expressions.
  75. Testing
  76. =======
  77. A comprehensive list of test cases can be found at
  78. https://github.com/jmespath/jmespath.php/tree/master/tests/compliance.
  79. These compliance tests are utilized by jmespath.php to ensure consistency with
  80. other implementations, and can serve as examples of the language.
  81. jmespath.php is tested using PHPUnit. In order to run the tests, you need to
  82. first install the dependencies using Composer as described in the *Installation*
  83. section. Next you just need to run the tests via make:
  84. .. code-block:: bash
  85. make test
  86. You can run a suite of performance tests as well:
  87. .. code-block:: bash
  88. make perf