Bundle.php 19.4 KB
Newer Older
Pham Huy committed
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
<?php
/**
 * A bundle may use one or more text domains, and may or may not physically house them. 
 * Essentially a bundle "uses" a text domain.
 * Types are "theme", "plugin" and "core" 
 */
abstract class Loco_package_Bundle extends ArrayObject implements JsonSerializable {

    /**
     * Internal handle for targeting in WordPress, e.g. "twentyfifteen" or "loco-translate/loco.php"
     * @var string
     */
    private $handle;

    /**
     * Short name, e.g. "twentyfifteen" or "loco-translate"
     * @var string
     */
    private $slug;

    /**
     * Friendly name, e.g. "Twenty Fifteen
     * @var string
     */
    private $name;

    /**
     * Full path to root directory of bundle
     * @var Loco_fs_Directory
     */
    private $root;

    /**
     * Directory paths to exclude from all projects
     * @var Loco_fs_FileList
     */
    private $xpaths;

    /**
     * Full path to PHP bootsrap file
     * @var string
     */
    private $boot;

    /**
     * Whether bundle is a single file, as opposed to in its own directory
     * @var bool
     */
    protected $solo;

    /**
     * Method with which bundle has been configured
     * @var string|false (file|db|meta|internal)
     */
    private $saved = false;

    /**
     * Get system (i.e. "global") target locations for all projects of this type.
     * These are aways append to configs, and always excluded from serialization
     * @return array<string> absolute directory paths
     */
    abstract public function getSystemTargets();

    /**
     * Get canonical info registered with WordPress, i.e. plugin or theme headers
     * @return Loco_package_Header
     */
    abstract public function getHeaderInfo();

    /**
     * Get built-in translatable values mapped to annotation for translators
     * @return array 
     */
    abstract public function getMetaTranslatable();

    /**
     * Get type of Bundle (title case)
     * @return string
     */
    abstract public function getType();


    /**
     * Construct bundle from unique ID containing type and handle
     * @param string
     * @return Loco_package_Bundle
     */
    public static function fromId( $id ){
        $r = explode( '.', $id, 2 );
        return self::createType( $r[0], isset($r[1]) ? $r[1] : '' );
    }

    
    /**
     * @param string
     * @param string
     * @return Loco_package_Bundle
     * @throws Loco_error_Exception
     */
    public static function createType( $type, $handle ){
        $func = array( 'Loco_package_'.ucfirst($type), 'create' );
        if( is_callable($func) ){
            $bundle = call_user_func( $func, $handle );
        }
        else {
            throw new Loco_error_Exception('Unexpected bundle type: '.$type );
        }  
        return $bundle;
    }

 
    /**
     * Construct from WordPress handle and friendly name
     * @param string
     * @param string
     */
    public function __construct( $handle, $name ){
        parent::__construct( array() );
        $this->setHandle($handle)->setName($name);
        $this->xpaths = new Loco_fs_FileList;
    }


    /**
     * Re-fetch this bundle from its currently saved location
     * @return Loco_package_Bundle
     */
    public function reload(){
        return call_user_func( array( get_class($this), 'create' ), $this->getSlug() );
    }


    /**
     * Get ID that uniquely identifies bundle by its type and handle
     * @return string
     */
    public function getId(){
        $type = strtolower( $this->getType() );
        return $type.'.'.$this->getHandle();
    }



    /**
     * @return string
     */
    public function __toString(){
        return (string) $this->name;
    }


    /**
     * @return bool
     */
    public function isTheme(){
        return false;
    }


    /**
     * Get parent bundle if possible
     * @codeCoverageIgnore
     * @return Loco_package_Bundle|null
     */
    public function getParent(){
        trigger_error( $this->getType().' bundles cannot have parents. Check isTheme first', E_USER_NOTICE );
        return null;
    }


    /**
     * @return bool
     */
    public function isPlugin(){
        return false;
    }


    /**
     * Get handle of bundle unique for its type, e.g. "twentyfifteen" or "loco-translate/loco.php"
     * @return string
     */
    public function getHandle(){
        return $this->handle;
    }


    /**
     * Attempt to get the vendor-specific slug, which may or may not be the same as the internal handle
     * @return string
     */
    public function getSlug(){
        if( $slug = $this->slug ){
            return $slug;
        }
        // fall back to runtime handle
        return $this->getHandle();
    }


