TYPO3 CMS  TYPO3_6-2
Package.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\Flow\Package;
3 
4 /* *
5  * This script belongs to the TYPO3 Flow framework. *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
15 
21 class Package implements PackageInterface {
22 
27  protected $packageKey;
28 
32  protected $manifestPath;
33 
38  protected $packagePath;
39 
44  protected $classesPath;
45 
51  protected $protected = FALSE;
52 
56  protected $composerManifest;
57 
62  protected $packageMetaData;
63 
68  protected $classFiles;
69 
74  protected $namespace;
75 
82  protected $objectManagementEnabled = TRUE;
83 
87  protected $packageManager;
88 
102  if (preg_match(self::PATTERN_MATCH_PACKAGEKEY, $packageKey) !== 1) {
103  throw new \TYPO3\Flow\Package\Exception\InvalidPackageKeyException('"' . $packageKey . '" is not a valid package key.', 1217959510);
104  }
106  throw new \TYPO3\Flow\Package\Exception\InvalidPackagePathException(sprintf('Tried to instantiate a package object for package "%s" with a non-existing package path "%s". Either the package does not exist anymore, or the code creating this object contains an error.', $packageKey, $packagePath), 1166631889);
107  }
108  if (substr($packagePath, -1, 1) !== '/') {
109  throw new \TYPO3\Flow\Package\Exception\InvalidPackagePathException(sprintf('The package path "%s" provided for package "%s" has no trailing forward slash.', $packagePath, $packageKey), 1166633720);
110  }
111  if ($classesPath[1] === '/') {
112  throw new \TYPO3\Flow\Package\Exception\InvalidPackagePathException(sprintf('The package classes path provided for package "%s" has a leading forward slash.', $packageKey), 1334841320);
113  }
114  if (!file_exists($packagePath . $manifestPath . 'composer.json')) {
115  throw new \TYPO3\Flow\Package\Exception\InvalidPackageManifestException(sprintf('No composer manifest file found for package "%s". Please create one at "%scomposer.json".', $packageKey, $manifestPath), 1349776393);
116  }
117 
118  $this->packageManager = $packageManager;
119  $this->manifestPath = $manifestPath;
120  $this->packageKey = $packageKey;
121  $this->packagePath = Files::getNormalizedPath($packagePath);
122  if (isset($this->getComposerManifest()->autoload->{'psr-0'})) {
123  $this->classesPath = Files::getNormalizedPath($this->packagePath . $this->getComposerManifest()->autoload->{'psr-0'}->{$this->getNamespace()});
124  } else {
125  $this->classesPath = Files::getNormalizedPath($this->packagePath . $classesPath);
126  }
127  }
128 
135  public function boot(\TYPO3\Flow\Core\Bootstrap $bootstrap) {
136  }
137 
143  public function getPackageMetaData() {
144  if ($this->packageMetaData === NULL) {
145  $this->packageMetaData = new MetaData($this->getPackageKey());
146  $this->packageMetaData->setDescription($this->getComposerManifest('description'));
147  $this->packageMetaData->setVersion($this->getComposerManifest('version'));
148  $requirements = $this->getComposerManifest('require');
149  if ($requirements !== NULL) {
150  foreach ($requirements as $requirement => $version) {
151  if ($this->packageRequirementIsComposerPackage($requirement) === FALSE) {
152  // Skip non-package requirements
153  continue;
154  }
155  $packageKey = $this->packageManager->getPackageKeyFromComposerName($requirement);
156  $constraint = new MetaData\PackageConstraint(MetaDataInterface::CONSTRAINT_TYPE_DEPENDS, $packageKey);
157  $this->packageMetaData->addConstraint($constraint);
158  }
159  }
160 
161  }
162  return $this->packageMetaData;
163  }
164 
171  protected function packageRequirementIsComposerPackage($requirement) {
172  return (strpos($requirement, '/') !== FALSE);
173  }
174 
180  public function getClassFiles() {
181  if (!is_array($this->classFiles)) {
182  $this->classFiles = $this->buildArrayOfClassFiles($this->classesPath);
183  }
184  return $this->classFiles;
185  }
186 
192  public function getFunctionalTestsClassFiles() {
193  return $this->buildArrayOfClassFiles($this->packagePath . self::DIRECTORY_TESTS_FUNCTIONAL, $this->getNamespace() . '\\Tests\\Functional\\');
194  }
195 
202  public function getPackageKey() {
203  return $this->packageKey;
204  }
205 
213  public function getNamespace() {
214  if (!$this->namespace) {
215  $manifest = $this->getComposerManifest();
216  if (isset($manifest->autoload->{'psr-0'})) {
217  $namespaces = (array)$manifest->autoload->{'psr-0'};
218  if (count($namespaces) === 1) {
219  $namespace = key($namespaces);
220  } else {
221  throw new \TYPO3\Flow\Package\Exception\InvalidPackageStateException(sprintf('The Composer manifest of package "%s" contains multiple namespace definitions in its autoload section but Flow does only support one namespace per package.', $this->packageKey), 1348053245);
222  }
223  } else {
224  $namespace = str_replace('.', '\\', $this->getPackageKey());
225  }
226  $this->namespace = $namespace;
227  }
228  return $this->namespace;
229  }
230 
237  public function isProtected() {
238  return $this->protected;
239  }
240 
246  public function isObjectManagementEnabled() {
248  }
249 
257  public function setProtected($protected) {
258  $this->protected = (boolean)$protected;
259  }
260 
267  public function getPackagePath() {
268  return $this->packagePath;
269  }
270 
276  public function getManifestPath() {
277  return $this->packagePath . $this->manifestPath;
278  }
279 
286  public function getClassesPath() {
287  return $this->classesPath;
288  }
289 
297  public function getClassesNamespaceEntryPath() {
298  $pathifiedNamespace = str_replace('\\', '/', $this->getNamespace());
299  return Files::getNormalizedPath($this->classesPath . trim($pathifiedNamespace, '/'));
300  }
301 
308  public function getFunctionalTestsPath() {
309  return $this->packagePath . self::DIRECTORY_TESTS_FUNCTIONAL;
310  }
311 
318  public function getResourcesPath() {
319  return $this->packagePath . self::DIRECTORY_RESOURCES;
320  }
321 
328  public function getConfigurationPath() {
329  return $this->packagePath . self::DIRECTORY_CONFIGURATION;
330  }
331 
338  public function getMetaPath() {
339  return $this->packagePath . self::DIRECTORY_METADATA;
340  }
341 
348  public function getDocumentationPath() {
349  return $this->packagePath . self::DIRECTORY_DOCUMENTATION;
350  }
351 
358  public function getPackageDocumentations() {
359  $documentations = array();
360  $documentationPath = $this->getDocumentationPath();
361  if (is_dir($documentationPath)) {
362  $documentationsDirectoryIterator = new \DirectoryIterator($documentationPath);
363  $documentationsDirectoryIterator->rewind();
364  while ($documentationsDirectoryIterator->valid()) {
365  $filename = $documentationsDirectoryIterator->getFilename();
366  if ($filename[0] != '.' && $documentationsDirectoryIterator->isDir()) {
367  $filename = $documentationsDirectoryIterator->getFilename();
368  $documentation = new \TYPO3\Flow\Package\Documentation($this, $filename, $documentationPath . $filename . '/');
369  $documentations[$filename] = $documentation;
370  }
371  $documentationsDirectoryIterator->next();
372  }
373  }
374  return $documentations;
375  }
376 
384  public function getComposerManifest($key = NULL) {
385  if (!isset($this->composerManifest)) {
386  $this->composerManifest = PackageManager::getComposerManifest($this->getManifestPath());
387  }
388 
389  return PackageManager::getComposerManifest($this->getManifestPath(), $key, $this->composerManifest);
390  }
391 
404  protected function buildArrayOfClassFiles($classesPath, $extraNamespaceSegment = '', $subDirectory = '', $recursionLevel = 0) {
405  $classFiles = array();
406  $currentPath = $classesPath . $subDirectory;
407  $currentRelativePath = substr($currentPath, strlen($this->packagePath));
408 
409  if (!is_dir($currentPath)) {
410  return array();
411  }
412  if ($recursionLevel > 100) {
413  throw new \TYPO3\Flow\Package\Exception('Recursion too deep.', 1166635495);
414  }
415 
416  try {
417  $classesDirectoryIterator = new \DirectoryIterator($currentPath);
418  while ($classesDirectoryIterator->valid()) {
419  $filename = $classesDirectoryIterator->getFilename();
420  if ($filename[0] != '.') {
421  if (is_dir($currentPath . $filename)) {
422  $classFiles = array_merge($classFiles, $this->buildArrayOfClassFiles($classesPath, $extraNamespaceSegment, $subDirectory . $filename . '/', ($recursionLevel + 1)));
423  } else {
424  if (substr($filename, -4, 4) === '.php') {
425  $className = (str_replace('/', '\\', ($extraNamespaceSegment . substr($currentPath, strlen($classesPath)) . substr($filename, 0, -4))));
426  $classFiles[$className] = $currentRelativePath . $filename;
427  }
428  }
429  }
430  $classesDirectoryIterator->next();
431  }
432 
433  } catch (\Exception $exception) {
434  throw new \TYPO3\Flow\Package\Exception($exception->getMessage(), 1166633721);
435  }
436  return $classFiles;
437  }
438 
451  public function __sleep() {
452  $properties = get_class_vars(get_class($this));
453  unset($properties['packageManager']);
454  return array_keys($properties);
455  }
456 
467  public function __wakeup() {
468  if (isset($GLOBALS['TYPO3_currentPackageManager'])) {
469  $this->packageManager = $GLOBALS['TYPO3_currentPackageManager'];
470  }
471  }
472 }
473 
474 ?>
static getNormalizedPath($path)
Definition: Files.php:41
static is_link($pathAndFilename)
Definition: Files.php:277
boot(\TYPO3\Flow\Core\Bootstrap $bootstrap)
Definition: Package.php:135
packageRequirementIsComposerPackage($requirement)
Definition: Package.php:171
getComposerManifest($key=NULL)
Definition: Package.php:384
setProtected($protected)
Definition: Package.php:257
buildArrayOfClassFiles($classesPath, $extraNamespaceSegment='', $subDirectory='', $recursionLevel=0)
Definition: Package.php:404
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
__construct(\TYPO3\Flow\Package\PackageManager $packageManager, $packageKey, $packagePath, $classesPath=NULL, $manifestPath='')
Definition: Package.php:101
static getComposerManifest($manifestPath, $key=NULL, $composerManifest=NULL)