]> _ Git - couzy.git/blob
cbeb60b800fb7f5088695f04941aa6c94c292676
[couzy.git] /
1 <?php
2 namespace Composer\Installers;
3
4 use Composer\DependencyResolver\Pool;
5 use Composer\Package\PackageInterface;
6 use Composer\Package\LinkConstraint\MultiConstraint;
7 use Composer\Package\LinkConstraint\VersionConstraint;
8
9 class CakePHPInstaller extends BaseInstaller
10 {
11     protected $locations = array(
12         'plugin' => 'Plugin/{$name}/',
13     );
14
15     /**
16      * Format package name to CamelCase
17      */
18     public function inflectPackageVars($vars)
19     {
20         if ($this->matchesCakeVersion('>=', '3.0.0')) {
21             return $vars;
22         }
23
24         $nameParts = explode('/', $vars['name']);
25         foreach ($nameParts as &$value) {
26             $value = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $value));
27             $value = str_replace(array('-', '_'), ' ', $value);
28             $value = str_replace(' ', '', ucwords($value));
29         }
30         $vars['name'] = implode('/', $nameParts);
31
32         return $vars;
33     }
34
35     /**
36      * Change the default plugin location when cakephp >= 3.0
37      */
38     public function getLocations()
39     {
40         if ($this->matchesCakeVersion('>=', '3.0.0')) {
41             $this->locations['plugin'] =  $this->composer->getConfig()->get('vendor-dir') . '/{$vendor}/{$name}/';
42         }
43         return $this->locations;
44     }
45
46     /**
47      * Check if CakePHP version matches against a version
48      *
49      * @param string $matcher
50      * @param string $version
51      * @return bool
52      */
53     protected function matchesCakeVersion($matcher, $version)
54     {
55         $repositoryManager = $this->composer->getRepositoryManager();
56         if ($repositoryManager) {
57             $repos = $repositoryManager->getLocalRepository();
58             if (!$repos) {
59                 return false;
60             }
61             $cake3 = new MultiConstraint(array(
62                 new VersionConstraint($matcher, $version),
63                 new VersionConstraint('!=', '9999999-dev'),
64             ));
65             $pool = new Pool('dev');
66             $pool->addRepository($repos);
67             $packages = $pool->whatProvides('cakephp/cakephp');
68             foreach ($packages as $package) {
69                 $installed = new VersionConstraint('=', $package->getVersion());
70                 if ($cake3->matches($installed)) {
71                     return true;
72                     break;
73                 }
74             }
75         }
76         return false;
77     }
78 }