    /**
     * Set friendly name of bundle
     * @param string
     * @return Loco_package_Bundle
     */
    public function setName( $name ){
        $this->name = $name;
        return $this;
    }


    /**
     * Set short name of bundle which may or may not match unique handle
     * @param string
     * @return Loco_package_Bundle
     */
    public function setSlug( $slug ){
        $this->slug = $slug;
        return $this;
    }


    /**
     * Set internal handle registered with WordPress for this bundle type
     * @param string
     * @return Loco_package_Bundle
     */
    public function setHandle( $handle ){
        $this->handle = $handle;
        return $this;
    }


    /**
     * Get friendly name of bundle, e.g. "Twenty Fifteen" or "Loco Translate"
     * @return string
     */
    public function getName(){
        return $this->name;
    }
    
    
    /**
     * Whether bundle root is currently known
     * @return bool
     */
    public function hasDirectoryPath(){
        return (bool) $this->root;
    }    


    /**
     * Set root directory for bundle. e.g. theme or plugin directory
     * @param string
     * @return Loco_package_Bundle
     */
    public function setDirectoryPath( $path ){
        $this->root = new Loco_fs_Directory( $path );
        $this->root->normalize();
        return $this;
    }


    /**
     * Get absolute path to root directory for bundle. e.g. theme or plugin directory
     * @return string
     */
    public function getDirectoryPath(){
        if( $this->root ){
            return $this->root->getPath();
        }
        // without a root directory return WordPress root
        return untrailingslashit(ABSPATH);
    }


    /**
     * @return string[]
     */
    public function getVendorRoots(){
        $dirs = array();
        $base = (string) $this->getDirectoryPath();
        foreach( array('node_modules','vendor') as $f ){
            $path = $base.'/'.$f;
            if( is_dir($path) ){
                $dirs[] = $path;
            }
        }
        return $dirs;
    }


    /**
     * Get file locations to exclude from all projects in bundle. These are effectively "hidden"
     * @return Loco_fs_FileList
     */
    public function getExcludedLocations(){
        return $this->xpaths;
    }


    /**
     * Add a path for excluding from all projects
     * @param Loco_fs_File|string
     * @return Loco_package_Bundle
     */
    public function excludeLocation( $path ){
        $this->xpaths->add( new Loco_fs_File($path) );
        return $this;
    }


    /**
     * Create a file searcher from root location, excluding that which is excluded
     * @return Loco_fs_FileFinder
     */
    public function getFileFinder(){
        $root = $this->getDirectoryPath();
        /*/ if bundle is symlinked it's resource files won't be matched properly
        if( is_link($root) && ( $real = realpath($root) ) ){
            $root = $real;
        }*/
        $finder = new Loco_fs_FileFinder( $root );
        foreach( $this->xpaths as $path ){
            $finder->exclude( (string) $path );
        }
        return $finder;
    }


    /**
     * Get primary PHP source file containing bundle bootstrap code, if applicable
     * @return string
     */
    public function getBootstrapPath(){
        return $this->boot;
    }


    /**
     * Set primary PHP source file containing bundle bootstrap code, if applicable.
     * @return Loco_package_Bundle
     */
    public function setBootstrapPath( $path ){
        $this->boot = (string) $path;
        // base directory can be inferred from bootstrap path
        if( ! $this->hasDirectoryPath() ){
            $this->setDirectoryPath( dirname($this->boot) );
        }
        return $this;
    }


    /**
     * Test whether bundle consists of a single file
     */
    public function isSingleFile(){
        return (bool) $this->solo;
    }
    
    
    /**
     * Add all projects defined in a TextDomain
     * @param Loco_package_TextDomain
     * @return Loco_package_Bundle
     */
    public function addDomain( Loco_package_TextDomain $domain ){
        /* @var Loco_package_Project $proj */
        foreach( $domain as $proj ){
            $this->addProject($proj);
        }
        return $this;
    }


    /**
     * Add a translation project to bundle.
     * Note that this always adds without checking uniqueness. Call hasProject first if it could be a duplicate
     * @param Loco_package_Project
     * @return Loco_package_Bundle
     */
    public function addProject( Loco_package_Project $project ){
        // add global targets
        foreach( $this->getSystemTargets() as $path ){
            $project->addSystemTargetDirectory( $path );
        }
        // add global exclusions affecting source and target locations
        foreach( $this->xpaths as $path ){
            $project->excludeLocation( $path );
        }
        // projects must be unique by Text Domain and "slug" (used to prefix files)
        // however, I am not indexing them here on purpose so domain and slug may be added at any time.
        $this[] = $project;
        return $this;
    }


