ConventionsTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Core;
  16. use Cake\Utility\Inflector;
  17. /**
  18. * Provides methods that allow other classes access to conventions based inflections.
  19. */
  20. trait ConventionsTrait
  21. {
  22. /**
  23. * Creates a fixture name
  24. *
  25. * @param string $name Model class name
  26. * @return string Singular model key
  27. */
  28. protected function _fixtureName($name)
  29. {
  30. return Inflector::camelize($name);
  31. }
  32. /**
  33. * Creates the proper entity name (singular) for the specified name
  34. *
  35. * @param string $name Name
  36. * @return string Camelized and plural model name
  37. */
  38. protected function _entityName($name)
  39. {
  40. return Inflector::singularize(Inflector::camelize($name));
  41. }
  42. /**
  43. * Creates the proper underscored model key for associations
  44. *
  45. * If the input contains a dot, assume that the right side is the real table name.
  46. *
  47. * @param string $name Model class name
  48. * @return string Singular model key
  49. */
  50. protected function _modelKey($name)
  51. {
  52. list(, $name) = pluginSplit($name);
  53. return Inflector::underscore(Inflector::singularize($name)) . '_id';
  54. }
  55. /**
  56. * Creates the proper model name from a foreign key
  57. *
  58. * @param string $key Foreign key
  59. * @return string Model name
  60. */
  61. protected function _modelNameFromKey($key)
  62. {
  63. $key = str_replace('_id', '', $key);
  64. return Inflector::camelize(Inflector::pluralize($key));
  65. }
  66. /**
  67. * Creates the singular name for use in views.
  68. *
  69. * @param string $name Name to use
  70. * @return string Variable name
  71. */
  72. protected function _singularName($name)
  73. {
  74. return Inflector::variable(Inflector::singularize($name));
  75. }
  76. /**
  77. * Creates the plural variable name for views
  78. *
  79. * @param string $name Name to use
  80. * @return string Plural name for views
  81. */
  82. protected function _variableName($name)
  83. {
  84. return Inflector::variable($name);
  85. }
  86. /**
  87. * Creates the singular human name used in views
  88. *
  89. * @param string $name Controller name
  90. * @return string Singular human name
  91. */
  92. protected function _singularHumanName($name)
  93. {
  94. return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
  95. }
  96. /**
  97. * Creates a camelized version of $name
  98. *
  99. * @param string $name name
  100. * @return string Camelized name
  101. */
  102. protected function _camelize($name)
  103. {
  104. return Inflector::camelize($name);
  105. }
  106. /**
  107. * Creates the plural human name used in views
  108. *
  109. * @param string $name Controller name
  110. * @return string Plural human name
  111. */
  112. protected function _pluralHumanName($name)
  113. {
  114. return Inflector::humanize(Inflector::underscore($name));
  115. }
  116. /**
  117. * Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
  118. *
  119. * @param string $pluginName Name of the plugin you want ie. DebugKit
  120. * @return string path path to the correct plugin.
  121. */
  122. protected function _pluginPath($pluginName)
  123. {
  124. if (Plugin::isLoaded($pluginName)) {
  125. return Plugin::path($pluginName);
  126. }
  127. return current(App::path('Plugin')) . $pluginName . DIRECTORY_SEPARATOR;
  128. }
  129. /**
  130. * Return plugin's namespace
  131. *
  132. * @param string $pluginName Plugin name
  133. * @return string Plugin's namespace
  134. */
  135. protected function _pluginNamespace($pluginName)
  136. {
  137. return str_replace('/', '\\', $pluginName);
  138. }
  139. }