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

/vendor/laravel/framework/src/Illuminate/Workbench/PackageCreator.php

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 373 lines | 147 code | 48 blank | 178 comment | 5 complexity | 5fb5c62edabf8a12ea256a77dcc6e9c4 MD5 | raw file
  1. <?php namespace Illuminate\Workbench;
  2. use Illuminate\Filesystem\Filesystem;
  3. class PackageCreator {
  4. /**
  5. * The filesystem instance.
  6. *
  7. * @var \Illuminate\Filesystem\Filesystem
  8. */
  9. protected $files;
  10. /**
  11. * The basic building blocks of the package.
  12. *
  13. * @param array
  14. */
  15. protected $basicBlocks = array(
  16. 'SupportFiles',
  17. 'TestDirectory',
  18. 'ServiceProvider',
  19. );
  20. /**
  21. * The building blocks of the package.
  22. *
  23. * @param array
  24. */
  25. protected $blocks = array(
  26. 'SupportFiles',
  27. 'SupportDirectories',
  28. 'PublicDirectory',
  29. 'TestDirectory',
  30. 'ServiceProvider',
  31. );
  32. /**
  33. * Create a new package creator instance.
  34. *
  35. * @param \Illuminate\Filesystem\Filesystem $files
  36. * @return void
  37. */
  38. public function __construct(Filesystem $files)
  39. {
  40. $this->files = $files;
  41. }
  42. /**
  43. * Create a new package stub.
  44. *
  45. * @param \Illuminate\Workbench\Package $package
  46. * @param string $path
  47. * @param bool $plain
  48. * @return string
  49. */
  50. public function create(Package $package, $path, $plain = true)
  51. {
  52. $directory = $this->createDirectory($package, $path);
  53. // To create the package, we will spin through a list of building blocks that
  54. // make up each package. We'll then call the method to build that block on
  55. // the class, which keeps the actual building of stuff nice and cleaned.
  56. foreach ($this->getBlocks($plain) as $block)
  57. {
  58. $this->{"write{$block}"}($package, $directory, $plain);
  59. }
  60. return $directory;
  61. }
  62. /**
  63. * Create a package with all resource directories.
  64. *
  65. * @param Package $package
  66. * @param string $path
  67. * @return void
  68. */
  69. public function createWithResources(Package $package, $path)
  70. {
  71. return $this->create($package, $path, false);
  72. }
  73. /**
  74. * Get the blocks for a given package.
  75. *
  76. * @param bool $plain
  77. * @return array
  78. */
  79. protected function getBlocks($plain)
  80. {
  81. return $plain ? $this->basicBlocks : $this->blocks;
  82. }
  83. /**
  84. * Write the support files to the package root.
  85. *
  86. * @param \Illuminate\Workbench\Package $package
  87. * @param string $directory
  88. * @return void
  89. */
  90. public function writeSupportFiles(Package $package, $directory, $plain)
  91. {
  92. foreach (array('PhpUnit', 'Travis', 'Composer', 'Ignore') as $file)
  93. {
  94. $this->{"write{$file}File"}($package, $directory, $plain);
  95. }
  96. }
  97. /**
  98. * Write the PHPUnit stub file.
  99. *
  100. * @param \Illuminate\Workbench\Package $package
  101. * @param string $directory
  102. * @return void
  103. */
  104. protected function writePhpUnitFile(Package $package, $directory)
  105. {
  106. $stub = __DIR__.'/stubs/phpunit.xml';
  107. $this->files->copy($stub, $directory.'/phpunit.xml');
  108. }
  109. /**
  110. * Write the Travis stub file.
  111. *
  112. * @param \Illuminate\Workbench\Package $package
  113. * @param string $directory
  114. * @return void
  115. */
  116. protected function writeTravisFile(Package $package, $directory)
  117. {
  118. $stub = __DIR__.'/stubs/.travis.yml';
  119. $this->files->copy($stub, $directory.'/.travis.yml');
  120. }
  121. /**
  122. * Write the Composer.json stub file.
  123. *
  124. * @param \Illuminate\Workbench\Package $package
  125. * @param string $directory
  126. * @return void
  127. */
  128. protected function writeComposerFile(Package $package, $directory, $plain)
  129. {
  130. $stub = $this->getComposerStub($plain);
  131. $stub = $this->formatPackageStub($package, $stub);
  132. $this->files->put($directory.'/composer.json', $stub);
  133. }
  134. /**
  135. * Get the Composer.json stub file contents.
  136. *
  137. * @param bool $plain
  138. * @return string
  139. */
  140. protected function getComposerStub($plain)
  141. {
  142. if ($plain) return $this->files->get(__DIR__.'/stubs/plain.composer.json');
  143. return $this->files->get(__DIR__.'/stubs/composer.json');
  144. }
  145. /**
  146. * Write the stub .gitignore file for the package.
  147. *
  148. * @param \Illuminate\Workbench\Package $package
  149. * @param string $directory
  150. * @return void
  151. */
  152. public function writeIgnoreFile(Package $package, $directory, $plain)
  153. {
  154. $this->files->copy(__DIR__.'/stubs/gitignore.txt', $directory.'/.gitignore');
  155. }
  156. /**
  157. * Create the support directories for a package.
  158. *
  159. * @param \Illuminate\Workbench\Package $package
  160. * @param string $directory
  161. * @return void
  162. */
  163. public function writeSupportDirectories(Package $package, $directory)
  164. {
  165. foreach (array('config', 'controllers', 'lang', 'migrations', 'views') as $support)
  166. {
  167. $this->writeSupportDirectory($package, $support, $directory);
  168. }
  169. }
  170. /**
  171. * Write a specific support directory for the package.
  172. *
  173. * @param \Illuminate\Workbench\Package $package
  174. * @param string $support
  175. * @param string $directory
  176. * @return void
  177. */
  178. protected function writeSupportDirectory(Package $package, $support, $directory)
  179. {
  180. // Once we create the source directory, we will write an empty file to the
  181. // directory so that it will be kept in source control allowing the dev
  182. // to go ahead and push these components to Github right on creation.
  183. $path = $directory.'/src/'.$support;
  184. $this->files->makeDirectory($path, 0777, true);
  185. $this->files->put($path.'/.gitkeep', '');
  186. }
  187. /**
  188. * Create the public directory for the package.
  189. *
  190. * @param \Illuminate\Workbench\Package $package
  191. * @param string $directory
  192. * @return void
  193. */
  194. public function writePublicDirectory(Package $package, $directory, $plain)
  195. {
  196. if ($plain) return;
  197. $this->files->makeDirectory($directory.'/public');
  198. $this->files->put($directory.'/public/.gitkeep', '');
  199. }
  200. /**
  201. * Create the test directory for the package.
  202. *
  203. * @param \Illuminate\Workbench\Package $package
  204. * @param string $directory
  205. * @return void
  206. */
  207. public function writeTestDirectory(Package $package, $directory)
  208. {
  209. $this->files->makeDirectory($directory.'/tests');
  210. $this->files->put($directory.'/tests/.gitkeep', '');
  211. }
  212. /**
  213. * Write the stub ServiceProvider for the package.
  214. *
  215. * @param \Illuminate\Workbench\Package $package
  216. * @param string $directory
  217. * @return void
  218. */
  219. public function writeServiceProvider(Package $package, $directory, $plain)
  220. {
  221. // Once we have the service provider stub, we will need to format it and make
  222. // the necessary replacements to the class, namespaces, etc. Then we'll be
  223. // able to write it out into the package's workbench directory for them.
  224. $stub = $this->getProviderStub($package, $plain);
  225. $this->writeProviderStub($package, $directory, $stub);
  226. }
  227. /**
  228. * Write the service provider stub for the package.
  229. *
  230. * @param \Illuminate\Workbench\Package $package
  231. * @param string $directory
  232. * @param string $stub
  233. * @return void
  234. */
  235. protected function writeProviderStub(Package $package, $directory, $stub)
  236. {
  237. $path = $this->createClassDirectory($package, $directory);
  238. // The primary source directory where the package's classes will live may not
  239. // exist yet, so we will need to create it before we write these providers
  240. // out to that location. We'll go ahead and create now here before then.
  241. $file = $path.'/'.$package->name.'ServiceProvider.php';
  242. $this->files->put($file, $stub);
  243. }
  244. /**
  245. * Get the stub for a ServiceProvider.
  246. *
  247. * @param \Illuminate\Workbench\Package $package
  248. * @param bool $plain
  249. * @return string
  250. */
  251. protected function getProviderStub(Package $package, $plain)
  252. {
  253. return $this->formatPackageStub($package, $this->getProviderFile($plain));
  254. }
  255. /**
  256. * Load the raw service provider file.
  257. *
  258. * @param bool $plain
  259. * @return string
  260. */
  261. protected function getProviderFile($plain)
  262. {
  263. if ($plain)
  264. {
  265. return $this->files->get(__DIR__.'/stubs/plain.provider.stub');
  266. }
  267. else
  268. {
  269. return $this->files->get(__DIR__.'/stubs/provider.stub');
  270. }
  271. }
  272. /**
  273. * Create the main source directory for the package.
  274. *
  275. * @param \Illuminate\Workbench\Package $package
  276. * @param string $directory
  277. * @return string
  278. */
  279. protected function createClassDirectory(Package $package, $directory)
  280. {
  281. $path = $directory.'/src/'.$package->vendor.'/'.$package->name;
  282. if ( ! $this->files->isDirectory($path))
  283. {
  284. $this->files->makeDirectory($path, 0777, true);
  285. }
  286. return $path;
  287. }
  288. /**
  289. * Format a generic package stub file.
  290. *
  291. * @param \Illuminate\Workbench\Package $package
  292. * @param string $stub
  293. * @return string
  294. */
  295. protected function formatPackageStub(Package $package, $stub)
  296. {
  297. foreach (get_object_vars($package) as $key => $value)
  298. {
  299. $stub = str_replace('{{'.snake_case($key).'}}', $value, $stub);
  300. }
  301. return $stub;
  302. }
  303. /**
  304. * Create a workbench directory for the package.
  305. *
  306. * @param \Illuminate\Workbench\Package $package
  307. * @param string $path
  308. * @return string
  309. *
  310. * @throws \InvalidArgumentException
  311. */
  312. protected function createDirectory(Package $package, $path)
  313. {
  314. $fullPath = $path.'/'.$package->getFullName();
  315. // If the directory doesn't exist, we will go ahead and create the package
  316. // directory in the workbench location. We will use this entire package
  317. // name when creating the directory to avoid any potential conflicts.
  318. if ( ! $this->files->isDirectory($fullPath))
  319. {
  320. $this->files->makeDirectory($fullPath, 0777, true);
  321. return $fullPath;
  322. }
  323. throw new \InvalidArgumentException("Package exists.");
  324. }
  325. }