    /**
     * Export projects grouped by domain
     * @return array indexed by Text Domain name
     */
    public function exportGrouped(){
        $domains = array();
        /* @var $proj Loco_package_Project */
        foreach( $this as $proj ){
            $domain = $proj->getDomain();
            $key = $domain->getName();
            $domains[$key][] = $proj; 
        }
        return $domains;
    }



    /**
     * Create a suitable Text Domain from bundle's name.
     * Note that internal handle may be a directory name differing entirely from the author's intention, hence the configured bundle name is slugged instead
     * @return Loco_package_TextDomain
     */
    public function createDomain(){
        $slug = sanitize_title( $this->name, $this->slug );
        return new Loco_package_TextDomain( $slug );
    }



    /**
     * Generate default configuration. 
     * Adds a simple one domain, one project config
     * @param string optional Text Domain to use
     * @return Loco_package_Project
     */
    public function createDefault( $domainName = null ){
        if( is_null($domainName) ){
            $domain = $this->createDomain();
        }
        else {
            $domain = new Loco_package_TextDomain($domainName);
        }
        $project = $domain->createProject( $this, $this->name );
        if( $this->solo ){
            $project->addSourceFile( $this->getBootstrapPath() );
        }
        else {
            $project->addSourceDirectory( $this->getDirectoryPath() );
        }

        $this->addProject( $project );
        return $project;
    }



    /**
     * Configure from custom saved option
     * @return bool whether configured
     */
    public function configureDb(){
        if( $option = $this->getCustomConfig() ){
            $option->configure();
            $this->saved = 'db';
            return true;
        }
        return false;
    }



    /**
     * Configure from XML config
     * @return bool whether configured
     */
    public function configureXml(){
        if( $xmlfile = $this->getConfigFile() ){
            $reader = new Loco_config_BundleReader($this);
            $reader->loadXml( $xmlfile );
            $this->saved = 'file';
            return true;
        }
        return false;
    }



    /**
     * Get XML configuration file used to define this bundle
     * TODO will we also support JSON for when dom extension is loaded?
     * TODO support custom location for user-saved XML?
     * @return Loco_fs_File
     */
    public function getConfigFile(){
        $base = $this->getDirectoryPath();
        $file = new Loco_fs_File( $base.'/loco.xml' );
        if( ! $file->exists() || ! loco_check_extension('dom') ){
            return null;
        }
        return $file;
    }



    /**
     * Check whether bundle is manually configured, as opposed to guessed
     * @return string (file|db|meta|internal)
     */    
    public function isConfigured(){
        return $this->saved;
    }


    /**
     * Do basic configuration from bundle meta data (file headers)
     * @param array header tags from theme or plugin bootstrapper
     * @return bool whether configured
     */
    public function configureMeta( array $header ){
        if( isset($header['Name']) ){
            $this->setName( $header['Name'] );
        }
        if( isset($header['TextDomain']) && ( $slug = $header['TextDomain'] ) ){
            $domain = new Loco_package_TextDomain($slug);
            $domain->setCanonical( true );
            // use domain as bundle handle and slug if not set when constructed
            if( ! $this->handle ){
                $this->handle = $slug;
            }
            if( ! $this->getSlug() ){
                $this->setSlug( $slug );
            }
            $project = $domain->createProject( $this, $this->name );
            // May have declared DomainPath
            $base = $this->getDirectoryPath();
            if( isset($header['DomainPath']) && ( $path = trim($header['DomainPath'],'/') ) ){
                $project->addTargetDirectory( $base.'/'.$path );
            }
            else if( $this->solo ){
                // skip
            }
            // else use standard language path if it exists
            else if( is_dir($base.'/languages') ) {
                $project->addTargetDirectory($base.'/languages');
            }
            // else add bundle root by default
            else {
                $project->addTargetDirectory( $base );
            }
            // single file bundles can have only one source file
            if( $this->solo ){
                $project->addSourceFile( $this->getBootstrapPath() );
            }
            // else add bundle root as default source file location
            else {
                $project->addSourceDirectory( $base );
            }
            // automatically block common vendor locations
            foreach( $this->getVendorRoots() as $root ){
                $this->excludeLocation($root);
            }
            // default domain added
            $this->addProject($project);
            $this->saved = 'meta';
            return true;
        }

        return false;
    }


