This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ChangeRegistry.php
125 lines (113 loc) · 3.42 KB
/
ChangeRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* A registry to track potential changes to files we have parsed
*
* This could extend Zend_Registry or something similar if we desired.
*/
class Scisr_ChangeRegistry
{
/**
* Stores the raw data we've been given
* @var array
*/
private $_data;
/**
* Set a value
* @param string $name the name of the value
* @param mixed $value the value
*/
public function set($name, $value)
{
$this->_data[$name] = $value;
}
/**
* Get a value
* @param string $name the name of the value
* @return mixed the value stored for this name
*/
public function get($name)
{
return (isset($this->_data[$name]) ? $this->_data[$name] : null);
}
/**
* Clear all data stored in the registry
*/
public static function clearAll()
{
}
/**
* Set a potential change to a file
* @param string $filename the filename
* @param int $line the line number that our change begins on
* @param int $column the column number that our change begins at
* @param int $length the length of the original text to be replaced
* @param string $replacement the text to insert in its place
* @param boolean $tentative true if this change is something we "aren't
* sure about" - for example, a word match found in a string. Changes marked
* tentative will only be acted upon if we are in "aggressive" mode.
*/
public function addChange($filename, $line, $column, $length, $replacement, $tentative=false)
{
$file = $this->getFile($filename);
$file->addEdit($line, $column, $length, $replacement, $tentative);
$this->setFile($file);
}
/**
* Create a new file.
* @param string $filename the filename
* @param string $content file content
* @param boolean $tentative true if we want to overwrite the file
*/
public function createFile($filename, $content, $tentative = false) {
$file = new Scisr_CreateFile($filename, $content);
$this->setFile($file);
}
/**
* Get the stored file object for a given filename
* @param string $filename the filename
* @return Scisr_File
*/
protected function getFile($filename)
{
$changes = $this->getChanges();
// We just store our pending changes as file objects themselves. If one
// doesn't exist yet for this file, create it
if (!isset($changes[$filename])) {
$changes[$filename] = new Scisr_File($filename);
}
return $changes[$filename];
}
/**
* Save the stored file object
* @param Scisr_File the file to save
*/
protected function setFile($file)
{
$changes = $this->getChanges();
$changes[$file->filename] = $file;
$this->set('storedChanges', $changes);
}
/**
* Get stored file changes
* @return array(Scisr_File)
*/
private function getChanges()
{
$changes = $this->get('storedChanges');
if (!is_array($changes)) {
$changes = array();
}
return $changes;
}
/**
* Set a file to be renamed
* @param string $oldName the path to the file to be renamed
* @param string $newName the new path to give it
*/
public function addRename($oldName, $newName)
{
$file = $this->getFile($oldName);
$file->rename($newName);
$this->setFile($file);
}
}