Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • Configuration
  • Configurator
  • CoverallsConfiguration
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: namespace Contrib\Bundle\CoverallsV1Bundle\Config;
  3: 
  4: use Contrib\Component\File\Path;
  5: use Symfony\Component\Yaml\Yaml;
  6: use Symfony\Component\Config\Definition\Processor;
  7: use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  8: 
  9: /**
 10:  * Coveralls API configurator.
 11:  *
 12:  * @author Kitamura Satoshi <with.no.parachute@gmail.com>
 13:  */
 14: class Configurator
 15: {
 16:     // API
 17: 
 18:     /**
 19:      * Load configuration.
 20:      *
 21:      * @param  string                                                 $coverallsYmlPath Path to .coveralls.yml.
 22:      * @param  string                                                 $rootDir          Path to project root directory.
 23:      * @return \Contrib\Bundle\CoverallsV1Bundle\Config\Configuration
 24:      */
 25:     public function load($coverallsYmlPath, $rootDir)
 26:     {
 27:         $yml     = $this->parse($coverallsYmlPath);
 28:         $options = $this->process($yml);
 29: 
 30:         return $this->createConfiguration($options, $rootDir);
 31:     }
 32: 
 33:     // Internal method
 34: 
 35:     /**
 36:      * Parse .coveralls.yml.
 37:      *
 38:      * @param  string $coverallsYmlPath Path to .coveralls.yml.
 39:      * @return array
 40:      */
 41:     protected function parse($coverallsYmlPath)
 42:     {
 43:         $file = new Path();
 44:         $path = realpath($coverallsYmlPath);
 45: 
 46:         if ($file->isRealFileReadable($path)) {
 47:             $yml = Yaml::parse($path);
 48: 
 49:             return empty($yml) ? array() : $yml;
 50:         }
 51: 
 52:         return array();
 53:     }
 54: 
 55:     /**
 56:      * Process parsed configuration according to the configuration definition.
 57:      *
 58:      * @param  array $yml Parsed configuration.
 59:      * @return array
 60:      */
 61:     protected function process(array $yml)
 62:     {
 63:         $processor     = new Processor();
 64:         $configuration = new CoverallsConfiguration();
 65: 
 66:         return $processor->processConfiguration($configuration, array('coveralls' => $yml));
 67:     }
 68: 
 69:     /**
 70:      * Create coveralls configuration.
 71:      *
 72:      * @param  array                                                  $options Processed configuration.
 73:      * @param  string                                                 $rootDir Path to project root directory.
 74:      * @return \Contrib\Bundle\CoverallsV1Bundle\Config\Configuration
 75:      */
 76:     protected function createConfiguration(array $options, $rootDir)
 77:     {
 78:         $configuration = new Configuration();
 79:         $file          = new Path();
 80: 
 81:         $repoToken       = $options['repo_token'];
 82:         $repoSecretToken = $options['repo_secret_token'];
 83: 
 84:         return $configuration
 85:         ->setRepoToken($repoToken !== null ? $repoToken : $repoSecretToken)
 86:         ->setServiceName($options['service_name'])
 87:         // for PHP lib
 88:         ->setSrcDir($this->ensureSrcDir($options['src_dir'], $rootDir, $file))
 89:         ->setCloverXmlPaths($this->ensureCloverXmlPaths($options['coverage_clover'], $rootDir, $file))
 90:         ->setJsonPath($this->ensureJsonPath($options['json_path'], $rootDir, $file))
 91:         ->setExcludeNoStatements($options['exclude_no_stmt']);
 92:     }
 93: 
 94:     /**
 95:      * Ensure src_dir is valid.
 96:      *
 97:      * @param  string                        $option  src_dir option.
 98:      * @param  string                        $rootDir Path to project root directory.
 99:      * @param  Path                          $file    Path object.
100:      * @return string                        Valid src_dir.
101:      * @throws InvalidConfigurationException
102:      */
103:     protected function ensureSrcDir($option, $rootDir, Path $file)
104:     {
105:         // normalize
106:         $realpath = $file->getRealPath($option, $rootDir);
107: 
108:         // validate
109:         if (!$file->isRealDirExist($realpath)) {
110:             throw new InvalidConfigurationException('src directory is not found');
111:         }
112: 
113:         return $realpath;
114:     }
115: 
116:     /**
117:      * Ensure coverage_clover is valid.
118:      *
119:      * @param  string                        $option  coverage_clover option.
120:      * @param  string                        $rootDir Path to project root directory.
121:      * @param  Path                          $file    Path object.
122:      * @return array                         Valid Absolute pathes of coverage_clover.
123:      * @throws InvalidConfigurationException
124:      */
125:     protected function ensureCloverXmlPaths($option, $rootDir, Path $file)
126:     {
127:         if (is_array($option)) {
128:             return $this->getGlobPathsFromArrayOption($option, $rootDir, $file);
129:         }
130: 
131:         return $this->getGlobPathsFromStringOption($option, $rootDir, $file);
132:     }
133: 
134:     /**
135:      * Return absolute paths from glob path.
136:      *
137:      * @param  string                        $path Absolute path.
138:      * @return array                         Absolute paths.
139:      * @throws InvalidConfigurationException
140:      */
141:     protected function getGlobPaths($path)
142:     {
143:         $paths    = array();
144:         $iterator = new \GlobIterator($path);
145: 
146:         foreach ($iterator as $fileInfo) {
147:             /* @var $fileInfo \SplFileInfo */
148:             $paths[] = $fileInfo->getPathname();
149:         }
150: 
151:         // validate
152:         if (count($paths) === 0) {
153:             throw new InvalidConfigurationException('coverage_clover XML file is not readable');
154:         }
155: 
156:         return $paths;
157:     }
158: 
159:     /**
160:      * Return absolute paths from string option value.
161:      *
162:      * @param  string                        $option  coverage_clover option value.
163:      * @param  string                        $rootDir Path to project root directory.
164:      * @param  Path                          $file    Path object.
165:      * @return array                         Absolute pathes.
166:      * @throws InvalidConfigurationException
167:      */
168:     protected function getGlobPathsFromStringOption($option, $rootDir, Path $file)
169:     {
170:         if (!is_string($option)) {
171:             throw new InvalidConfigurationException('coverage_clover XML file is not readable');
172:         }
173: 
174:         // normalize
175:         $path = $file->toAbsolutePath($option, $rootDir);
176: 
177:         return $this->getGlobPaths($path);
178:     }
179: 
180:     /**
181:      * Return absolute paths from array option values.
182:      *
183:      * @param  array  $options coverage_clover option values.
184:      * @param  string $rootDir Path to project root directory.
185:      * @param  Path   $file    Path object.
186:      * @return array  Absolute pathes.
187:      */
188:     protected function getGlobPathsFromArrayOption(array $options, $rootDir, Path $file)
189:     {
190:         $paths = array();
191: 
192:         foreach ($options as $option) {
193:             $paths = array_merge($paths, $this->getGlobPathsFromStringOption($option, $rootDir, $file));
194:         }
195: 
196:         return $paths;
197:     }
198: 
199:     /**
200:      * Ensure json_path is valid.
201:      *
202:      * @param  string                        $option  json_path option.
203:      * @param  string                        $rootDir Path to project root directory.
204:      * @param  Path                          $file    Path object.
205:      * @return string                        Valid json_path.
206:      * @throws InvalidConfigurationException
207:      */
208:     protected function ensureJsonPath($option, $rootDir, Path $file)
209:     {
210:         // normalize
211:         $realpath = $file->getRealWritingFilePath($option, $rootDir);
212: 
213:         // validate file
214:         $realFilePath = $file->getRealPath($realpath, $rootDir);
215: 
216:         if ($realFilePath !== false && !$file->isRealFileWritable($realFilePath)) {
217:             throw new InvalidConfigurationException('json_path is not writable');
218:         }
219: 
220:         // validate parent dir
221:         $realDir = $file->getRealDir($realpath, $rootDir);
222: 
223:         if (!$file->isRealDirWritable($realDir)) {
224:             throw new InvalidConfigurationException('json_path is not writable');
225:         }
226: 
227:         return $realpath;
228:     }
229: }
230: 
php-coveralls API documentation generated by ApiGen 2.8.0