    /**
     * Configure bundle from canonical sources.
     * Source order is "db","file","meta" where meta is the auto-config fallback.
     * No deep scanning is performed at this point
     * @return Loco_package_Bundle
     */
    public function configure( $base, array $header ){
        $this->setDirectoryPath( $base );
        $this->configureDb() || $this->configureXml() || $this->configureMeta($header);
        return $this;
    }


    /**
     * Get the custom config saved in WordPress DB for this bundle
     * @return Loco_config_CustomSaved
     */
    public function getCustomConfig(){
        $custom = new Loco_config_CustomSaved;
        if( $custom->setBundle($this)->fetch() ){
            return $custom;
        }
    }


    /**
     * Inherit another bundle. Used for child themes to display parent translations
     * @return Loco_package_Bundle
     */
    public function inherit( Loco_package_Bundle $parent ){
        foreach( $parent as $project ){
            if( ! $this->hasProject($project) ){
                $this->addProject( $project );
            }
        }
        return $this;
    }



    /**
     * Get unique translation project by text domain (and optionally slug)
     * TODO would prefer to avoid iteration, but slug can be changed at any time
     * @param string
     * @param string | null
     * @return Loco_package_Project
     */
    public function getProject( $domain, $slug = null ){
        if( is_null($slug) ){
            $slug = $domain;
        }
        /* @var $project Loco_package_Project */
        foreach( $this as $project ){
            if( $project->getSlug() === $slug && $project->getDomain()->getName() === $domain ){
                return $project;
            }
        }
        return null;
    }


    /**
     * @return Loco_package_Project
     */
    public function getDefaultProject(){
        $i = 0;
        /* @var $project Loco_package_Project */
        foreach( $this as $project ){
            if( $project->isDomainDefault() ){
                return $project;
            }
            $i++;
        }
        // nothing is domain default, but if we only have one, then duh
        if( 1 === $i ){
            return $project;
        }
    }

    
    /**
     * Test if project already exists in bundle
     * @param Loco_package_Project
     * @return bool
     */
    public function hasProject( Loco_package_Project $project ){
        return (bool) $this->getProject( $project->getDomain()->getName(), $project->getSlug() );
    }


    /**
     * @return array<Loco_package_TextDomain>
     */
    public function getDomains(){
        $domains = array();
        /* @var $project Loco_package_Project */
        foreach( $this as $project ){
            if( $domain = $project->getDomain() ){
                $d = (string) $domain;
                if( ! isset($domains[$d]) ){
                    $domains[$d] = $domain;
                }
            }
        }
        return $domains;
    }



    /**
     * Get newest timestamp of all translation files (includes template, but exclude source files)
     * @return int
     */    
    public function getLastUpdated(){
        // recent items is a convenient cache for checking last modified times
        $t = Loco_data_RecentItems::get()->hasBundle( $this->getId() );
        // else have to scan targets across all projects
        if( 0 === $t ){
            /* @var $project Loco_package_Project */
            foreach( $this as $project ){
                $t = max( $t, $project->getLastUpdated() );
            }
        }
        return $t;
    }
     

    /**
     * Get project by ID
     * @param string <domain>[.<slug>]
     * @return Loco_package_Project
     */
    public function getProjectById( $id ){
        list( $domain, $slug ) = Loco_package_Project::splitId($id);
        return $this->getProject( $domain, $slug );
    }


    /**
     * Reset bundle configuration, but keep metadata like name and slug.
     * Call this before applying a saved config, otherwise values will just be added on top.
     * @return Loco_package_Bundle
     */
    public function clear(){
        $this->exchangeArray( array() );
        $this->xpaths = new Loco_fs_FileList;
        $this->saved = false;
        return $this;
    }


    /**
     * @return array
     */
    public function jsonSerialize(){
        $writer = new Loco_config_BundleWriter( $this );
        return $writer->toArray();
    }


    /**
     * Create a copy of this bundle containing any files found that aren't currently configured
     * @return Loco_package_Bundle
     */
    public function invert(){
        return Loco_package_Inverter::compile( $this );
    }


}