1 | <?php |
||
34 | class DetectCommand extends Command { |
||
35 | |||
36 | /** |
||
37 | * @var Fetcher $fetcher |
||
38 | */ |
||
39 | protected $fetcher; |
||
40 | |||
41 | /** |
||
42 | * @var ConfigReader $configReader |
||
43 | */ |
||
44 | protected $configReader; |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | */ |
||
49 | protected $output; |
||
50 | |||
51 | /** |
||
52 | * Constructor |
||
53 | * |
||
54 | * @param Fetcher $fetcher |
||
55 | * @param ConfigReader $configReader |
||
56 | */ |
||
57 | public function __construct(Fetcher $fetcher, ConfigReader $configReader){ |
||
58 | parent::__construct(); |
||
59 | $this->fetcher = $fetcher; |
||
60 | $this->configReader = $configReader; |
||
61 | } |
||
62 | |||
63 | protected function configure(){ |
||
64 | $this |
||
65 | ->setName('upgrade:detect') |
||
66 | ->setDescription('Detect |
||
67 | - 1. currently existing code, |
||
68 | - 2. version in config.php, |
||
69 | - 3. online available verison. |
||
70 | (ASK) what to do? (download, upgrade, abort, …)') |
||
71 | ->addOption( |
||
72 | 'exit-if-none', null, InputOption::VALUE_NONE, 'exit with non-zero status code if new version is not found' |
||
73 | ) |
||
74 | ->addOption( |
||
75 | 'only-check', null, InputOption::VALUE_NONE, 'Only check if update is available' |
||
76 | ) |
||
77 | ; |
||
78 | ; |
||
79 | } |
||
80 | |||
81 | protected function execute(InputInterface $input, OutputInterface $output){ |
||
82 | $registry = $this->container['utils.registry']; |
||
83 | $registry->set('feed', false); |
||
84 | |||
85 | $fsHelper = $this->container['utils.filesystemhelper']; |
||
86 | $downloadController = new DownloadController($this->fetcher, $registry, $fsHelper); |
||
87 | try { |
||
88 | $currentVersion = $this->configReader->getByPath('system.version'); |
||
89 | if (!strlen($currentVersion)){ |
||
90 | throw new \UnexpectedValueException('Could not detect installed version.'); |
||
91 | } |
||
92 | |||
93 | $this->getApplication()->getLogger()->info('ownCloud ' . $currentVersion . ' found'); |
||
|
|||
94 | $output->writeln('Current version is ' . $currentVersion); |
||
95 | |||
96 | $feedData = $downloadController->checkFeed(); |
||
97 | if (!$feedData['success']){ |
||
98 | // Network errors, etc |
||
99 | $output->writeln("Can't fetch feed."); |
||
100 | $output->writeln($feedData['exception']->getMessage()); |
||
101 | $this->getApplication()->logException($feedData['exception']); |
||
102 | // Return a number to stop the queue |
||
103 | return $input->getOption('exit-if-none') ? 4 : null; |
||
104 | } |
||
105 | |||
106 | $feed = $feedData['data']['feed']; |
||
107 | if (!$feed->isValid()){ |
||
108 | // Feed is empty. Means there are no updates |
||
109 | $output->writeln('No updates found online.'); |
||
110 | return $input->getOption('exit-if-none') ? 4 : null; |
||
111 | } |
||
112 | |||
113 | $registry->set('feed', $feed); |
||
114 | $output->writeln($feed->getVersionString() . ' is found online'); |
||
115 | |||
116 | if ($input->getOption('only-check')){ |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | $action = $this->ask($input, $output); |
||
121 | if ($action === 'abort'){ |
||
122 | $output->writeln('Exiting on user command.'); |
||
123 | return 128; |
||
124 | } |
||
125 | |||
126 | $this->output = $output; |
||
127 | $packageData = $downloadController->downloadOwncloud([$this, 'progress']); |
||
128 | //Empty line, in order not to overwrite the progress message |
||
129 | $this->output->writeln(''); |
||
130 | if (!$packageData['success']){ |
||
131 | $registry->set('feed', null); |
||
132 | throw $packageData['exception']; |
||
133 | } |
||
134 | |||
135 | if ($action === 'download'){ |
||
136 | $output->writeln('Downloading has been completed. Exiting.'); |
||
137 | return 64; |
||
138 | } |
||
139 | } catch (\Exception $e){ |
||
140 | $this->getApplication()->getLogger()->error($e->getMessage()); |
||
141 | return 2; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Ask what to do |
||
147 | * @param InputInterface $input |
||
148 | * @param OutputInterface $output |
||
149 | * @return string |
||
150 | */ |
||
151 | public function ask(InputInterface $input, OutputInterface $output){ |
||
162 | |||
163 | /** |
||
164 | * Callback to output download progress |
||
165 | * @param ProgressEvent $e |
||
166 | */ |
||
167 | public function progress(ProgressEvent $e){ |
||
174 | } |
||
175 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: