1: <?php
2: /**
3: * LICENSE (BSD)
4: *
5: * Copyright (c) 2013, Gerd Christian Kunze
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions are
10: * met:
11: *
12: * * Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
15: * * Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * * Neither the name of Gerd Christian Kunze nor the names of the
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34: *
35: * Changelog
36: * 18.02.2013 08:19
37: */
38: namespace MOC\Core;
39: use MOC\Api;
40: use MOC\Generic\Device\Core;
41:
42: /**
43: *
44: */
45: class Changelog implements Core {
46: /** @var Changelog $Singleton */
47: private static $Singleton = null;
48:
49: /**
50: * Get Dependencies
51: *
52: * @static
53: * @return Depending
54: */
55: public static function InterfaceDepending() {
56: return Api::Core()->Depending();
57: }
58:
59: /**
60: * Get Singleton/Instance
61: *
62: * @static
63: * @return object
64: */
65: public static function InterfaceInstance() {
66: if( self::$Singleton === null ) {
67: self::$Singleton = new Changelog();
68: } return self::$Singleton;
69: }
70:
71: /**
72: * Get Changelog
73: *
74: * @static
75: * @return Changelog
76: */
77: public static function InterfaceChangelog() {
78: return Api::Core()->Changelog()->Create( __CLASS__ )
79: ->Build()->Clearance( '03.06.2013 14:43', 'Development' )
80: ;
81: }
82:
83: /** @var string $RecordClass */
84: private $RecordClass = '-NA-';
85: /** @var Changelog\Record[] $RecordList */
86: private $RecordList = array();
87:
88: /** @var Changelog\Entry[] $Log */
89: private $Log = array();
90:
91: /** @var Version $Version */
92: private $Version = null;
93:
94: /**
95: * @param $__CLASS__
96: *
97: * @return Changelog
98: */
99: public function Create( $__CLASS__ ) {
100: $this->RecordClass = $__CLASS__;
101: $this->RecordList = array();
102: return $this;
103: }
104:
105: /**
106: * @param Changelog\Record $Record
107: *
108: * @return Changelog
109: */
110: public function Append( Changelog\Record $Record ) {
111: array_push( $this->RecordList, $Record );
112: return $this;
113: }
114:
115: /**
116: * @return Version
117: */
118: public function Version() {
119: $this->Setup();
120: return $this->Version;
121: }
122:
123: /**
124: * @return Changelog\Entry[]
125: */
126: public function Log() {
127: $this->Setup();
128: return $this->Log;
129: }
130:
131: /**
132: * @return array
133: */
134: public function Protocol() {
135: $Version = Api::Core()->Version();
136: $Protocol = array();
137: /** @var Changelog\Record $Record */
138: foreach( (array)$this->RecordList as $Record ) {
139: $Suspect = explode( '::', $Record->Method() );
140: $rMethod = new \ReflectionMethod( $Suspect[0], $Suspect[1] );
141: $ClassName = $rMethod->getDeclaringClass()->getShortName();
142: $MethodName = $rMethod->getName();
143: switch( $ClassName ) {
144: case 'Release': {
145: $Value = $Version->Release();
146: $Version->Release( $Value +1 );
147: $Version->Build( 0 );
148: $Version->Update( 0 );
149: $Version->Fix( 0 );
150: break;
151: }
152: case 'Build': {
153: $Value = $Version->Build();
154: $Version->Build( $Value +1 );
155: $Version->Update( 0 );
156: $Version->Fix( 0 );
157: break;
158: }
159: case 'Update': {
160: $Value = $Version->Update();
161: $Version->Update( $Value +1 );
162: $Version->Fix( 0 );
163: break;
164: }
165: case 'Fix': {
166: $Value = $Version->Fix();
167: $Version->Fix( $Value +1 );
168: break;
169: }
170: }
171: $Protocol[$this->RecordClass][] = array(
172: 'Timestamp' => $Record->Timestamp(),
173: 'Type' => $ClassName,
174: 'Action' => $MethodName,
175: 'Version' => clone $Version,
176: 'Message' => $Record->Message()
177: );
178: }
179: return $Protocol;
180: }
181:
182: private function Setup() {
183: $this->Log = array();
184: $this->Version = Api::Core()->Version();
185:
186: if( empty( $this->RecordList ) ) {
187:
188: $ChangeLogEntry = Changelog\Entry::InterfaceInstance();
189:
190: // $ChangeLogEntry->Location( $this->RecordClass );
191: $ChangeLogEntry->Message( 'MISSING CHANGELOG' );
192: $ChangeLogEntry->Timestamp( '-NA-' );
193:
194: $this->Log[] = $ChangeLogEntry;
195:
196: } else {
197:
198: /** @var Changelog\Record $Record */
199: foreach( (array)$this->RecordList as $Record ) {
200: $Suspect = explode( '::', $Record->Method() );
201: $rMethod = new \ReflectionMethod( $Suspect[0], $Suspect[1] );
202: $ClassName = $rMethod->getDeclaringClass()->getShortName();
203: $MethodName = $rMethod->getName();
204: $ChangeLogEntry = Changelog\Entry::InterfaceInstance();
205: switch( $ClassName ) {
206: case 'Release': {
207: $Value = $this->Version->Release();
208: $this->Version->Release( $Value +1 );
209: $this->Version->Build( 0 );
210: $this->Version->Update( 0 );
211: $this->Version->Fix( 0 );
212: break;
213: }
214: case 'Build': {
215: $Value = $this->Version->Build();
216: $this->Version->Build( $Value +1 );
217: $this->Version->Update( 0 );
218: $this->Version->Fix( 0 );
219: break;
220: }
221: case 'Update': {
222: $Value = $this->Version->Update();
223: $this->Version->Update( $Value +1 );
224: $this->Version->Fix( 0 );
225: break;
226: }
227: case 'Fix': {
228: $Value = $this->Version->Fix();
229: $this->Version->Fix( $Value +1 );
230: break;
231: }
232: }
233: $ChangeLogEntry->Timestamp( $Record->Timestamp() );
234: $ChangeLogEntry->Version( $this->Version->Number() );
235: $ChangeLogEntry->Type( $ClassName );
236: $ChangeLogEntry->Cause( $MethodName );
237: $ChangeLogEntry->Message( $Record->Message() );
238: //$ChangeLogEntry->Location( $Record->Location() );
239: $this->Log[] = $ChangeLogEntry;
240: }
241:
242: }
243: rsort( $this->Log );
244: }
245:
246: /**
247: * Allowed Changes:
248: *
249: * - Interface: NO (use Release)
250: * - Deprecate Method: NO (use Update)
251: * - Add Method: NO (use Update)
252: * - Remove Method: NO (use Build)
253: * - Internal Change: YES
254: *
255: * @return Changelog\Fix
256: */
257: public function Fix() {
258: return Changelog\Fix::InterfaceInstance();
259: }
260:
261: /**
262: * Allowed Changes:
263: *
264: * - Interface: NO (use Release)
265: * - Deprecate Method: YES
266: * - Add Method: YES
267: * - Remove Method: NO (use Build)
268: * - Internal Change: NO (use Fix)
269: *
270: * @return Changelog\Update
271: */
272: public function Update() {
273: return Changelog\Update::InterfaceInstance();
274: }
275:
276: /**
277: * Allowed Changes:
278: *
279: * - Interface: NO (use Release)
280: * - Deprecate Method: NO (use Update)
281: * - Add Method: NO (use Update)
282: * - Remove Method: YES
283: * - Internal Change: NO (use Fix)
284: *
285: * @return Changelog\Build
286: */
287: public function Build() {
288: return Changelog\Build::InterfaceInstance();
289: }
290:
291: /**
292: * Allowed Changes:
293: *
294: * - Interface: YES
295: * - Deprecate Method: NO (use Update)
296: * - Add Method: NO (use Update)
297: * - Remove Method: NO (use Build)
298: * - Internal Change: NO (use Fix)
299: *
300: * @return Changelog\Release
301: */
302: public function Release() {
303: return Changelog\Release::InterfaceInstance();
304: }
305: }
306: