1: <?php
2: /**
3: * LICENSE (BSD)
4: *
5: * Copyright (c) 2012, 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: * Depending
36: * 31.08.2012 12:50
37: */
38: namespace MOC\Core;
39: use MOC\Api;
40: use MOC\Core\Depending\Package;
41: use MOC\Generic\Device\Core;
42:
43: /**
44: *
45: */
46: class Depending implements Core {
47:
48: /** @var Package[] $Dependencies */
49: private $PackageList = array();
50:
51: /**
52: * Get Singleton/Instance
53: *
54: * @static
55: * @return Depending
56: */
57: public static function InterfaceInstance() {
58: return new Depending();
59: }
60:
61: /**
62: * Get Changelog
63: *
64: * @static
65: * @return Changelog
66: */
67: public static function InterfaceChangelog() {
68: return Api::Core()->Changelog()->Create( __CLASS__ )
69: ->Build()->Clearance( '03.06.2013 14:43', 'Redesign' )
70: ->Build()->Clearance( '03.06.2013 14:58', 'Development' )
71: ;
72: }
73:
74: /**
75: * Get Dependencies
76: *
77: * @static
78: * @return Depending
79: */
80: public static function InterfaceDepending() {
81: return Api::Core()->Depending()
82: ->AddPackage( Api::Core()->Depending()->NewPackage()->SetNamespace( 'MOC\Core\Depending' )
83: ->SetClass( 'Package' )->SetOptional( false )->SetVersion(
84: Api::Core()->Version()
85: )
86: )
87: ->AddPackage( Api::Core()->Depending()->NewPackage()->SetNamespace( 'MOC\Core\Depending' )
88: ->SetClass( 'Result' )->SetOptional( false )->SetVersion(
89: Api::Core()->Version()
90: )
91: )
92: ;
93: }
94:
95: /**
96: * Create a new Dependency-Package
97: *
98: * @return Package
99: */
100: public function NewPackage() {
101: return Package::InterfaceInstance();
102: }
103:
104: /**
105: * @param Package $Package
106: *
107: * @return Depending
108: */
109: public function AddPackage( Package $Package ){
110: $this->PackageList[] = $Package;
111: return $this;
112: }
113:
114: /**
115: * @return Package[]
116: */
117: public function GetPackageList() {
118: return $this->PackageList;
119: }
120:
121: /**
122: * @return int
123: */
124: private function PackageTree() {
125: foreach( $this->PackageList as $Package ) {
126: if( $Package->GetFile()->Exists() ) {
127: $Object = $Package->GetFQN();
128: /** @var \MOC\Generic\Common $Object */
129: $Children = $Object::InterfaceDepending()->GetPackageList();
130: foreach( $Children as $ChildPackage ) {
131: if( !in_array( $ChildPackage, $this->PackageList ) ) {
132: $this->PackageList[] = $ChildPackage;
133: }
134: }
135: }
136: }
137: return count( $this->PackageList );
138: }
139:
140: /**
141: * @return Depending\Result
142: */
143: public function Analyze() {
144: $Analyze = Version::InterfaceInstance();
145: $Result = Depending\Result::InterfaceInstance();
146:
147: // Collect Package-Tree
148: $PackageCount = count( $this->PackageList );
149: while( $PackageCount < $this->PackageTree() ) {
150: $PackageCount = count( $this->PackageList );
151: }
152:
153: // Analyze Package-Dependencies
154: /** @var Package[] $RequiredVersion */
155: $RequiredVersion = array();
156: foreach( $this->PackageList as $Package ) {
157: if( array_key_exists( $Package->GetFQN(), $RequiredVersion ) ) {
158: if( true === $Analyze->Compare( $RequiredVersion[$Package->GetFQN()]->GetVersion(), $Package->GetVersion() ) ) {
159: $RequiredVersion[$Package->GetFQN()] = $Package;
160: }
161: } else {
162: $RequiredVersion[$Package->GetFQN()] = $Package;
163: }
164: }
165:
166: // Create Package-Update-List
167: foreach( $RequiredVersion as $Package ) {
168: if( $Package->GetFile()->Exists() ) {
169: /** @var \MOC\Generic\Common $Object */
170: $Object = $Package->GetFQN();
171: $Changelog = $Object::InterfaceChangelog();
172: if( true === $Analyze->Compare( $Changelog->Version(), $Package->GetVersion() ) ) {
173: $Package->SetChangelog( $Changelog );
174: if( $Package->GetOptional() ) {
175: $Result->UpdateOptional( $Package );
176: } else {
177: $Result->Update( $Package );
178: }
179: } else {
180: $Package->SetVersion( $Changelog->Version() );
181: $Result->Available( $Package );
182: }
183: } else {
184: if( $Package->GetOptional() ) {
185: $Result->InstallOptional( $Package );
186: } else {
187: $Result->Install( $Package );
188: }
189: }
190: }
191:
192: return $Result;
193: }
194: }
195: