Development

/branches/1.3/lib/task/project/upgrade1.3/sfYamlUpgrade.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/task/project/upgrade1.3/sfYamlUpgrade.class.php

Revision 28988, 3.2 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3] fixed upgrade of yaml booleans followed by inline comments (closes #8342)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Lists YAML files that use the deprecated Boolean notations.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfYamlUpgrade extends sfUpgrade
20 {
21   public function upgrade()
22   {
23     $specVersion = sfYaml::getSpecVersion();
24
25     $queue = array();
26     $success = true;
27
28     $finder = sfFinder::type('file')->name('*.yml')->prune('vendor');
29     foreach ($finder->in(sfConfig::get('sf_root_dir')) as $file)
30     {
31       // attempt to upgrade booleans
32       $original = file_get_contents($file);
33       $upgraded = sfToolkit::pregtr($original, array(
34         '/^([^:]+: +)(?:on|y(?:es)?|\+)(\s*(#.*)?)$/im' => '\\1true\\2',
35         '/^([^:]+: +)(?:off|no?|-)(\s*(#.*)?)$/im'      => '\\1false\\2',
36       ));
37
38       try
39       {
40         sfYaml::setSpecVersion('1.1');
41         $yaml11 = sfYaml::load($original);
42
43         sfYaml::setSpecVersion('1.2');
44         $yaml12 = sfYaml::load($upgraded);
45       }
46       catch (Exception $e)
47       {
48         // unable to load the YAML
49         $yaml11 = 'foo';
50         $yaml12 = 'bar';
51       }
52
53       if ($yaml11 == $yaml12)
54       {
55         if ($original != $upgraded)
56         {
57           $this->getFilesystem()->touch($file);
58           file_put_contents($file, $upgraded);
59         }
60       }
61       else
62       {
63         $this->logSection('yaml', 'Unable to upgrade '.sfDebug::shortenFilePath($file), null, 'ERROR');
64
65         // force project to use YAML 1.1 spec
66         if ('1.1' != $specVersion)
67         {
68           $specVersion = '1.1';
69
70           $class = sfClassManipulator::fromFile(sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php');
71
72           $original = $class->getCode();
73           $modified = $class->wrapMethod('setup', 'sfYaml::setSpecVersion(\'1.1\');');
74
75           if ($original != $modified && $this->askConfirmation(array(
76             'Unable to convert YAML file:',
77             sfDebug::shortenFilePath($file),
78             '',
79             'Would you like to force YAML to be parsed with the 1.1 specification? (Y/n)',
80           ), 'QUESTION_LARGE'))
81           {
82             $this->logSection('yaml', 'Forcing YAML 1.1 spec');
83
84             $this->getFilesystem()->touch($class->getFile());
85             $class->save();
86           }
87           else
88           {
89             $this->logBlock(array('Unable to either upgrade YAML files or force 1.1 spec.', '(see UPGRADE_TO_1_3 file for more information)'), 'ERROR_LARGE');
90           }
91         }
92
93         $success = false;
94       }
95     }
96
97     if ($success && '1.1' == $specVersion)
98     {
99       $file = sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
100       $original = file_get_contents($file);
101       $modified = preg_replace('/^\s*sfYaml::setSpecVersion\(\'1\.1\'\);\n/im', '', $original);
102
103       if ($original != $modified)
104       {
105         $this->logSection('yaml', 'Removing setting of YAML 1.1 spec');
106
107         $this->getFilesystem()->touch($file);
108         file_put_contents($file, $modified);
109       }
110     }
111   }
112 }
113
Note: See TracBrowser for help on using the browser.